malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 100000;   int p[N], u[N];   int main() {     int n, k;     cin >> n >> k;     for (int i = 1; i <= k; i++) {       int x;       cin >> x;       if (p[x] == 0) p[x] = i;       u[x] = i;     }     long long rez = 0;     for (int i = 1; i <= n; i++) {       if (p[i] == 0) {         if (i == 1 || i == n)           rez += 2;         else           rez += 3;       } else {         if (i < n && (p[i] > u[i + 1])) rez++;         if (i > 1 && (p[i] > u[i - 1])) rez++;       }     }     cout << rez;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e9 + 7;   const long long INF = 1LL << 60;   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;   }   struct UnionFind {     vector<long long> par, w;     UnionFind(long long n) : par(n, -1), w(n, 0) {}     void init(long long n) {       par.assign(n, -1);       w.assign(n, 0);     }     long long root(long long x) {       if (par[x] < 0)         return x;       else         return par[x] = root(par[x]);     }     bool issame(long long x, long long y) { return root(x) == root(y); }     bool merge(long long x, long long y) {       x = root(x);       y = root(y);       if (x == y) {         ++w[x];         return false;       }       if (par[x] > par[y]) swap(x, y);       par[x] += par[y];       par[y] = x;       w[x] += w[y];       ++w[x];       return true;     }     long long size(long long x) { return -par[root(x)]; }     long long wei(long long x) { return w[root(x)]; }   };   void solve() {     long long n, m, q;     cin >> n >> m >> q;     UnionFind uf(n + m);     long long cnt = n + m;     for (long long i = 0; i < q; ++i) {       long long r, c;       cin >> r >> c;       --r, --c;       if (!uf.issame(r, c + n)) {         uf.merge(r, c + n);         --cnt;       }     }     cout << cnt - 1 <<   n ;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct union_find {     vector<int> uf;     vector<int> num_segments;     vector<bool> used;     union_find(int n) {       uf.assign(n, -1);       num_segments.assign(n, 0);       used.assign(n, false);       for (int i = 0; i < n; ++i) {         if (i % 2 == 0) num_segments[i] = 1;       }     }     int find(int i) {       if (uf[i] < 0)         return i;       else         return uf[i] = find(uf[i]);     }     bool merge(int a, int b) {       a = find(a);       b = find(b);       if (a == b) return false;       if (uf[a] <= uf[b]) {         uf[b] = a;         if (uf[a] == uf[b]) {           uf[a]--;         }         num_segments[a] =             min(num_segments[a] + num_segments[b], 2 * (int)uf.size());         used[a] = used[a] || used[b];       } else if (uf[b] < uf[a]) {         uf[a] = b;         num_segments[b] =             min(num_segments[a] + num_segments[b], 2 * (int)uf.size());         used[b] = used[a] || used[b];       }       return true;     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int N, K;     cin >> N >> K;     string s;     cin >> s;     vector<vector<int>> groups(K);     vector<vector<int>> groups_for_bit(N);     for (int i = 0; i < K; ++i) {       int k;       cin >> k;       groups[i].resize(k);       for (int j = 0; j < k; ++j) {         cin >> groups[i][j];         groups[i][j]--;         groups_for_bit[groups[i][j]].push_back(i);       }     }     auto inverse = [](int x) { return x ^ 1; };     union_find uf(2 * K);     int num_segments = 0;     auto unset = [&](int i) {       if (uf.used[i]) {         uf.used[i] = false;         num_segments -= uf.num_segments[i];         ;       }     };     auto set = [&](int i) {       assert(!uf.used[i]);       uf.used[i] = true;       num_segments += uf.num_segments[i];       ;     };     for (int i = 0; i < N; ++i) {       ;       int g = -1;       if (groups_for_bit[i].size() == 0) {         ;       } else if (groups_for_bit[i].size() == 1) {         g = uf.find(2 * groups_for_bit[i][0]);         int ig = uf.find(inverse(g));         ;         unset(g);         unset(ig);         if (s[i] ==  0 ) {           uf.num_segments[ig] = N * 3;         } else {           uf.num_segments[g] = N * 3;         }       } else {         g = uf.find(2 * groups_for_bit[i][0]);         int h = uf.find(2 * groups_for_bit[i][1]);         int ig = uf.find(inverse(g));         int ih = uf.find(inverse(h));         ;         ;         unset(g);         unset(ig);         unset(h);         unset(ih);         if (s[i] ==  0 ) {           uf.merge(ig, h);           uf.merge(g, ih);         } else {           uf.merge(g, h);           uf.merge(ig, ih);         }       }       if (g >= 0) {         g = uf.find(g);         int ig = uf.find(inverse(g));         set(uf.num_segments[g] <= uf.num_segments[ig] ? g : ig);       }       cout << num_segments <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const pair<int, int> Dir[8] = {{0, 1}, {0, -1}, {1, 0},   {-1, 0},                                  {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};   template <typename A, size_t N, typename T>   void Fill(A (&array)[N], const T &val) {     std::fill((T *)array, (T *)(array + N), val);   }   signed main() {     long long n;     scanf( %lld , &n);     long long dp[2] = {1, 2};     long long turn = 1;     while (1) {       long long next = dp[0] + dp[1];       if (next > n) {         break;       }       dp[0] = dp[1];       dp[1] = next;       turn++;     }     printf( %lld n , turn);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, ad, a[444444], u[444444];   char str[11];   int getnum(char str[]) {     int ret = 0, neg = -1, i = 1;     if (str[0] !=  - ) {       neg = 1;       i = 0;     }     for (; str[i]; i++) {       ret = ret * 10 + str[i] -  0 ;     }     ret *= neg;     return ret;   }   int main() {     int i, j, fg = 0, nd, pd, ll, lt, rt, ds, mid, ld;     scanf( %d%d , &n, &k);     ad = k;     for (i = 1; i <= n; i++) {       scanf( %s , str);       if (str[0] ==  ? )         a[i + ad] = 1;       else         u[i + ad] = getnum(str);     }     for (i = 1; i <= ad; i++) {       u[i] = -1010000000 + i;       u[ad + n + i] = 1010000000 + i;     }     for (i = 1; i <= k; i++) {       if (fg) break;       nd = pd = -1;       ll = u[i];       for (j = i + k; j <= n + 2 * ad; j += k) {         if (a[j]) continue;         if (u[j] <= ll) {           fg = 1;           break;         }         ll = u[j];       }       if (fg) break;       for (j = i; j <= n + 2 * ad; j += k) {         if (pd == -1 && !a[j] && u[j] >= 0) {           pd = j;           break;         }       }       for (j = pd; j >= 1; j -= k) {         if (nd == -1 && !a[j] && u[j] < 0) {           nd = j;           break;         }       }       for (j = pd + k; j <= n + 2 * ad; j += k) {         ld = j - k;         if (!a[j]) {           if (u[ld] >= u[j]) fg = 1;           continue;         }         if (fg) break;         a[j] = 0;         u[j] = u[ld] + 1;       }       if (fg) break;       for (j = nd - k; j >= 1; j -= k) {         ld = j + k;         if (!a[j]) {           if (u[j] >= u[ld]) fg = 1;           continue;         }         if (fg) break;         a[j] = 0;         u[j] = u[ld] - 1;       }       mid = (pd - nd) / k - 1;       if (mid > u[pd] - u[nd]) {         fg = 1;         break;       }       lt = -((mid - 1) / 2);       rt = mid / 2;       if (u[pd] <= rt) {         ds = rt - u[pd] + 1;         rt -= ds;         lt -= ds;       }       if (u[nd] >= lt) {         ds = u[nd] - lt + 1;         rt += ds;         lt += ds;       }       for (j = nd + k; j < pd; j += k) {         u[j] = lt;         lt++;       }     }     if (fg)       puts( Incorrect sequence );     else {       for (i = k + 1; i <= k + n; i++) printf( %d  , u[i]);       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int c, t, n, a[1000005], b[1000005], used[100005];   int main() {     cin >> t;     while (t--) {       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];         used[i] = 0;       }       for (int i = 1; i <= n; i++) cin >> b[i];       if (n % 2 == 1 && a[n / 2 + 1] != b[n / 2 + 1]) {         cout <<  NO  << endl;         continue;       }       c = 1;       long long int l, r;       if (n % 2 == 1)         l = n / 2, r = n / 2 + 2;       else         l = n / 2, r = n / 2 + 1;       for (int j = l; j >= 1; j--) {         long long int ok = 1;         for (int i = 1; i <= n / 2; i++) {           if (used[i] == 1) continue;           if ((a[i] == b[l] && a[n - i + 1] == b[r]) ||               (a[i] == b[r] && a[n - i + 1] == b[l])) {             used[i] = 1;             ok = 0;             break;           }         }         if (ok == 1) {           c = 0;           break;         }         l--;         r++;       }       if (c == 1)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int main() {     scanf( %d%d , &n, &m);     if (n > m) swap(n, m);     int ans;     if (n == 1)       ans = m;     else if (n == 2) {       if (m % 4 <= 2)         ans = m + m % 4;       else         ans = m + 1;     } else       ans = (n * m + 1) / 2;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   using namespace std;   long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return ((a * b) / (gcd(a, b))); }   bool compare(const pair<long long, long long> &a,                const pair<long long, long long> &b) {     return a.second < b.second;   }   long long ceil(long long a, long long b) { return (a + b - 1) / b; }   long long binpowmod(long long a, long long b, long long m) {     long long res = 1;     while (b > 0) {       if (b & 1) {         res = ((res % m) * (a % m)) % m;       }       a = ((a % m) * (a % m)) % m;       b >>= 1;     }     res %= m;     return res;   }   long long binpow(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) {         res = ((res) * (a));       }       a = ((a) * (a));       b >>= 1;     }     return res;   }   void swap(long long *a, long long *b) {     long long temp = *a;     *a = *b;     *b = temp;   }   long long sum_digits(long long x) {     long long s = 0;     while (x) {       s += (x % 10);       x /= 10;     }     return s;   }   bool is_perfect_square(long long n) {     return (floor(double(sqrt(n))) == ceil(double(sqrt(n))));   }   vector<int> pr;   void prime() {     const int N = 10000000;     int lp[N + 1] = {0};     for (int i = 2; i <= N; ++i) {       if (lp[i] == 0) {         lp[i] = i;         pr.push_back(i);       }       for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j)         lp[i * pr[j]] = pr[j];     }   }   long long bs(vector<long long> &arr, long long l, long long r, long long x) {     long long mid = 0;     if (r >= l) {       mid = l + (r - l) / 2;       if (arr[mid] == x) return mid;       if (arr[mid] > x) return bs(arr, l, mid - 1, x);       return bs(arr, mid + 1, r, x);     }     return -1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     std::vector<long long> v;     int c1 = 0, c2 = 0;     for (int i = (0); i < (n); i++) {       int x;       cin >> x;       if (x == 1)         c1++;       else         c2++;     }     if (c1 == 0)       v = std::vector<long long>(n, 2);     else if (c2 == 0)       v = std::vector<long long>(n, 1);     else {       v.push_back(2);       c2--;       v.push_back(1);       c1--;       while ((c2)--) v.push_back(2);       while ((c1)--) v.push_back(1);     }     for (int i = (0); i < (n); i++) cout << v[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int a[200100];   int sub[200100];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       sort(a + 1, a + n + 1);       for (int i = 1; i <= n; i++) {         sub[i] = sub[i - 1] + a[i - 1];         a[i] = a[i] - sub[i];       }       sort(a + 1, a + 1 + n);       cout << a[n] << endl;       memset(sub, 0, sizeof(sub));     }   }
#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000007ll;   const long long int MOD = 1000000009ll;   int local_debug = 0;   long long int bit_count(long long int _x) {     long long int _ret = 0;     while (_x) {       if (_x % 2 == 1) _ret++;       _x /= 2;     }     return _ret;   }   long long int bit(long long int _mask, long long int _i) {     long long int x = 1;     return (_mask & (x << _i)) == 0 ? 0 : 1;   }   long long int powermod(long long int _a, long long int _b,                          long long int _m = mod) {     long long int _r = 1;     while (_b) {       if (_b % 2 == 1) _r = (_r * _a) % _m;       _b /= 2;       _a = (_a * _a) % _m;     }     return _r;   }   long long int power(long long int _a, long long int _b) {     long long int _r = 1;     while (_b) {       if (_b % 2 == 1) _r = (_r * _a);       _b /= 2;       _a = (_a * _a);     }     return _r;   }   long long int add(long long int a, long long int b, long long int m = mod) {     a %= m;     b %= m;     long long int x = a + b;     while (x >= m) x -= m;     return x;   }   long long int sub(long long int a, long long int b, long long int m = mod) {     long long int x = a - b;     while (x < 0) x += m;     return x;   }   long long int mul(long long int a, long long int b, long long int m = mod) {     a %= m;     b %= m;     long long int x = a * b;     x %= m;     return x;   }   long long int gcd(long long int a, long long int b) {     if (a < b) {       a = a ^ b;       b = a ^ b;       a = a ^ b;     }     if (b == 0) return a;     long long int t;     while (b) {       t = b;       b = a % b;       a = t;     }     return a;   }   long long int lcm(long long int a, long long int b) {     return (a * b) / gcd(a, b);   }   long long int maxSubArraySum(vector<long long int> a, long long int size) {     long long int max_so_far = a[0];     long long int curr_max = a[0];     for (long long int i = 1; i < size; i++) {       curr_max = max(a[i], curr_max + a[i]);       max_so_far = max(max_so_far, curr_max);     }     return max_so_far;   }   int main() {     long long int n;     scanf( %lld , &n);     ;     vector<long long> hrr(n);     vector<long long> grr(n);     for (int i = 0; i < n; i++) {       scanf( %lld , &hrr[i]);       ;       scanf( %lld , &grr[i]);       ;     }     int count = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j <= n; j++) {         if (hrr[i] == grr[j]) {           count++;         }       }     }     cout << count << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 105;   int atk[N], atk2[N], def2[N];   bool mark[N];   int main() {     int n, len;     while (scanf( %d%d , &n, &len) != EOF) {       int lena = 0, lend = 0, i, j;       for (i = 0; i < n; i++) {         char s[5];         int v;         scanf(  %s%d , s, &v);         if (s[0] ==  A )           atk2[lena++] = v;         else           def2[lend++] = v;       }       for (int i = 0; i < len; i++) scanf( %d , &atk[i]);       sort(atk, atk + len);       sort(atk2, atk2 + lena);       sort(def2, def2 + lend);       memset(mark, false, sizeof(mark));       int ans1 = 0, ans2 = 0;       for (i = 0, j = 0; i < lend && j < len; i++) {         while (j < len && (mark[j] || atk[j] <= def2[i])) j++;         if (j == len) break;         mark[j] = true;       }       if (j < len) {         for (i = 0, j = 0; i < lena && j < len; i++) {           while (j < len && (mark[j] || atk[j] < atk2[i])) j++;           if (j == len) break;           mark[j] = true;           ans1 += atk[j] - atk2[i];         }         if (j < len) {           for (i = 0; i < len; i++)             if (!mark[i]) ans1 += atk[i];         } else           ans1 = 0;       }       for (i = len - 1, j = 0; i >= 0 && j < lena && atk[i] >= atk2[j];            i--, j++) {         ans2 += atk[i] - atk2[j];       }       printf( %d n , max(ans1, ans2));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, a[10000], first = -1, val, t = 1;     cin >> n;     cin >> a[0];     for (int i = 1; i < n; i++) {       cin >> a[i];       if (a[i - 1] > a[i] || a[i - 1] == a[i]) t = 0;     }     if (t == 1) {       cout <<  YES ;       return 0;     }     for (int i = 0; i < n - 1; i++) {       if (a[i] > a[i + 1] || a[i] == a[i + 1]) {         first = i;         val = a[i];         break;       }     }     if (first == -1) {       first == n - 1;       val = a[n - 1];     }     for (int i = 0; a[i] != val; i++) {       if (a[i] == a[i + 1]) {         cout <<  NO ;         return 0;       }     }     for (int i = first; i < n - 1; i++) {       if (a[i] != a[i + 1]) {         first = i;         break;       }       if (a[i + 1] == val) {         first = i + 1;       }     }     for (int i = first; i < n - 1; i++) {       if (a[i] < a[i + 1] || a[i] == a[i + 1]) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;   }
#include <bits/stdc++.h>   using namespace std;   long long int n, a, k;   char s[2000007], res[2000007];   bool vis[2000007];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     long long int maxn = -1;     for (long long int i = 0; i < n; ++i) {       cin >> s >> k;       long long int len = strlen(s);       long long int cur = INT_MIN;       for (long long int j = 0; j < k; ++j) {         cin >> a;         a--;         maxn = max(maxn, a + len);         for (long long int jj = max(cur, a); jj <= a + len - 1; ++jj) {           if (!vis[jj]) {             vis[jj] = 1;             res[jj] = s[jj - a];           }         }         cur = max(cur, a + len - 1);       }     }     for (long long int i = 0; i < maxn; ++i)       if (!res[i])         cout <<  a ;       else         cout << res[i];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void read() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   string a1, a2, b1, b2;   void check() {     if (a1[0] ==  X )       swap(a1[0], a1[1]);     else if (a1[1] ==  X )       swap(a1[1], a2[1]);     else if (a2[1] ==  X )       swap(a2[1], a2[0]);     else if (a2[0] ==  X )       swap(a2[0], a1[0]);   }   void solve() {     cin >> a1 >> a2 >> b1 >> b2;     for (int i = 0; i < 65; ++i) {       check();       if (a1 == b1 && a2 == b2) {         cout <<  YES ;         return;       }     }     cout <<  NO ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int T = 1;     while (T--) {       solve();       cout <<   n ;     }     cerr <<   nTime elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char s[1000000];     cin >> s;     int l = strlen(s);     int a[26][2];     char x = 0;     int diff = 0;     for (int i = 0; i < 26; i++) {       a[i][0] = 0;       a[i][1] = 0;     }     for (int i = 0; s[i] !=   0 ; i++) {       x = (int)s[i];       x = x - 97;       diff = i - a[x][1];       if (a[x][0] == 0) {         diff++;       }       if (diff > a[x][0]) {         a[x][0] = diff;       }       a[x][1] = i;     }     for (int i = 0; i < 26; i++) {       diff = l - a[i][1];       if (diff > a[i][0]) {         a[i][0] = diff;       }     }     int min = 1000000;     for (int i = 0; i < 26; i++) {       if ((a[i][0] < min) && (a[i][0] != 0)) {         min = a[i][0];       }     }     cout << min << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void debug(T a, T b) {     ;   }   template <class T>   void chmin(T& a, const T& b) {     if (a > b) a = b;   }   template <class T>   void chmax(T& a, const T& b) {     if (a < b) a = b;   }   namespace std {   template <class S, class T>   ostream& operator<<(ostream& out, const pair<S, T>& a) {     out <<  (  << a.first <<  ,  << a.second <<  ) ;     return out;   }   }  // namespace std   const int INF = 7e8;   int n;   int ar[300005];   int sum[300005];   vector<int> Tpos, Opos;   int solve1(int a, int b, int A, int B) {     if (B == INF) return INF;     if (b == INF) return INF;     int Tlen = B - b;     int tmp = sum[b] * 1000;     int pt = sum[b] * 100;     int Trange = Tlen * 2000;     tmp += max(0, Trange - pt);     pt -= min(Trange, pt);     tmp += 1000;     pt += 100;     int rest = (sum[n] - sum[B + 1]) * 1000;     tmp += max(0, rest - pt);     return tmp;   }   int solve2(int a, int b, int A, int B) {     if (a == -1) return INF;     if (A == -1) return INF;     int Tlen = a - A;     int tmp = sum[A] * 1000;     int pt = sum[A] * 100;     tmp += max(0, 1000 - pt);     pt -= min(pt, 1000);     int Trange = Tlen * 2000;     tmp += Trange;     pt += Tlen * 200;     int rest = (sum[n] - sum[a + 1]) * 1000;     tmp += max(0, rest - pt);     return tmp;   }   int main() {     cin >> n;     Opos.push_back(-1);     Tpos.push_back(-1);     for (int i = 0; i < (n); ++i) {       int a;       scanf( %d , &a);       ar[i] = a / 1000;       if (ar[i] == 1)         Opos.push_back(i);       else         Tpos.push_back(i);     }     Tpos.push_back(INF);     Opos.push_back(INF);     for (int i = 0; i < (n); ++i) sum[i + 1] = sum[i] + ar[i];     int res = INF;     for (int i = 0; i < (n + 1); ++i) {       int tmp = sum[i] * 1000;       int pt = sum[i] * 100;       int rest = (sum[n] - sum[i]) * 1000;       tmp += max(0, rest - pt);       chmin(res, tmp);     }     int prev = res;     for (int i = 0; i < ((int)Tpos.size() - 1); ++i) {       int a = Tpos[i], b = Tpos[i + 1];       int B = *lower_bound((Opos).begin(), (Opos).end(), b);       int A = *(--lower_bound((Opos).begin(), (Opos).end(), a));       int X = solve1(a, b, A, B);       int Y = solve2(a, b, A, B);       chmin(res, X);       chmin(res, Y);     }     cout << res << endl;     return 0;   }