<commit_before>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

ll n, m;
ll a[100010];

ll imos[3][100010];

int main () {
  cin >> n >> m;
  for (auto i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll sum = 0;
  for (auto i = 0; i < n-1; ++i) {
    if (a[i+1] > a[i]) {
      sum += a[i+1] - a[i];
    } else {
      sum += m - a[i] + a[i+1];
    }
  }
  cerr << "sum = " << sum << endl;
  fill(&imos[0][0], &imos[0][0]+3*100010, 0);
  for (auto i = 0; i < n-1; ++i) {
    if (a[i] < a[i+1]) {
      imos[0][a[i]+2] += 1;
      imos[0][a[i+1]+1] -= 1;
      imos[2][a[i+1]+1] -= a[i+1] - (a[i] + 1);
    } else {
      imos[0][1] += 1;
      imos[0][a[i+1]+1] -= 1;
      imos[0][a[i]+2] += 1;
      imos[2][1] += m - (a[i] + 1);
      imos[2][a[i+1]+1] -= m - (a[i] + 1) + (a[i+1] + 1);
    }
  }
  for (auto k = 0; k < 3; ++k) {
    for (auto i = 1; i < m+10; ++i) {
      cerr << "imos[" << k << "][" << i << "] = " << imos[k][i] << endl;
    }
  }
  for (auto k = 0; k < 3; ++k) {
    for (auto i = 0; i < m+10; ++i) {
      imos[k][i+1] += imos[k][i];
    }
    if (k < 2) {
      for (auto i = 0; i <= m+10; ++i) {
        imos[k+1][i] += imos[k][i];
      }
    }
  }
  for (auto i = 1; i < m+10; ++i) {
    cerr << "imos[" << 2 << "][" << i << "] = " << imos[2][i] << endl;
  }
  ll ans = 0;
  for (auto i = 1; i <= m; ++i) {
    ans = max(ans, imos[2][i]);
  }
  cout << sum - ans << endl;
}

<commit_msg>tried E.cpp to 'E'<commit_after>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

ll n, m;
ll a[100010];

ll imos[3][100010];

int main () {
  cin >> n >> m;
  for (auto i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll sum = 0;
  for (auto i = 0; i < n-1; ++i) {
    if (a[i+1] > a[i]) {
      sum += a[i+1] - a[i];
    } else {
      sum += m - a[i] + a[i+1];
    }
  }
  cerr << "sum = " << sum << endl;
  fill(&imos[0][0], &imos[0][0]+3*100010, 0);
  for (auto i = 0; i < n-1; ++i) {
    if (a[i] < a[i+1]) {
      imos[0][a[i]+2] += 1;
      imos[0][a[i+1]+1] -= 1;
      imos[2][a[i+1]+1] -= a[i+1] - (a[i] + 1);
    } else {
      imos[0][1] += 1;
      imos[0][a[i+1]+1] -= 1;
      imos[0][a[i]+2] += 1;
      imos[2][1] += m - (a[i] + 1);
      imos[2][a[i+1]+1] -= m - (a[i] + 1) + (a[i+1] + 1);
    }
  }
  for (auto k = 0; k < 3; ++k) {
    for (auto i = 1; i < m+2; ++i) {
      cerr << "imos[" << k << "][" << i << "] = " << imos[k][i] << endl;
    }
  }
  for (auto k = 0; k < 3; ++k) {
    for (auto i = 0; i < m+2; ++i) {
      imos[k][i+1] += imos[k][i];
    }
    if (k < 2) {
      for (auto i = 0; i <= m+2; ++i) {
        imos[k+1][i] += imos[k][i];
      }
    }
  }
  for (auto i = 1; i < m+2; ++i) {
    cerr << "imos[" << 2 << "][" << i << "] = " << imos[2][i] << endl;
  }
  ll ans = 0;
  for (auto i = 1; i <= m; ++i) {
    ans = max(ans, imos[2][i]);
  }
  cout << sum - ans << endl;
}

<|endoftext|>


<commit_before>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int A[200010];
int X[200010];
int Y[200010];

int C[200010];
int S[200010];

int main () {
  cin >> N >> M;
  for (auto i = 0; i < N; ++i) {
    cin >> A[i];
    A[i]--;
  }
  for (auto i = 0; i < M; ++i) {
    cin >> X[i] >> Y[i];
    X[i]--;
    Y[i]--;
  }
  // assert(N <= 200 && M <= 200);
  fill(C, C+200010, 0);
  fill(S, S+N, 0);
  for (auto i = 0; i < N; ++i) {
    C[A[i]]++;
  }
  for (auto i = 0; i < N; ++i) {
    cerr << C[i] << " ";
  }
  cerr << endl;
  for (auto i = 0; i < N; ++i) {
    for (auto j = max(0, i-C[i]+1); j <= i; ++j) {
      S[j]++;
    }
  }
  int ans = 0;
  for (auto i = 0; i < N; ++i) {
    if (S[i] == 0) ++ans;
  }
  for (auto k = 0; k < M; ++k) {
    int before = A[X[k]];
    int after = Y[k];
    S[before - C[before] + 1]--;
    if (S[before - C[before] + 1] == 0) {
      ans++;
    }
    if (S[after - C[after]] == 0) {
      ans--;
    }
    S[after - C[after]]++;
    C[before]--;
    C[after]++;
    A[X[k]] = after;
    for (auto i = 0; i < N; ++i) {
      cerr << S[i] << " ";
    }
    cerr << endl;
    cout << ans << endl;
  }
}
<commit_msg>tried C.cpp to 'C'<commit_after>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int A[200010];
int X[200010];
int Y[200010];

int C[200010];
int S[200010];

int main () {
  cin >> N >> M;
  for (auto i = 0; i < N; ++i) {
    cin >> A[i];
    A[i]--;
  }
  for (auto i = 0; i < M; ++i) {
    cin >> X[i] >> Y[i];
    X[i]--;
    Y[i]--;
  }
  // assert(N <= 200 && M <= 200);
  fill(C, C+200010, 0);
  fill(S, S+N, 0);
  for (auto i = 0; i < N; ++i) {
    C[A[i]]++;
  }
  /*
  for (auto i = 0; i < N; ++i) {
    cerr << C[i] << " ";
  }
  cerr << endl;
  */
  for (auto i = 0; i < N; ++i) {
    for (auto j = max(0, i-C[i]+1); j <= i; ++j) {
      S[j]++;
    }
  }
  int ans = 0;
  for (auto i = 0; i < N; ++i) {
    if (S[i] == 0) ++ans;
  }
  for (auto k = 0; k < M; ++k) {
    int before = A[X[k]];
    int after = Y[k];
    int kezuru = before - C[before] + 1;
    int fuyasu = after - C[after];
    if (kezuru >= 0) {
      S[kezuru]--;
      if (S[kezuru] == 0) {
        ans++;
      }
    }
    if (fuyasu >= 0) {
      if (S[fuyasu] == 0) {
        ans--;
      }
      S[fuyasu]++;
    }
    C[before]--;
    C[after]++;
    A[X[k]] = after;
    /*
    for (auto i = 0; i < N; ++i) {
      cerr << S[i] << " ";
    }
    cerr << endl;
    */
    cout << ans << endl;
  }
}
<|endoftext|>


<commit_before>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;

class SegTree { // index starts at 0.
public:
  int N;
  int* dat;
  static int infty; // (1 << 31) - 1;

  SegTree(){}

  SegTree(int n) {
    N = 1;
    while (N < n) N *= 2;
    dat = new int[2 * N - 1];
    for (auto i = 0; i < 2 * N - 1; ++i) {
      dat[i] = infty;
    }
  }

  ~SegTree() {
    delete[] dat;
  }

  void update(int k, int a) {
    k += N - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1)/2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  pair<int, int> find(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return make_pair(infty, k);
    if (a <= l && r <= b) return make_pair(dat[k], k);
    pair<int, int> vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
    pair<int, int> vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }

  pair<int, int> find(int a, int b) {
    return find(a, b, 0, 0, N);
  }

};

int SegTree::infty = 2147483647; // (1 << 31) - 1;

typedef pair<int, int> unit;

SegTree st[2];

vector<unit> ans;

void solve(int l, int r) {
  if (l == r) return;
  int take = 0;
  if (l%2 == 1) {
    take = 1;
  }
  pair<int, int> p = st[take].find(l/2, r/2);
  int f = p.first;
  int indf = p.second * 2 + take;
  take++;
  take %= 2;
  pair<int, int> q = st[take].find((indf+1)/2, r/2);
  int s = q.first;
  int inds = q.second * 2 + take;
  ans.push_back(unit(f, s));
  solve(l, indf);
  solve(indf+1, inds);
  solve(inds+1, r);
}

int main () {
  int N;
  int p[200010];
  cin >> N;
  for (auto i = 0; i < N; ++i) {
    cin >> p[i];
  }
  st[0] = SegTree(N/2);
  st[1] = SegTree(N/2);
  for (auto i = 0; i < N; ++i) {
    if (i % 2 == 0) {
      st[i%2].update(i/2, p[i]);
    }
  }
  solve(0, N);
  sort(ans.begin(), ans.end());
  for (auto it = ans.begin(); it != ans.end(); ++it) {
    auto x = *it;
    cout << x.first << " " << x.second;
    if (it+1 == ans.end()) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}
<commit_msg>tried E.cpp to 'E'<commit_after>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;

class SegTree { // index starts at 0.
public:
  int N;
  int* dat;
  static int infty; // (1 << 31) - 1;

  SegTree(){}

  SegTree(int n) {
    N = 1;
    while (N < n) N *= 2;
    dat = new int[2 * N - 1];
    for (auto i = 0; i < 2 * N - 1; ++i) {
      dat[i] = infty;
    }
  }

  ~SegTree() {
    delete[] dat;
  }

  void update(int k, int a) {
    k += N - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1)/2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  pair<int, int> find(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return make_pair(infty, k);
    if (a <= l && r <= b) return make_pair(dat[k], k);
    pair<int, int> vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
    pair<int, int> vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }

  pair<int, int> find(int a, int b) {
    return find(a, b, 0, 0, N);
  }

};

int SegTree::infty = 2147483647; // (1 << 31) - 1;

typedef pair<int, int> unit;

SegTree st[2];

vector<unit> ans;

void solve(int l, int r) {
  if (l == r) return;
  int take = 0;
  if (l%2 == 1) {
    take = 1;
  }
  pair<int, int> p = st[take].find(l/2, r/2);
  int f = p.first;
  int indf = p.second * 2 + take;
  take++;
  take %= 2;
  pair<int, int> q = st[take].find((indf+1)/2, r/2);
  int s = q.first;
  int inds = q.second * 2 + take;
  ans.push_back(unit(f, s));
  solve(l, indf);
  solve(indf+1, inds);
  solve(inds+1, r);
}

int main () {
  int N;
  int p[200010];
  cin >> N;
  for (auto i = 0; i < N; ++i) {
    cin >> p[i];
  }
  st[0] = SegTree(N/2);
  st[1] = SegTree(N/2);
  for (auto i = 0; i < N; ++i) {
    if (i % 2 == 0) {
      st[i%2].update(i/2, p[i]);
    }
  }
  cerr << "aaa" << endl;
  solve(0, N);
  sort(ans.begin(), ans.end());
  for (auto it = ans.begin(); it != ans.end(); ++it) {
    auto x = *it;
    cout << x.first << " " << x.second;
    if (it+1 == ans.end()) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}
<|endoftext|>


<commit_before>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
typedef tuple<int, int> takahashi;
vector<takahashi> V;

int main () {
  cin >> N >> M;
  for (auto i = 0; i < N; ++i) {
    int L, R;
    cin >> L >> R;
    V.push_back(takahashi(L, R));
  }
  sort(V.begin(), V.end());
  priority_queue<int, vector<int>, greater<int> > Q;
  vector<int> W;
  int ind = 1;
  for (auto x : V) {
    if (ind <= get<0>(x)) {
      ++ind;
    } else {
      Q.push(get<1>(x));
      W.push_back(Q.top());
      Q.pop();
    }
  }
  for (auto it = W.begin(); it != W.end(); ++it) {
    *it = M+1 - *it;
  }
  ind = 1;
  int ans = 0;
  for (auto x : W) {
    if (ind <= x) {
      ++ind;
    } else {
      ++ans;
    }
  }
  cout << ans << endl;
}

<commit_msg>tried F.cpp to 'F'<commit_after>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
typedef tuple<int, int> takahashi;
vector<takahashi> V;

int main () {
  cin >> N >> M;
  for (auto i = 0; i < N; ++i) {
    int L, R;
    cin >> L >> R;
    V.push_back(takahashi(L, R));
  }
  sort(V.begin(), V.end());
  priority_queue<int, vector<int>, greater<int> > Q;
  vector<int> W;
  int ind = 1;
  for (auto x : V) {
    if (ind <= get<0>(x)) {
      ++ind;
    } else {
      Q.push(get<1>(x));
      W.push_back(Q.top());
      Q.pop();
    }
  }
  for (auto it = W.begin(); it != W.end(); ++it) {
    *it = M+1 - *it;
  }
  sort(W.begin(), W.end());
  ind = 1;
  int ans = 0;
  for (auto x : W) {
    if (ind <= x) {
      ++ind;
    } else {
      ++ans;
    }
  }
  cout << ans << endl;
}

<|endoftext|>


<commit_before>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int main () {
  int N;
  cin >> N;
  int a[100010];
  for (auto i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int cnt[100010];
  fill(cnt, cnt+100010, 0);
  int ans = 0;
  for (auto i = 0; i < N; ++i) {
    if (a[i] > N) {
      ans++;
    } else {
      cnt[a[i]]++;
    }
  }
  for (auto i = 1; i <= N; ++i) {
    if (cnt[i] > i) {
      ans += cnt[i] - i;
    }
  }
  cout << ans << endl;
}
<commit_msg>submit C.cpp to 'C - Good Sequence' (arc087) [C++14 (GCC 5.4.1)]<commit_after>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int main () {
  int N;
  cin >> N;
  int a[100010];
  for (auto i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int cnt[100010];
  fill(cnt, cnt+100010, 0);
  int ans = 0;
  for (auto i = 0; i < N; ++i) {
    if (a[i] > N) {
      ans++;
    } else {
      cnt[a[i]]++;
    }
  }
  for (auto i = 1; i <= N; ++i) {
    if (cnt[i] >= i) {
      ans += cnt[i] - i;
    } else {
      ans += cnt[i];
    }
  }
  cout << ans << endl;
}
<|endoftext|>


<commit_before>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-5-12 21:08:34
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

const int UFSIZE = 100010;
int union_find[UFSIZE];

void init()
{
  for (auto i = 0; i < UFSIZE; i++)
  {
    union_find[i] = i;
  }
}

int root(int a)
{
  if (a == union_find[a])
    return a;
  return (union_find[a] = root(union_find[a]));
}

bool issame(int a, int b)
{
  return root(a) == root(b);
}

void unite(int a, int b)
{
  union_find[root(a)] = root(b);
}

bool isroot(int a)
{
  return root(a) == a;
}

int N, M;
int p[100010];
int x[100010];
int y[100010];

int main()
{
  init();
  cin >> N >> M;
  for (auto i = 0; i < N; i++)
  {
    cin >> p[i];
    p[i]--;
  }
  for (auto i = 0; i < M; i++)
  {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  for (auto i = 0; i < M; i++)
  {
    unite(p[x[i]], p[y[i]]);
  }
  int ans = 0;
  for (auto i = 0; i < M; i++)
  {
    if (issame(i, p[i]))
    {
      ++ans;
    }
  }
  cout << ans << endl;
}<commit_msg>submit D.cpp to 'D - Equals' (arc097) [C++14 (GCC 5.4.1)]<commit_after>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-5-12 21:08:34
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

const int UFSIZE = 100010;
int union_find[UFSIZE];

void init()
{
  for (auto i = 0; i < UFSIZE; i++)
  {
    union_find[i] = i;
  }
}

int root(int a)
{
  if (a == union_find[a])
    return a;
  return (union_find[a] = root(union_find[a]));
}

bool issame(int a, int b)
{
  return root(a) == root(b);
}

void unite(int a, int b)
{
  union_find[root(a)] = root(b);
}

bool isroot(int a)
{
  return root(a) == a;
}

int N, M;
int p[100010];
int x[100010];
int y[100010];

int main()
{
  init();
  cin >> N >> M;
  for (auto i = 0; i < N; i++)
  {
    cin >> p[i];
    p[i]--;
  }
  for (auto i = 0; i < M; i++)
  {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  for (auto i = 0; i < M; i++)
  {
    unite(p[x[i]], p[y[i]]);
  }
  int ans = 0;
  for (auto i = 0; i < N; i++)
  {
    if (issame(i, p[i]))
    {
      ++ans;
    }
  }
  cout << ans << endl;
}<|endoftext|>


<commit_before>/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-7-2 20:45:55
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
const ll MOD = 1000000007;
const ll M = 250010;
random_device rd;
mt19937 mt(rd());
typedef tuple<ll, ll> D;

int N;
string S;
map<ll, int> ans;
ll X;
ll X_inv;
ll C;
ll a[500100];
D g[250010];
map<ll, ll> G;

ll power2(int i)
{
  if (i == 0)
  {
    return 1;
  }
  else if (i % 2 == 0)
  {
    ll half = power2(i / 2);
    return (half * half) % MOD;
  }
  else
  {
    return (power2(i - 1) * X) % MOD;
  }
}

ll power(int i)
{
  if (i == 0)
  {
    return 1;
  }
  else if (i < 0)
  {
    return power2(MOD + i - 1);
  }
  return power2(i);
}

ll calc_hash()
{
  fill(a, a + 500100, 0);
  int P = M;
  for (auto e : S)
  {
    if (e == '+')
    {
      a[P]++;
    }
    else if (e == '-')
    {
      a[P]--;
    }
    else if (e == '>')
    {
      P++;
    }
    else
    {
      P--;
    }
  }
  ll res = 0;
  for (auto i = 0; i < 500100; i++)
  {
    res += (((MOD + a[i]) % MOD) * power(i - M)) % MOD;
    res %= MOD;
  }
  return res;
}

void calc_g()
{
  g[N] = D(1, 0);
  for (auto i = N - 1; i >= 0; i--)
  {
    ll A_prime = get<0>(g[i + 1]);
    ll B_prime = get<1>(g[i + 1]);
    ll A, B;
    if (S[i] == '+')
    {
      A = 1, B = -1;
    }
    else if (S[i] == '-')
    {
      A = 1, B = -1;
    }
    else if (S[i] == '>')
    {
      A = X_inv, B = 0;
    }
    else
    {
      A = X, B = 0;
    }
    g[i] = D((A_prime * A) % MOD,
             ((A_prime * B) % MOD + B_prime) % MOD);
  }
}

ll count_ans()
{
  ll res = 0;
  G.clear();
  for (int i = N; i >= 1; i--)
  {
    ll J = get<1>(g[i]);
    if (G.find(J) == G.end())
    {
      G[J] = 1;
    }
    else
    {
      G[J]++;
    }
    ll I = ((get<0>(g[i - 1]) * C) % MOD + get<1>(g[i - 1])) % MOD;
    if (G.find(I) != G.end())
    {
      res += G[I];
    }
  }
  return res;
}

void solve()
{
  X = abs((ll)mt());
  X_inv = power(-1);
  C = calc_hash();
  calc_g();
  ll res = count_ans();
  if (ans.find(res) == ans.end())
  {
    ans[res] = 1;
  }
  else
  {
    ans[res]++;
  }
}

int main()
{
  cin >> N >> S;
  for (auto i = 0; i < 6; i++)
  {
    solve();
  }
  ll res = 0;
  int maxi = 0;
  for (auto e : ans)
  {
    ll a = e.first;
    int cnt = e.second;
    if (cnt > maxi)
    {
      cnt = maxi;
      res = a;
    }
  }
  cout << res << endl;
}<commit_msg>tried F.cpp to 'F'<commit_after>/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-7-2 20:45:55
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
const ll MOD = 1000000007;
const ll M = 250010;
random_device rd;
mt19937 mt(rd());
typedef tuple<ll, ll> D;

int N;
string S;
map<ll, int> ans;
ll X;
ll X_inv;
ll C;
ll a[500100];
D g[250010];
map<ll, ll> G;

ll power2(int i)
{
  if (i == 0)
  {
    return 1;
  }
  else if (i % 2 == 0)
  {
    ll half = power2(i / 2);
    return (half * half) % MOD;
  }
  else
  {
    return (power2(i - 1) * X) % MOD;
  }
}

ll power(int i)
{
  if (i == 0)
  {
    return 1;
  }
  else if (i < 0)
  {
    return power2(MOD + i - 1);
  }
  return power2(i);
}

ll calc_hash()
{
  fill(a, a + 500100, 0);
  int P = M;
  for (auto e : S)
  {
    if (e == '+')
    {
      a[P]++;
    }
    else if (e == '-')
    {
      a[P]--;
    }
    else if (e == '>')
    {
      P++;
    }
    else
    {
      P--;
    }
  }
  ll res = 0;
  for (auto i = 0; i < 500100; i++)
  {
    res += (((MOD + a[i]) % MOD) * power(i - M)) % MOD;
    res %= MOD;
  }
  return res;
}

void calc_g()
{
  g[N] = D(1, 0);
  for (auto i = N - 1; i >= 0; i--)
  {
    ll A_prime = get<0>(g[i + 1]);
    ll B_prime = get<1>(g[i + 1]);
    ll A, B;
    if (S[i] == '+')
    {
      A = 1, B = -1;
    }
    else if (S[i] == '-')
    {
      A = 1, B = -1;
    }
    else if (S[i] == '>')
    {
      A = X_inv, B = 0;
    }
    else
    {
      A = X, B = 0;
    }
    g[i] = D((A_prime * A) % MOD,
             ((A_prime * B) % MOD + B_prime) % MOD);
  }
}

ll count_ans()
{
  ll res = 0;
  G.clear();
  for (int i = N; i >= 1; i--)
  {
    ll J = get<1>(g[i]);
    if (G.find(J) == G.end())
    {
      G[J] = 1;
    }
    else
    {
      G[J]++;
    }
    ll I = ((get<0>(g[i - 1]) * C) % MOD + get<1>(g[i - 1])) % MOD;
    if (G.find(I) != G.end())
    {
      cerr << "i = " << i - 1 << ", cnt of j = " << G[I] << endl;
      res += G[I];
    }
  }
  return res;
}

void solve()
{
  X = abs((ll)mt());
  X_inv = power(-1);
  C = calc_hash();
  calc_g();
  ll res = count_ans();
  if (ans.find(res) == ans.end())
  {
    ans[res] = 1;
  }
  else
  {
    ans[res]++;
  }
}

int main()
{
  cin >> N >> S;
  for (auto i = 0; i < 6; i++)
  {
    solve();
  }
  ll res = 0;
  int maxi = 0;
  for (auto e : ans)
  {
    ll a = e.first;
    int cnt = e.second;
    if (cnt > maxi)
    {
      cnt = maxi;
      res = a;
    }
  }
  cout << res << endl;
}<|endoftext|>


<commit_before>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-7-1 22:00:11
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

typedef tuple<int, int> D;

int N;
int A[100010];
int DP[100010];
int DP2[100010];

set<D> S[100010];

void make_S(int i)
{
  if (!S[i].empty())
  {
    return;
  }
  S[i].insert(D(-A[0], 0));
  S[i].insert(D(-A[i], i));
  for (auto j = 0; j < N; j++)
  {
    int mask = (1 << N) - 1 - (1 << j);
    int new_i = mask & i;
    if (new_i == i || new_i == 0)
    {
      continue;
    }
    make_S(new_i);
    auto it = S[new_i].begin();
    S[i].insert(*it);
    it++;
    S[i].insert(*it);
  }
}

int main()
{
  cin >> N;
  for (auto i = 0; i < N; i++)
  {
    cin >> A[i];
  }
  for (auto i = 0; i < (1 << N); i++)
  {
    make_S(i);
    auto it = S[i].begin();
    DP2[i] = -(get<0>(*it));
    it++;
    DP2[i] += -(get<0>(*it));
  }
  // flush
  DP[0] = 0;
  for (auto i = 1; i < (1 << N); i++)
  {
    DP[i] = max(DP[i - 1], DP2[i]);
    cout << DP[i] << endl;
  }
}<commit_msg>tried E.cpp to 'E'<commit_after>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-7-1 22:00:11
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

typedef tuple<int, int> D;

int N;
int A[100010];
int DP[100010];
int DP2[100010];

set<D> S[100010];

void make_S(int i)
{
  if (!S[i].empty())
  {
    return;
  }
  S[i].insert(D(-A[0], 0));
  S[i].insert(D(-A[i], i));
  for (auto j = 0; j < N; j++)
  {
    int mask = (1 << N) - 1 - (1 << j);
    int new_i = mask & i;
    if (new_i == i || new_i == 0)
    {
      continue;
    }
    make_S(new_i);
    auto it = S[new_i].begin();
    S[i].insert(*it);
    it++;
    S[i].insert(*it);
  }
}

int main()
{
  cin >> N;
  for (auto i = 0; i < N; i++)
  {
    cin >> A[i];
  }
  for (auto i = 0; i < (1 << N); i++)
  {
    make_S(i);
    auto it = S[i].begin();
    cerr << "i = " << i << endl;
    cerr << "(" << get<0>(*it) << ", " << get<1>(*it) << ")" << endl;
    DP2[i] = -(get<0>(*it));
    it++;
    cerr << "(" << get<0>(*it) << ", " << get<1>(*it) << ")" << endl;
    DP2[i] += -(get<0>(*it));
  }
  // flush
  DP[0] = 0;
  for (auto i = 1; i < (1 << N); i++)
  {
    DP[i] = max(DP[i - 1], DP2[i]);
    cout << DP[i] << endl;
  }
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2/12/2020, 4:45:12 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint &operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint &operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

class Solve
{
  bool all_red;
  int N, M;
  string S;
  int X;

public:
  Solve(int N, int M, string S) : all_red{false}, N{N}, M{M}, S{S}, X{max_length()} {}

  void flush()
  {
    if (all_red)
    {
      cout << solve_all_red() << endl;
    }
    else
    {
      cout << solve_normal() << endl;
    }
  };

private:
  mint solve_all_red()
  {
    vector<vector<mint>> dp(N + 1, vector<mint>(2, 0));
    dp[0][0] = 1;
    for (auto i = 1; i <= N; ++i)
    {
      dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
      dp[i][1] = dp[i - 1][0];
    }
    return 3 * dp[N - 2][0] + dp[N - 2][1];
  }

  mint solve_normal()
  {
    if (N % 2 == 1)
    {
      return 0;
    }
    int N{Solve::N / 2}, X{Solve::X / 2};
    vector<mint> DP(N + 1, 0);
    DP[0] = 1;
    for (auto i = 1; i <= N; ++i)
    {
      DP[i] = 2 * DP[i - 1];
      if (i - 2 - X >= 0)
      {
        DP[i] -= DP[i - 2 - X];
      }
    }
    mint ans{0};
    for (auto K = 0; K <= X; ++K)
    {
      if (N - K - 2 >= 0)
      {
        ans += (K + 1) * DP[N - K - 2];
      }
    }
    return ans;
  }

  int max_length()
  {
    if (S[0] == 'B')
    {
      for (auto &x : S)
      {
        if (x == 'R')
        {
          x = 'B';
        }
        else
        {
          x = 'R';
        }
      }
    }
    vector<int> V;
    int now{0};
    for (auto i = 0; i < M; ++i)
    {
      if (S[i] == 'R')
      {
        ++now;
      }
      else if (now > 0)
      {
        V.push_back(now);
        now = 0;
      }
    }
    if (V.empty())
    {
      all_red = true;
      return N;
    }
    if (V[0] % 2 == 0)
    {
      ++V[0];
    }
    for (auto it = V.begin() + 1; it != V.end(); ++it)
    {
      if (*it % 2 == 0)
      {
        *it = N;
      }
    }
    return *min_element(V.begin(), V.end());
  }
};

int main()
{
  int N, M;
  string S;
  cin >> N >> M >> S;
  Solve solve(N, M, S);
  solve.flush();
}
<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2/12/2020, 4:45:12 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint &operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint &operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

class Solve
{
  bool all_red;
  int N, M;
  string S;
  int X;

public:
  Solve(int N, int M, string S) : all_red{false}, N{N}, M{M}, S{S}, X{max_length()} {}

  void flush()
  {
    #if DEBUG == 1
    cerr << "X = " << X << endl;
    #endif
    if (all_red)
    {
      cout << solve_all_red() << endl;
    }
    else
    {
      cout << solve_normal() << endl;
    }
  };

private:
  mint solve_all_red()
  {
    vector<vector<mint>> dp(N + 1, vector<mint>(2, 0));
    dp[0][0] = 1;
    for (auto i = 1; i <= N; ++i)
    {
      dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
      dp[i][1] = dp[i - 1][0];
    }
    return 3 * dp[N - 2][0] + dp[N - 2][1];
  }

  mint solve_normal()
  {
    if (N % 2 == 1)
    {
      return 0;
    }
    int N{Solve::N / 2}, X{Solve::X / 2};
    vector<mint> DP(N + 1, 0);
    DP[0] = 1;
    for (auto i = 1; i <= N; ++i)
    {
      DP[i] = 2 * DP[i - 1];
      if (i - 2 - X >= 0)
      {
        DP[i] -= DP[i - 2 - X];
      }
    }
    mint ans{0};
    for (auto K = 0; K <= X; ++K)
    {
      if (N - K - 2 >= 0)
      {
        ans += (K + 1) * DP[N - K - 2];
      }
    }
    return ans;
  }

  int max_length()
  {
    if (S[0] == 'B')
    {
      for (auto &x : S)
      {
        if (x == 'R')
        {
          x = 'B';
        }
        else
        {
          x = 'R';
        }
      }
    }
    vector<int> V;
    int now{0};
    for (auto i = 0; i < M; ++i)
    {
      if (S[i] == 'R')
      {
        ++now;
      }
      else if (now > 0)
      {
        V.push_back(now);
        now = 0;
      }
    }
    if (V.empty())
    {
      all_red = true;
      return N;
    }
    if (V[0] % 2 == 0)
    {
      ++V[0];
    }
    for (auto it = V.begin() + 1; it != V.end(); ++it)
    {
      if (*it % 2 == 0)
      {
        *it = N;
      }
    }
    return *min_element(V.begin(), V.end());
  }
};

int main()
{
  int N, M;
  string S;
  cin >> N >> M >> S;
  Solve solve(N, M, S);
  solve.flush();
}
<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-26 21:13:56
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <functional>
#include <random> // auto rd = bind(uniform_int_distribution<int>(0, 9), mt19937(10101010));
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;
int N, K;
int X[100];

int _solve(vector<int> V, int k)
{
  int ans = 0;
  for (auto i = 0; i <= min((int)V.size(), k); i++)
  {
    vector<int> W;
    for (auto j = 0; j < i; j++)
    {
      W.push_back(V[j]);
    }
    sort(W.begin(), W.end());
    int n = k - i;
    int ind = 0;
    for (auto j = 0; j < min(n, (int)W.size()); j++)
    {
      if (W[j] < 0)
      {
        ind++;
      }
    }
    int t = 0;
    for (auto j = ind; j < (int)W.size(); j++)
    {
      t += W[j];
    }
    ans = max(ans, t);
  }
  return ans;
}

int solve(vector<int> V, vector<int> W)
{
  int ans = 0;
  for (auto i = 0; i <= K; i++)
  {
    ans = max(ans, _solve(V, i) + _solve(W, K - i));
  }
  return ans;
}

int main()
{
  cin >> N >> K;
  for (auto i = 0; i < N; i++)
  {
    cin >> X[i];
  }
  int ans = 0;
  for (auto i = 0; i <= K; i++)
  {
    vector<int> V, W;
    for (auto j = 0; j < i; j++)
    {
      V.push_back(X[j]);
    }
    for (auto j = K - 1; j >= i; j--)
    {
      W.push_back(X[j]);
    }
#if DEBUG == 1
    cerr << "V :";
    for (auto x : V)
    {
      cerr << x << " ";
    }
    cerr << endl;
    cerr << "W :";
    for (auto x : W)
    {
      cerr << x << " ";
    }
    cerr << endl;
#endif
    ans = max(ans, solve(V, W));
  }
  cout << ans << endl;
}<commit_msg>submit D.cpp to 'D - equeue' (abc128) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-26 21:13:56
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <functional>
#include <random> // auto rd = bind(uniform_int_distribution<int>(0, 9), mt19937(10101010));
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;
int N, K;
int X[100];

int _solve(vector<int> V, int k)
{
  int ans = 0;
  for (auto i = 0; i <= min((int)V.size(), k); i++)
  {
    vector<int> W;
    for (auto j = 0; j < i; j++)
    {
      W.push_back(V[j]);
    }
    sort(W.begin(), W.end());
    int n = k - i;
    int ind = 0;
    for (auto j = 0; j < min(n, (int)W.size()); j++)
    {
      if (W[j] < 0)
      {
        ind++;
      }
    }
    int t = 0;
    for (auto j = ind; j < (int)W.size(); j++)
    {
      t += W[j];
    }
    ans = max(ans, t);
  }
  return ans;
}

int solve(vector<int> V, vector<int> W)
{
  int ans = 0;
  for (auto i = 0; i <= K; i++)
  {
    ans = max(ans, _solve(V, i) + _solve(W, K - i));
  }
  return ans;
}

int main()
{
  cin >> N >> K;
  for (auto i = 0; i < N; i++)
  {
    cin >> X[i];
  }
  int ans = 0;
  for (auto i = 0; i <= N; i++)
  {
    vector<int> V, W;
    for (auto j = 0; j < i; j++)
    {
      V.push_back(X[j]);
    }
    for (auto j = N - 1; j >= i; j--)
    {
      W.push_back(X[j]);
    }
#if DEBUG == 1
    cerr << "V :";
    for (auto x : V)
    {
      cerr << x << " ";
    }
    cerr << endl;
    cerr << "W :";
    for (auto x : W)
    {
      cerr << x << " ";
    }
    cerr << endl;
#endif
    ans = max(ans, solve(V, W));
  }
  cout << ans << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : A.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-6-2 20:42:31
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

// const ll MOD = 1000000007;

string S;
int A, B, C, D, N;

bool solve(int X, int Y)
{
  for (auto i = X; i < Y; i++)
  {
    if (S[i] == '#' && S[i + 1] == '#')
    {
      return false;
    }
  }
  return true;
}

bool solve2(int X, int Y, int Z, int W)
{
  bool ok = false;
  for (auto i = Z; i < W - 1; i++)
  {
    if ((i == 0 || S[i - 1] == '.') && S[i] == '.' && S[i + 1] == '.')
    {
      S[i] = '#';
      ok = true;
      break;
    }
  }
  if (!ok)
  {
    return solve(X, Y);
  }
  else
  {
    S[W] = '#';
    return solve(X, Y);
  }
}

int main()
{
  cin >> N >> A >> B >> C >> D >> S;
  A--;
  B--;
  C--;
  D--;
  if (C < B)
  {
    if (solve(A, C) && solve(B, D))
    {
      Yes();
    }
    No();
  }
  if (solve(B, D) && solve2(A, C, B, D))
  {
    Yes();
  }
  No();
}<commit_msg>submit A.cpp to 'A - Kenken Race' (agc034) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1

/**
 * File    : A.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-6-2 20:42:31
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

// const ll MOD = 1000000007;

string S;
int A, B, C, D, N;

bool solve(int X, int Y)
{
  for (auto i = X; i < Y; i++)
  {
    if (S[i] == '#' && S[i + 1] == '#')
    {
      return false;
    }
  }
  return true;
}

bool solve2(int X, int Y, int Z, int W)
{
  bool ok = false;
  for (auto i = Z; i < W - 1; i++)
  {
    if ((i == 0 || S[i - 1] == '.') && S[i] == '.' && S[i + 1] == '.')
    {
      S[i] = '#';
      ok = true;
      break;
    }
  }
  if (ok)
  {
    return solve(X, Y);
  }
  else
  {
    S[W] = '#';
    return solve(X, Y);
  }
}

int main()
{
  cin >> N >> A >> B >> C >> D >> S;
  A--;
  B--;
  C--;
  D--;
  if (C < B)
  {
    if (solve(A, C) && solve(B, D))
    {
      Yes();
    }
    No();
  }
  if (solve(B, D) && solve2(A, C, B, D))
  {
    Yes();
  }
  No();
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/9/7 22:14:56
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

using info = tuple<int, int>;

int main()
{
  ll N;
  cin >> N;
  ll const M{1LL << N};
  vector<int> A(M);
  for (auto i = 0LL; i < M; i++)
  {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  vector<bool> B(M, false);
  vector<int> S(M);
  B[0] = true;
  S[0] = A[0];
  for (auto i = 0LL; i < N; i++)
  {
    ll sz{1LL << i};
    ll k{0};
    for (auto j = 0; j < M; j++)
    {
      if (!B[j] && k < sz && A[j] < S[k])
      {
        S[sz + k] = A[j];
        B[j] = true;
        ++k;
      }
      if (k < sz)
      {
        No();
      }
      sort(S.begin(), S.begin() + sz * 2);
      reverse(S.begin(), S.begin() + sz * 2);
    }
  }
  Yes();
}
<commit_msg>submit F.cpp to 'F - Many Slimes' (abc140) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/9/7 22:14:56
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

using info = tuple<int, int>;

int main()
{
  ll N;
  cin >> N;
  ll const M{1LL << N};
  vector<int> A(M);
  for (auto i = 0LL; i < M; i++)
  {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  vector<bool> B(M, false);
  vector<int> S(M);
  B[0] = true;
  S[0] = A[0];
  for (auto i = 0LL; i < N; i++)
  {
    ll const T{1LL << i};
    ll k{0};
    for (auto j = 0; j < M; j++)
    {
      if (!B[j] && k < T && A[j] < S[k])
      {
        S[T + k] = A[j];
        B[j] = true;
        ++k;
      }
    }
    if (k < T)
    {
      No();
    }
    sort(S.begin(), S.begin() + T * 2);
    reverse(S.begin(), S.begin() + T * 2);
  }
  Yes();
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2/7/2020, 2:49:26 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint &operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint &operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

template <typename T>
void flush_cerr(vector<T> const &V)
{
#if DEBUG == 1
  int now{0};
  for (auto it = V.begin(); it != V.end(); ++it)
  {
    cerr << "V[" << now++ << "] = " << *it << endl;
  }
#endif
}

int main()
{
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (auto i = 0; i < N; ++i)
  {
    cin >> A[i];
    --A[i];
  }
  vector<ll> H(N);
  for (auto i = 0; i < N; ++i)
  {
    H[A[i]]++;
  }
  flush_cerr(H);
  vector<ll> C(N + 1);
  for (auto i = 0; i < N; ++i)
  {
    C[H[i]]++;
  }
  flush_cerr(C);
  vector<ll> S(N + 1);
  S[0] = 0;
  for (auto x = 1LL; x < N; ++x)
  {
    S[x + 1] = S[x] + (x + 1) * C[x + 1];
  }
  vector<ll> T(N + 1);
  T[0] = accumulate(C.begin() + 1, C.end(), 0LL);
  for (auto x = 1LL; x < N; ++x)
  {
    T[x + 1] = T[x] - C[x + 1];
  }
  auto f = [&](ll x) {
    return (S[x] + x * T[x]) / x;
  };
  vector<ll> F(N + 1);
  for (auto i = 1; i <= N; ++i)
  {
    F[i] = f(i);
  }
  ll ok{0}, ng{N + 1};
  for (auto k = 1; k <= N; ++k)
  {
    while (abs(ok - ng) > 1)
    {
      ll t{(ok + ng) / 2};
      if (F[t] >= k)
      {
        ok = k;
      }
      else
      {
        ng = k;
      }
    }
    cout << ok << endl;
  }
}
<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2/7/2020, 2:49:26 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint &operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint &operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

template <typename T>
void flush_cerr(vector<T> const &V, string const &name)
{
#if DEBUG == 1
  int now{0};
  for (auto it = V.begin(); it != V.end(); ++it)
  {
    cerr << "V[" << now++ << "] = " << *it << endl;
  }
#endif
}

int main()
{
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (auto i = 0; i < N; ++i)
  {
    cin >> A[i];
    --A[i];
  }
  vector<ll> H(N);
  for (auto i = 0; i < N; ++i)
  {
    H[A[i]]++;
  }
  flush_cerr(H, "H");
  vector<ll> C(N + 1);
  for (auto i = 0; i < N; ++i)
  {
    C[H[i]]++;
  }
  flush_cerr(C, "C");
  vector<ll> S(N + 1);
  S[0] = 0;
  for (auto x = 1LL; x < N; ++x)
  {
    S[x + 1] = S[x] + (x + 1) * C[x + 1];
  }
  vector<ll> T(N + 1);
  T[0] = accumulate(C.begin() + 1, C.end(), 0LL);
  for (auto x = 1LL; x < N; ++x)
  {
    T[x + 1] = T[x] - C[x + 1];
  }
  auto f = [&](ll x) {
    return (S[x] + x * T[x]) / x;
  };
  vector<ll> F(N + 1);
  for (auto i = 1; i <= N; ++i)
  {
    F[i] = f(i);
  }
  ll ok{0}, ng{N + 1};
  for (auto k = 1; k <= N; ++k)
  {
    while (abs(ok - ng) > 1)
    {
      ll t{(ok + ng) / 2};
      if (F[t] >= k)
      {
        ok = k;
      }
      else
      {
        ng = k;
      }
    }
    cout << ok << endl;
  }
}
<|endoftext|>