AtCoder Beginner Contest 247

## [A - Move Right](https://atcoder.jp/contests/abc247/tasks/abc247_a)

O(1)
先頭に'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() {
    string s;
    cin >> s;

    s = ('0' + s);
    s = s.substr(0, 4);
    cout << s << endl;

    return 0;
} 
```

## [B - Unique Nicknames](https://atcoder.jp/contests/abc247/tasks/abc247_b)

O(N)
全探索。
自身の名、性が同一の時だけパターン分けをする。

```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 N;
    cin >> N;
    vector<string> s(N), t(N);
    map<string, int> mp;
    rep(i, N){
        cin >> s[i] >> t[i];
        mp[s[i]]++;
        mp[t[i]]++;
    }

    int ok=1;
    rep(i, N){
        if(s[i]==t[i]) mp[t[i]]-=1;
        if(!(mp[s[i]]==1 || mp[t[i]]==1)) ok=0;
        if(s[i]==t[i]) mp[t[i]]+=1;
    }
    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
} 
```

## [C - 1 2 1 3 1 2 1](https://atcoder.jp/contests/abc247/tasks/abc247_c)

O(2^N)
再帰処理。

```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; }

string S(int n){
    if(n==1) return "1";
    return S(n-1) + " " + to_string(n) + " " + S(n-1);
}

int main() {
    int N;
    cin >> N;

    cout << S(N) << endl;

    return 0;
} 
```

## [D - Cylinder](https://atcoder.jp/contests/abc247/tasks/abc247_d)

O(N)
ラングレス圧縮。

```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<ll,ll>;
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 Q;
    cin >> Q;
    deque<P> dqu;

    rep(i, Q){
        int type;
        cin >> type;

        if(type == 1){
            int x, c;
            cin >> x >> c;
            dqu.push_back(P(x, c));

        }else{
            int c;
            cin >> c;

            ll ans=0;
            while(c){
                P p = dqu.front();
                if(p.second <= c){
                    c -= p.second;
                    ans += p.second * p.first;
                    dqu.pop_front();
                }else{
                    ans += c * p.first;
                    p.second -= c;
                    c = 0;
                    dqu.pop_front();
                    dqu.push_front(P(p.first, p.second));
                }
            }
            cout << ans << endl;
        }
    }


    return 0;
} 
```