malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long MAX = 9999999999999999;   int t, n;   vector<long long> ans;   vector<long long> graphSize;   vector<long long> aToFirst, aToLast, bToFirst, bToLast;   void countLength(vector<long long> &toFirst, vector<long long> &toLast,                    long long v, int n) {     if (n == 0) {       toFirst[0] = 0;       toLast[0] = 0;       return;     }     if (n == 1) {       toFirst[1] = v == 2;       toLast[1] = v == 1;       return;     }     if (v <= graphSize[n - 1]) {       countLength(toFirst, toLast, v, n - 1);       toFirst[n] = min(toFirst[n - 1], toLast[n - 1] + 2);       toLast[n] = min(toFirst[n - 1], toLast[n - 1]) + (n - 1) / 2 + 1;     } else {       countLength(toFirst, toLast, v - graphSize[n - 1], n - 2);       toFirst[n] = toFirst[n - 2] + 1;       toLast[n] = toLast[n - 2];     }   }   long long findMinLength(long long a, long long b, int n) {     if (a == b) return 0;     if (n <= 2) return 1;     if (a <= graphSize[n - 1]) {       if (b >= graphSize[n - 1] + 1) {         return min(aToFirst[n - 1], aToLast[n - 1]) + bToFirst[n - 2] + 1;       } else {         long long d1 = min(aToFirst[n - 1] + bToLast[n - 1] + 2,                            aToLast[n - 1] + bToFirst[n - 1] + 2);         return min(d1, findMinLength(a, b, n - 1));       }     } else {       return findMinLength(a - graphSize[n - 1], b - graphSize[n - 1], n - 2);     }   }   int main() {     cin >> t >> n;     graphSize.push_back(1);     graphSize.push_back(2);     int iMax = 2;     for (; graphSize.back() < MAX; ++iMax) {       graphSize.push_back(graphSize[iMax - 1] + graphSize[iMax - 2]);     }     for (int i = 1; i <= t; ++i) {       long long a, b;       cin >> a >> b;       if (a > b) swap(a, b);       aToFirst.resize(min(n, iMax) + 1);       aToLast.resize(min(n, iMax) + 1);       bToFirst.resize(min(n, iMax) + 1);       bToLast.resize(min(n, iMax) + 1);       countLength(aToFirst, aToLast, a, min(n, iMax));       countLength(bToFirst, bToLast, b, min(n, iMax));       ans.push_back(findMinLength(a, b, min(n, iMax)));     }     for (int i = 0; i < ans.size(); ++i) {       cout << ans[i] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class Reduced_Indexs {     vector<int> idxs;     int n;       public:     Reduced_Indexs() = default;     template <typename InputIterator>     Reduced_Indexs(InputIterator first, InputIterator last) {       idxs = vector<int>(first, last);       sort(idxs.begin(), idxs.end());       n = unique(idxs.begin(), idxs.end()) - idxs.begin();       idxs.resize(n);     }     int size();     int get_idx(int);     int get_value(int);   };   int Reduced_Indexs::size() { return n; }   int Reduced_Indexs::get_idx(int x) {     auto p = lower_bound(idxs.begin(), idxs.end(), x);     return (p != idxs.end() && *p == x) ? (p - idxs.begin()) : -1;   }   int Reduced_Indexs::get_value(int i) { return idxs[i]; }   const int N = 100000;   const long long MOD = 1000000007LL;   long long pow2[N + 2];   int xs[N], ys[N];   Reduced_Indexs xidxs, yidxs;   vector<int> xeq[N], yeq[N];   bool checked[N];   set<int> xused, yused;   int cnt;   void check(int);   int main() {     pow2[0] = 1LL;     for (int i = 1; i <= N + 1; ++i) {       pow2[i] = (pow2[i - 1] * 2) % MOD;     }     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &xs[i], &ys[i]);     }     xidxs = Reduced_Indexs(xs, xs + n);     yidxs = Reduced_Indexs(ys, ys + n);     for (int i = 0; i < n; ++i) {       xeq[xidxs.get_idx(xs[i])].push_back(i);       yeq[yidxs.get_idx(ys[i])].push_back(i);     }     long long ans = 1LL;     for (int i = 0; i < n; ++i) {       xused.clear();       yused.clear();       cnt = 0;       check(i);       int total = xused.size() + yused.size();       ans = (ans * (pow2[total] - (cnt < total))) % MOD;     }     printf( %lld n , ans);     exit(EXIT_SUCCESS);   }   void check(int u) {     if (checked[u]) {       return;     }     checked[u] = true;     ++cnt;     int x = xs[u], y = ys[u];     if (!xused.count(x)) {       xused.insert(x);       for (int v : xeq[xidxs.get_idx(x)]) {         check(v);       }     }     if (!yused.count(y)) {       yused.insert(y);       for (int v : yeq[yidxs.get_idx(y)]) {         check(v);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int n, ar[3100], cur;   vector<int> v;   bool valid, used[10000];   pair<int, string> tab[3100];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> tab[i].second >> tab[i].first;     }     sort(tab, tab + n);     valid = true;     for (int i = 0; i < n; i++) {       if (tab[i].first > i) valid = false;     }     if (valid) {       cur = tab[0].first;       for (int i = 0; i < n; i++) {         ar[i] = 10;         if (tab[i].first > cur) {           for (int j = 0; j < v.size(); j++) {             ar[v[j]]++;           }           int bil = tab[i].first - cur;           int j = i;           while (bil > 0) {             j--;             if (used[j]) continue;             used[j] = true;             v.push_back(j);             bil--;           }           cur = tab[i].first;         }       }       for (int j = 0; j < v.size(); j++) {         ar[v[j]]++;       }       for (int i = 0; i < n; i++) {         cout << tab[i].second <<     << ar[i] << endl;       }     } else {       cout << -1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[500010], n, ans = 0;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int x = 1;     while (x < n) {       while ((x < n) && (a[x] == a[x + 1])) x++;       int y = x;       while ((x < n) && (a[x] != a[x + 1])) x++;       if (x - y >= 2) {         if (a[y] == a[x]) {           for (int i = y; i <= x; i++) a[i] = a[y];           ans = max(ans, (x - y) / 2);         } else {           for (int i = y; i <= (x + y) / 2; i++) a[i] = a[y];           for (int i = ((x + y) / 2) + 1; i <= x; i++) a[i] = a[x];           ans = max(ans, (x - y) / 2);         }       }     }     printf( %d n , ans);     for (int i = 1; i <= n; i++) printf( %d  , a[i]);   }
#include <bits/stdc++.h>   using namespace std;   void readInput() {}   void solve() {}   priority_queue<pair<int, int>> pq;   int n, x, y, res, a[100004], b[100004], T, cnt, vitri[100004];   vector<int> pos[100004], ans;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     readInput();     solve();     cin >> T;     while (T--) {       cin >> n >> x >> y;       for (int i = 1; i <= n + 1; i++) {         pos[i].clear();         b[i] = 0;         vitri[i] = 0;       }       ans.clear();       while (!pq.empty()) pq.pop();       for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]].push_back(i);       cnt = 1;       for (int i = 1; i <= n + 1; i++) {         pq.push({pos[i].size(), i});         if (pos[i].size() == 0) res = i;       }       while (cnt <= x) {         pair<int, int> u = pq.top();         pq.pop();         b[pos[u.second].back()] = u.second;         vitri[pos[u.second].back()] = 1;         pos[u.second].pop_back();         cnt++;         pq.push({pos[u.second].size(), u.second});       }       pair<int, int> u = pq.top();       if (2 * u.first > n - x + n - y) {         cout <<  NO  <<   n ;         continue;       } else {         cout <<  YES  <<   n ;         for (int i = 1; i <= n + 1; i++) {           for (auto v : pos[i]) ans.push_back(v);         }         cnt = 1;         for (int i = 0; i < ans.size(); i++) {           b[ans[i]] = a[ans[(i + ans.size() / 2) % (ans.size())]];         }         cnt = 1;         for (int i = 1; i <= n; i++) {           if (vitri[i] == 0 && b[i] == a[i]) {             vitri[i] = 1;             cnt++;             b[i] = res;           }         }         for (int i = 1; i <= n; i++) {           if (vitri[i] == 0 && cnt <= n - y) b[i] = res, cnt++;         }         for (int i = 1; i <= n; i++) cout << b[i] <<    ;         cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXLEN = 200;   char s[MAXLEN + 1];   int n;   void run() {     scanf( %d%s , &n, s);     n = strlen(s);     int ret = 0;     for (int at = 0, to = at; at < n; at = to) {       if (isupper(s[to])) {         ++to;         continue;       }       while (to < n && islower(s[to])) ++to;       int mask = 0, cnt = 0;       for (int i = (at); i < (to); ++i) {         int x = s[i] -  a ;         if (((mask >> x) & 1) == 0) mask |= 1 << x, ++cnt;       }       ret = max(ret, cnt);     }     printf( %d n , ret);   }   int main() {     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, c, l, r, i, j, x1, x2, x3, x, r_y, r_y1, r_y2, r_y3, k, g, sum,       maxy, maxx, qy, qx;   long long kk;   long long fact(long long a) {     unsigned long long k = 1, i;     for (i = 1; i <= a; i++) k *= i;     return k;   }   int main() {     cin >> x1 >> r_y1 >> x2 >> r_y2;     x = x2 - x1;     r_y = r_y2 - r_y1;     maxy = (2 + r_y) / 2;     qy = (2 + r_y) % 2;     maxx = x + 1;     qx = maxx % 2;     if (qy == 1)       cout << maxx * maxy;     else {       cout << (maxx / 2 + qx) * maxy + (maxx / 2) * (maxy - 1);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int cor[n], wro[m];     for (int i = 0; i < n; i++) cin >> cor[i];     for (int i = 0; i < m; i++) cin >> wro[i];     int tl;     sort(cor, cor + n);     sort(wro, wro + m);     int v = max(2 * cor[0], cor[n - 1]);     if (v < wro[0])       cout << v;     else       cout <<  -1 ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MX = 3009;   long long x[MX], par[MX], num[MX], ans = 0;   pair<long long, pair<long long, long long> > edg[MX];   void MS(long long v) {     par[v] = -1;     num[v] = x[v];   }   long long FS(long long v) {     if (par[v] < 0) return v;     return par[v] = FS(par[v]);   }   void US(long long a, long long b) {     a = FS(a), b = FS(b);     if (a == b) return;     if (par[a] > par[b]) swap(a, b);     par[a] += par[b];     par[b] = a;     num[a] += num[b];   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     for (long long i = 0; i < n - 1; i++) {       long long u, v, w;       cin >> u >> v >> w;       edg[i] = {w, {u, v}};     }     for (long long i = 1; i <= n; i++) cin >> x[i];     for (long long i = 1; i <= n; i++) MS(i);     sort(edg, edg + n - 1);     for (long long i = 0; i < n - 1; i++) {       long long u = edg[i].second.first, v = edg[i].second.second,                 w = edg[i].first;       long long maxx = 0, sum = 0, res = 1;       for (long long j = 1; j <= n; j++) {         if (par[j] < 0) {           maxx = max(maxx, -par[j]);           sum += num[j];         }       }       for (long long j = 1; j <= n; j++) {         if (par[j] < 0) {           if (sum - num[j] < -par[j]) res = 0;         }       }       if (res) ans = w;       US(u, v);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   unordered_map<int, int> dp;   int msb(int x) { return __builtin_clz(1) - __builtin_clz(x); }   int solve(int mask) {     if (mask <= 1) return 0;     if (dp.count(mask)) return dp[mask];     int maxi = msb(mask);     vector<int> mex;     for (int i = 1; i <= maxi; i++) {       int at = mask;       for (int j = i; j <= maxi; j++)         if (mask & (1 << j)) {           at ^= (1 << j);           if (i != j) at |= (1 << (j - i));         }       mex.push_back(solve(at));     }     sort(mex.begin(), mex.end());     vector<int>::iterator it = unique(mex.begin(), mex.end());     mex.resize(distance(mex.begin(), it));     int g = 0;     int i = 0;     for (auto k : mex) {       if (g == k)         g++;       else         break;     }     return dp[mask] = g;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     map<int, vector<int>> mp;     for (int i = 0; i < n; i++) {       int a;       cin >> a;       for (int i = 2; i * i <= a; i++)         if (a % i == 0) {           int at = 0;           while (a % i == 0) a /= i, at++;           mp[i].push_back(at);         }       if (a != 1) mp[a].push_back(1);     }     int ans = 0;     for (auto x : mp) {       sort(x.second.begin(), x.second.end());       vector<int>::iterator it = unique(x.second.begin(), x.second.end());       x.second.resize(distance(x.second.begin(), it));       int at = 0;       for (int k : x.second) {         at += 1 << k;       }       ans ^= solve(at);     }     if (ans == 0)       cout <<  Arpa  << endl;     else       cout <<  Mojtaba  << endl;     exit(0);   }
#include <bits/stdc++.h>   using namespace std;   long long int arr[200001];   int cnt[200001];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n;     bool flag;     int T;     cin >> T;     while (T--) {       cin >> n;       for (int i = 0; i < n; i++) {         cin >> arr[i];       }       for (int i = 0; i < n; i++) {         cnt[i] = 0;       }       for (int i = 0; i < n; i++) {         arr[i] = (arr[i] + i + n * 2147483647) % n;         cnt[arr[i]]++;       }       flag = 1;       for (int i = 0; i < n; i++) {         if (cnt[i] != 1) {           flag = 0;         }       }       if (flag == 1) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int pos, neg, tmp, x, y, len;   int main() {     scanf( %d%d , &n, &m);     pos = 0;     neg = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , &tmp);       if (tmp > 0)         ++pos;       else         ++neg;     }     while (m--) {       scanf( %d%d , &x, &y);       len = y - x + 1;       if (len & 1)         printf( 0 n );       else {         if (len / 2 <= min(pos, neg))           printf( 1 n );         else           printf( 0 n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   unordered_map<int, int> Task;   unordered_map<int, int> Idle;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int N, K;     cin >> N >> K;     for (int i = 1; i <= N; i++) {       int val;       cin >> val;       Task[val]++;       Idle[i] = val;     }     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         Cost;     for (int i = 1; i <= N; i++) {       int val;       cin >> val;       if (Task[Idle[i]] == 1) continue;       Cost.push(make_pair(val, i));     }     long long int Ans = 0;     for (int i = 1; i <= K; i++) {       if (Task[i] == 0) {         while (!Cost.empty()) {           pair<int, int> u = Cost.top();           Cost.pop();           if (Task[Idle[u.second]] > 1) {             Task[Idle[u.second]]--;             Ans += u.first;             Task[i]++;             break;           }         }       }     }     cout << Ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, u, v, a[1010], b[1010][1010];   vector<int> vv;   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &x, &y);       a[y]++;       b[x][y]++;     }     for (int i = 0; i < m; ++i) {       scanf( %d%d , &x, &y);       if (a[y] && b[x][y]) {         ++u;         --a[y];         ++v;         --b[x][y];       } else         vv.push_back(y);     }     for (int i = 0; i < (int)vv.size(); ++i)       if (a[vv[i]]) ++u, --a[vv[i]];     printf( %d %d , u, v);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = 3.14159265358979323846;   void rw() {}   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s, r;     long long n, c = 0;     cin >> n >> s;     for (int i = 0; i < n - 1; i++) {       if (s[i] == s[i + 1])         c++, s[i] =  0 ;       else         i++;     }     cout << ((n - c) % 2 ? c + 1 : c);     for (int i = 0; i < n; i++)       if (s[i] !=  0 ) r += s[i];     if ((n - c) % 2) r.erase(r.size() - 1);     cout <<   n  << r;     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize(2)   #pragma G++ optimize(2)   using namespace std;   constexpr long long N = 2e5 + 9;   constexpr long long M = 1e9 + 7;   constexpr long long INF = 0x3f3f3f3f3f3f3f3f;   constexpr long long INF2 = 0xcfcfcfcfcfcfcfcf;   long long n;   long long a[N], b[N];   string ans;   long long p, r;   void print() {     for (long long i = 1; i <= n; i++) cout << b[i] <<    ;     cout << endl;   }   signed fail() {     cout <<  IMPOSSIBLE  << endl;     return 0;   }   bool rcheck() {     for (long long i = 1; i <= n; i++) {       if (a[i] != b[n + 1 - i]) return false;     }     r++;     ans +=  R ;     return true;   }   bool check() {     if (rcheck()) return true;     for (long long i = 1; i <= n; i++) {       if (a[i] != b[i]) return false;     }     return true;   }   signed answer() {     if (p <= 2e5) {       cout <<  SMALL  << endl;       cout << ans.size() << endl;       reverse(ans.begin(), ans.end());       cout << ans << endl;       return 0;     }     cout <<  BIG  << endl;     cout << p << endl;     return 0;   }   signed solve1() {     if (check()) {       return answer();     }     return fail();   }   inline bool inc() {     for (long long i = 1; i < n; i++) {       if (b[i + 1] <= b[i]) return false;     }     return true;   }   bool R() {     if (inc()) return true;     reverse(b + 1, b + 1 + n);     r++;     ans +=  R ;     return inc();   }   inline bool P() {     for (long long i = n; i >= 1; i--) {       b[i] = b[i] - b[i - 1];       if (b[i] < 1) return false;     }     p++;     ans +=  P ;     return true;   }   signed solve2() {     bool rev = false;     if (a[1] > a[2]) swap(a[1], a[2]), r++, rev = true;     while (!check()) {       if (b[1] == b[2]) return fail();       if (b[1] > b[2]) {         swap(b[1], b[2]), r++;         if (p <= 2e5) ans +=  R ;         continue;       }       if (b[1] == b[2]) return fail();       if (a[1] == b[1]) {         if ((b[2] - a[2]) % b[1] == 0 && b[2] > a[2]) {           long long op = (b[2] - a[2]) / b[1];           p += op;           if (p <= 2e5) {             while (op--) ans +=  P ;           }           break;         } else           return fail();       }       long long op = b[2] / b[1];       p += op;       if (p <= 2e5)         while (op--) ans +=  P ;       b[2] = b[2] % b[1];       if (b[2] == 0) return fail();     }     if (rev) ans +=  R ;     return answer();   }   signed solve() {     while (!check()) {       if (!R()) return fail();       if (!P()) return fail();     }     answer();     return 0;   }   signed main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     cin >> n;     for (long long i = 1; i <= n; i++) cin >> a[i];     for (long long i = 1; i <= n; i++) cin >> b[i];     if (n == 1) return solve1();     if (n == 2) return solve2();     return solve();   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   const int INF = 0x7fffffff;   vector<int> G[maxn];   long long weight[maxn];   long long dp[maxn];   int ans;   long long maxsum = -INF;   void dfs(int now, int fa) {     dp[now] = weight[now];     for (int v : G[now]) {       if (v != fa) {         dfs(v, now);         if (dp[v] > 0) dp[now] += dp[v];       }     }     maxsum = max(maxsum, dp[now]);   }   void dfs2(int now, int fa) {     dp[now] = weight[now];     for (int v : G[now]) {       if (v != fa) {         dfs2(v, now);         if (dp[v] > 0) dp[now] += dp[v];       }     }     if (dp[now] == maxsum) {       ans++;       dp[now] = 0;     }   }   int main() {     std::ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> weight[i];     int from, to;     for (int i = 2; i <= n; ++i) {       cin >> from >> to;       G[from].push_back(to);       G[to].push_back(from);     }     dfs(1, 0);     dfs2(1, 0);     cout << maxsum * ans <<     << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int i, j, k, n, l, n1, n2, m, m1, m2;     cin >> s;     l = s.size();     for (i = l - 1; i >= 0; i--) {       n = s[i] - 48;       m = n;       if (m == 0) {         cout <<  YES  << endl << 0 << endl;         exit(0);       }       if (n % 2 == 0) {         if (m % 8 == 0) {           cout <<  YES  << endl << m << endl;           exit(0);         }         if (i == 0) {           break;         }         for (j = i - 1; j >= 0; j--) {           n1 = (s[j] - 48) * 10;           m1 = n1 + m;           if (m1 % 8 == 0) {             cout <<  YES  << endl << m1 << endl;             exit(0);           }           if (j == 0) {             break;           }           for (k = j - 1; k >= 0; k--) {             n2 = (s[k] - 48) * 100;             m2 = n2 + m1;             if (m2 % 8 == 0) {               cout <<  YES  << endl << m2 << endl;               exit(0);             }           }         }       }     }     cout <<  NO  << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[105];     int cnt = 0;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 2; i <= n - 1; i++) {       if (a[i] == 0 && a[i - 1] == a[i + 1] && a[i + 1] == 1) {         a[i + 1] = 0;         cnt++;       }     }     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   int product(long long a) {     int maxi = 0, mini = 9, x, ans;     long long temp = a;     while (temp) {       x = temp % 10;       if (x < mini) mini = x;       if (x > maxi) maxi = x;       temp /= 10;     }     if (mini == 0) return 0;     ans = mini * maxi;     return ans;   }   using namespace std;   int main() {     ios_base::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);     long long a, k, ans;     int t;     cin >> t;     while (t--) {       cin >> a >> k;       k--;       while (k--) {         if (product(a) == 0)           break;         else {           a += product(a);         }       }       cout << a <<   n ;     }     return 0;   }