0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder Beginner Contest 449

0
Posted at

A - π

問題文

面積を求める問題です。
問題の本質の内容は小数の型の仕様を理解しているかです。
誤差は10^6までです。
コンテスト中は下記のコーディングをしました。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    double d;
    cin >> d;

    d = d / 2.0;

    cout << fixed_setprecision(7) << d * d * pai << endl;

    return 0;
} 

計算部分に関しては、

C++
fixed_setprecision(7) << d * d * pai / 4

でもいけます。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    double d;
    cin >> d;

    cout << fixed_setprecision(7) << d * d * pai / 4 << endl;

    return 0;
} 

B - Deconstruct Chocolate

問題文

問題はクエリによって、
・(0, 0)からR行を消す。
・(0, 0)からC列を消す。
という問題です。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    int h, w, q;
    cin >> h >> w >> q;

    rep(i, q){
        int x, rc;
        cin >> x >> rc;

        if(x == 1){
            cout << w * rc << endl;
            h -= rc;
        }
        if(x == 2){
            cout << h * rc << endl;
            w -= rc;
        }
    }

    return 0;
} 

C - Comfortable Distance

問題文

累積和、尺取り法の問題です。
コンテスト中は尺取り法にてACしました。
尺取り法で実装するとコーディングの内容が複雑化してWAを出しやすいです。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    ll N, L, R;
    cin >> N >> L >> R;
    string S;
    cin >> S;

    ll ans = 0;
    ll r = L;
    map<char, ll> mp;
    for(ll i=0; i<N; i++){
        while(r < N && r <= R + i){
            if(L + i <= r){
                mp[S[r]]++;
            }
            r++;
        }
        
        ans += mp[S[i]];
        if(L + i < N) mp[S[(L + i)]]--;
    }
    cout << ans << endl;

    return 0;
} 

非常に分かりにくいですね。
ここは、追加する文字列と削除する文字列を別々に処理すると綺麗にコーディングできます。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    ll n, l, r;
    cin >> n >> l >> r;
    string s;
    cin >> s;

    ll ans = 0;
    map<char, ll> mp;
    for(ll i=0; i<n; i++){
        if(i - l >= 0) mp[s[i - l]]++;
        if(i - r - 1 >= 0) mp[s[i - r - 1]]--;
        ans += mp[s[i]];
    }
    cout << ans << endl;

    return 0;
} 

累積和のコードも考察しましょう。

C++
#include <bits/stdc++.h>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }

int main() {
    ll n, l, r;
    cin >> n >> l >> r;
    string s;
    cin >> s;

    vector<vector<ll>> ss(26, vector<ll>(n+1, 0));
    for(ll i=0; i<n; i++){
        ss[s[i] - 'a'][i]++;
        for(ll j=0; j<26; j++){
            ss[j][i+1] = ss[j][i+1] + ss[j][i];
        }
    }

    ll ans = 0;
    for(ll i=0; i<n; i++){
        ans += ss[s[i] - 'a'][min(r+i, n)] - ss[s[i] - 'a'][min(l+i-1, n)];
    }
    cout << ans << endl;

    return 0;
} 

問題文

C++
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?