LoginSignup
0
0

More than 1 year has passed since last update.

Codeforces Round #784 (Div. 4) 解いた感想

Posted at

A. Division?

問題文に従ってif文を書きました.

void solve(){
    ll n; cin >> n;
    ll ans=-1;
    if(n<=1399) ans=4;
    else if(n<=1599) ans=3;
    else if(n<=1899) ans=2;
    else ans=1;
    printf("Division %lld\n", ans);
}

B. Triple

数字の出現回数をmapで数えて3回以上現れるものを出力しました.

void solve(){
    ll n; cin >> n;
    map<ll, ll> mp;
    for(ll i=0; i<n; i++){
        ll t; cin >> t;
        mp[t]++;
    }
    ll ans=-1;
    for(auto [a, b] : mp){
        if(b>=3) ans=a; 
    }
    cout << ans << "\n";
}
 

C. Odd/Even Increments

$a_0$と$a_1$の偶奇を調べて,異なっている場合は,配列の偶数番目の要素を全て+1しました.
この操作によって,全ての要素の偶奇が揃うか?を調べて判定します.

void solve(){
    ll n; cin >> n;
    vector<ll> a(n);
    for(ll i=0; i<n; i++) cin >> a[i];
    bool ok=true;
    if(a[0]%2 != a[1]%2){
        for(ll i=0; i<n; i+=2) a[i]++;
    }
    for(ll i=0; i<n; i++){
        if(a[i]%2 != a[0]%2) ok=false;
    }
    cout << (ok? "YES" : "NO") << "\n";
}

D. Colorful Stamp

$R$と$B$のみから構成される文字列に注目しました.
$RRBRWRBRB=>RRBR$ と$RBRB$のように$W$で文字列を切った時に,分離された文字列それぞれが,$R$と$B$の二種類の文字で構成されている必要があると考えました.それを探索して判定しました.

void solve(){
    ll n; cin >> n;
    n++;
    string s; cin >> s;
    s+='W';
    bool ok=true;
    bool flag = false;
    ll b=0, r=0;
    for(ll i=0; i<n; i++){
        if(flag){
            if(s[i]=='B' or s[i]=='R'){
                if(s[i]=='B') b++;
                if(s[i]=='R') r++;
            }
            else{
                if(b==0 || r==0) ok=false;
                flag=false;
                b=0;
                r=0;
            }
        }else{
            if(s[i]=='B' or s[i]=='R'){
                flag=true;
                if(s[i]=='B') b++;
                if(s[i]=='R') r++;
            }
        }
    }
    cout << (ok? "YES" : "NO") << "\n";
}
 

F. Eating Candies

左からの累積和と右からの累積和を調べました.左からと右からの累積和を,それぞれmapに(累積和の値,左から何番目か?)を記録させます.
累積和の値が,2つのmap(ここではa,bとして宣言して使っています)に含まれていて,a.second<b.secondの時に要素数を計算して最大値を更新していきました.

void solve(){
    ll n; cin >> n;
    vector<ll> w(n);
    for(ll i=0; i<n; i++) cin >> w[i];
    map<ll, ll> a, b;
    ll sa=0, sb=0;
    for(ll i=0; i<n; i++){
        sa+=w[i];
        a[sa]=i+1;
    }
    for(ll i=n-1; i>=0; i--){
        sb+=w[i];
        b[sb]=i+1;
    }
    ll mx=0;
    for(auto [x, j] : a){
        if(b[x]>0 && j<b[x]){
            chmax(mx, j+n-b[x]+1);
        }
    }
    cout << mx << "\n";
}
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