malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   string a[200009];   int pre[1 << 20][22], cost[200009], n, m, dp[200009];   int compute(string suf, string pre) {     for (int k = int(0); k <= int(m - 1); k++) {       int j = k, i = 0;       while (j < m) {         if (suf[j] == pre[i]) {           j++;           i++;         } else           break;       }       if (j == m) return k;     }     return m;   }   int calmask(string str, int len) {     int mask = 0;     for (int i = int(m - len); i <= int(m - 1); i++) {       mask = mask * 2;       if (str[i] ==  1 ) mask++;     }     return mask;   }   int calmask2(string str, int len) {     int mask = 0;     for (int i = int(0); i <= int(len - 1); i++) {       mask = mask * 2;       if (str[i] ==  1 ) mask++;     }     return mask;   }   int main() {     std::ios::sync_with_stdio(false);     int ans;     cin >> n;     for (int i = int(1); i <= int(n); i++) cin >> a[i];     m = a[1].length();     for (int i = int(0); i <= int((1 << m) - 1); i++)       for (int j = int(0); j <= int(m); j++) pre[i][j] = 2000000000;     cost[0] = 0;     for (int i = int(1); i <= int(n - 1); i++)       cost[i] = cost[i - 1] + compute(a[i], a[i + 1]);     pre[0][0] = cost[n - 1];     ans = 2000000000;     for (int i = int(n); i >= int(1); i--) {       int mini = 2000000000;       int mask = 0;       for (int j = int(0); j <= int(m); j++) {         if (i == 1 && j > 0) break;         mini = min(mini, pre[mask][j] + m - j);         if (m - j - 1 >= 0 && a[i - 1][m - j - 1] ==  1 ) mask |= (1 << j);       }       mini = mini - cost[i - 1];       dp[i] = mini;       if (i - 2 >= 0) {         mask = 0;         for (int j = int(0); j <= int(m); j++) {           pre[mask][j] = min(pre[mask][j], mini + cost[i - 2]);           mask *= 2;           if (j < m && a[i][j] ==  1 ) mask++;         }       }     }     cout << dp[1] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     for (int i = 0; i < a.size(); i++) {       a[i] = tolower(a[i]);       b[i] = tolower(b[i]);     }     if (a.compare(b) == 0)       cout <<  0             <<   n ;     else {       for (int i = 0; i < a.size(); i++) {         if (a[i] < b[i]) {           cout <<  -1                 <<   n ;           break;         }         if (a[i] > b[i]) {           cout <<  1                 <<   n ;           break;         }       }     }     return 0;   }
#include <bits/stdc++.h>   inline int sbt(int x) { return __builtin_popcount(x); }   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cout << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n, x, y, d;       int res = 1000000007;       cin >> n >> x >> y >> d;       int dff = abs(x - y);       if (!(dff % d)) res = min(res, dff / d);       if (!((y - 1) % d))         res = min(res, (y - 1) / d + (x - 1) / d + (((x - 1) % d) > 0));       if (!((n - y) % d))         res = min(res, (n - x) / d + (n - y) / d + (((n - x) % d) > 0));       if (res == 1000000007)         cout << -1;       else         cout << res;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   const int INFI = (1 << 29);   const long long INFL = (1LL << 62);   using namespace std;   long long f[1000010];   void solve() {     long long n, k, d, c = 0, r = INFL;     cin >> n >> k >> d;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     for (long long i = 0; i < n; i++) {       f[v[i]]++;       c += (f[v[i]] == 1);       if (i >= d) {         f[v[i - d]]--;         c -= (!f[v[i - d]]);       }       if (i >= (d - 1)) r = min(r, c);     }     cout << r <<   n ;     for (long long i = 0; i < n; i++) f[v[i]] = 0;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long casos;     cin >> casos;     while (casos--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int mod;   long long int power(long long int a, long long int b) {     if (b == 0) return 1;     if (b % 2 == 1) return (power(a, b - 1) * a) % mod;     long long int q = power(a, b / 2);     return (q * q) % mod;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     long long int a[n];     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     long long int ans = 1;     for (long long int i = 0; i < n; i++) {       long long int cnt = 0;       long long int prev = a[i];       for (long long int j = i - 1; j >= 0; j--) {         if (a[j] <= prev) {           prev = a[j];           cnt++;         } else           break;       }       prev = a[i];       for (long long int j = i + 1; j < n; j++) {         if (a[j] <= prev) {           prev = a[j];           cnt++;         } else           break;       }       cnt++;       ans = max(ans, cnt);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   template <class T>   ostream& operator<<(ostream& os, vector<T> V) {     os <<  [  ;     for (auto v : V) os << v <<    ;     return os <<  ] ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> P) {     return os <<  (  << P.first <<  ,  << P.second <<  ) ;   }   const long long int mod = 998244353;   const long long int maxn = 200001;   long long int power(long long int x, long long int n) {     long long int res = 1;     for (; n > 0; n >>= 1) {       if (n & 1) res = (res * x) % mod;       x = (x * x) % mod;     }     return res;   }   long long int no_of_ways(long long int m, long long int x) {     long long int ans = 0;     for (long long int r = 2; r <= 1000000; r++) {       long long int maxi = r * r * r;       if (maxi > x) break;       ans += (x / maxi);     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int m;     cin >> m;     long long int l = 1, r = 1e18;     long long int ans = -1;     while (r - l > 1) {       long long int mid = (l + r) / 2;       if (no_of_ways(m, mid) >= m)         r = mid;       else if (no_of_ways(m, mid) < m)         l = mid + 1;     }     if (no_of_ways(m, l) == m)       ans = l;     else if (no_of_ways(m, r) == m)       ans = r;     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     for (int i = 2; i < s.size(); i++) {       int a = 0, b = 0, c = 0;       for (int j = i; j >= i - 2; j--) {         if (s[j] ==  A ) a++;         if (s[j] ==  B ) b++;         if (s[j] ==  C ) c++;       }       if (a > 0 && b > 0 && c > 0) {         cout <<  Yes ;         return 0;       }     }     cout <<  No ;     return 0;   }
#include <bits/stdc++.h>   class point2D {    public:     long long x, y;     point2D() {}     point2D(long long x, long long y) : x(x), y(y) {}     bool operator<(const point2D &other) const {       return ((y == other.y) ? (x > other.x) : (y > other.y));     }   };   point2D operator-(const point2D &u, const point2D &v) {     return point2D(u.x - v.x, u.y - v.y);   }   long long det(const point2D &u, const point2D &v) {     return u.x * v.y - v.x * u.y;   }   long long dir(const point2D &p, const point2D &u, const point2D &v) {     return det(u - p, v - p);   }   bool contains(int n, point2D vertex, point2D polygon[]) {     int l = 1, r = n - 1, m;     while (l < r) {       m = (l + r) / 2;       if (dir(polygon[0], polygon[m], vertex) <= 0)         r = m;       else         l = m + 1;     }     if (r == 1 || dir(polygon[0], polygon[n - 1], vertex) >= 0 ||         dir(polygon[r], polygon[r - 1], vertex) >= 0)       return 0;     return 1;   }   int n, m;   point2D polygonA[100000], vertexB;   int main() {     std::cin >> n;     for (int i = n - 1; i >= 0; i--) {       std::cin >> polygonA[i].x >> polygonA[i].y;     }     std::cin >> m;     for (int i = 0; i < m; i++) {       std::cin >> vertexB.x >> vertexB.y;       if (!contains(n, vertexB, polygonA)) {         std::cout <<  NO  << std::endl;         return 0;       }     }     std::cout <<  YES  << std::endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int g[30][30];   char s[20];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , s);       int len = strlen(s);       for (int j = 0; j < len; j++) s[j] -=  a ;       for (int j = 0; j < 26; j++) {         if (g[j][s[0]] || j == s[0])           g[j][s[len - 1]] = max(g[j][s[len - 1]], g[j][s[0]] + len);       }     }     int maxn = 0;     for (int i = 0; i < 26; i++) maxn = max(maxn, g[i][i]);     printf( %d n , maxn);   }
#include <cstdio>   #include <iostream>   #include <cstdlib>   #include <cstring>   #include <string>   #include <algorithm>   #include <cmath>   #define LL long long   using namespace std;   LL T;   LL n, arr[300000], cnt[300000], dp[300000];   int main (){       cin >> T;       while (T--){           scanf( %lld , &n);           for (LL i = 1; i <= 200000; ++ i)               cnt[i] = 0;           for (LL i = 1; i <= n; ++ i){               scanf( %lld , &arr[i]);               cnt[arr[i]] ++;           }           for (LL i = 1; i <= 200000; ++ i)               dp[i] = cnt[i];           //sort(arr + 1, arr + 1 + n);           for (LL i = 1;i <= 200000; ++ i){               for (LL j = i * 2; j <= 200000; j += i){                   dp[j] = max(dp[j], dp[i] + cnt[j]);               }           }           printf( %lld n , n - *max_element(dp + 1, dp + 200001));       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 100;   int ans[N], link[N];   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       int x;       cin >> x;       int down = max(1, i - k);       int up = min(n, i + k);       int down2 = max(1, x - k);       int up2 = min(n, x + k);       if (x == 0) {         ans[i] = up - down + 1 + ans[x];       } else {         if (up2 < down)           ans[i] = up - down + 1 + ans[x];         else           ans[i] = up - down + 1 + ans[x] - (up2 - down + 1);       }       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int ans = 0;       string s;       cin >> s;       if (s.size() % 2 == 0) {         for (int j = 0; j < s.size() / 2; j++) {           if (s[j] != s[s.size() / 2 + j]) {             ans = 1;             cout <<  NO  << endl;             break;           }         }         if (ans != 1) {           cout <<  YES  << endl;         }       } else {         cout <<  NO  << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, q, a[200010];   struct node {     int l, r, color;     node(int ll = 0, int rr = 0, int cc = 0) : l(ll), r(rr), color(cc) {}   };   node p[200010];   vector<int> vec[200010];   bool cmp(node a, node b) {     if (a.l != b.l) return a.l < b.l;     return a.r > b.r;   }   int main() {     cin >> n >> q;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       vec[a[i]].push_back(i);     }     int cnt = 0;     for (int i = 1; i <= 200000; ++i) {       if (!vec[i].size()) continue;       int l = vec[i][0], r = vec[i][vec[i].size() - 1];       p[++cnt] = node(l, r, i);     }     sort(p + 1, p + 1 + cnt, cmp);     p[cnt + 1].l = 1e9 + 10;     int ans = 0, l = 1e9 + 10, r = 0, tmp = 0;     for (int i = 1; i <= cnt + 1; ++i) {       if (i == 1 || p[i].l < r) {         l = min(l, p[i].l);         r = max(r, p[i].r);         tmp = max(tmp, (int)vec[p[i].color].size());       } else {         ans += r - l + 1 - tmp;         l = p[i].l;         r = p[i].r;         tmp = vec[p[i].color].size();       }     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> stations[200001];   int nxt[200001];   int main() {     int d, n, m;     scanf( %d%d%d , &d, &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &stations[i].first, &stations[i].second);     }     sort(stations, stations + m);     stack<int> s;     for (int i = m - 1; i > -1; i--) {       nxt[i] = -1;       while (!s.empty()) {         if (stations[s.top()].second > stations[i].second) {           s.pop();         } else {           nxt[i] = s.top();           break;         }       }       s.push(i);     }     long long cost = 0;     int gas = n;     for (int i = 0; i < m; i++) {       gas -= stations[i].first - (i == 0 ? 0 : stations[i - 1].first);       if (gas < 0) {         cout << -1 << endl;         return 0;       }       int needed;       if (nxt[i] == -1 || stations[nxt[i]].first - stations[i].first > n) {         needed = min(n, d - stations[i].first);       } else {         needed = stations[nxt[i]].first - stations[i].first;       }       if (gas < needed) {         cost += (long long)stations[i].second * (needed - gas);         gas = needed;       }     }     gas -= d - stations[m - 1].first;     if (gas < 0) {       cout << -1 << endl;     } else {       cout << cost << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int tree[N << 2], Max[N << 2], Min[N << 2], cnt;   bool fu[N << 2];   pair<int, int> a[N];   void build(int l, int r, int rt) {     if (l == r) {       tree[rt] = a[l].first;       Max[rt] = Min[rt] = tree[rt];       return;     }     int m = (l + r) >> 1;     build(l, m, rt << 1);     build(m + 1, r, rt << 1 | 1);     Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);     Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);   }   bool qu(int f, int x, int rt) {     int ma, mi;     ma = Max[rt], mi = Min[rt];     if ((f ==  >  && mi > x) || (f ==  <  && ma < x))       return true;     else       return false;   }   bool qu2(int f, int x, int rt) {     int ma, mi;     ma = Max[rt], mi = Min[rt];     if ((f ==  >  && ma > x) || (f ==  <  && mi < x))       return true;     else       return false;   }   void ff(int rt) {     fu[rt] = !fu[rt];     swap(Max[rt], Min[rt]);     Max[rt] *= -1;     Min[rt] *= -1;   }   void updata(int l, int r, int rt, char f, int x) {     if (qu(f, x, rt)) {       ff(rt);       return;     }     int m = (l + r) >> 1;     if (fu[rt]) ff(rt << 1), ff(rt << 1 | 1), fu[rt] = 0;     if (qu2(f, x, rt << 1)) updata(l, m, rt << 1, f, x);     if (qu2(f, x, rt << 1 | 1)) updata(m + 1, r, rt << 1 | 1, f, x);     Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);     Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);   }   int ans[N];   void print(int l, int r, int rt) {     if (l == r) {       if (fu[rt]) tree[rt] *= -1;       ans[a[l].second] = tree[rt];       return;     }     int m = (l + r) >> 1;     if (fu[rt]) ff(rt << 1), ff(rt << 1 | 1), fu[rt] = 0;     print(l, m, rt << 1);     print(m + 1, r, rt << 1 | 1);   }   int main() {     int n, q;     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i].first, a[i].second = i;     sort(a + 1, a + n + 1);     build(1, n, 1);     while (q--) {       char f;       int x;       cin >> f >> x;       updata(1, n, 1, f, x);     }     print(1, n, 1);     for (int i = 1; i <= n; i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 1212;   int num, n, x, i, j, cur, t, s[MX], a[MX][3], r[MX], mx[MX], mxs[MX];   priority_queue<pair<int, int>> q[2];   vector<int> v[MX];   bool u[MX];   int main() {     scanf( %d , &n);     for (i = 0; i < 3 * n; i++) {       scanf( %d , &x);       u[x] = true;     }     for (i = 1; i <= 6 * n; i++)       if (t > 1 && u[s[t]] == u[i] && u[s[t - 1]] == u[i]) {         a[++num][0] = s[t - 1];         a[num][1] = s[t];         a[num][2] = i;         t -= 2;         for (j = 0; j < 3; j++) mxs[num] = max(mxs[num], mx[a[num][j]]);         v[s[t]].push_back(num);         mx[s[t]] = max(mx[s[t]], mxs[num] + 1);       } else         s[++t] = i;     for (i = 0; i < v[0].size(); i++)       q[int(u[a[v[0][i]][0]])].push({mxs[v[0][i]], v[0][i]});     for (i = 0; i < 2 * n; i++) {       cur = i & 1;       x = q[cur].top().second;       q[cur].pop();       r[2 * n - i - 1] = x;       cur = 1 - cur;       for (j = 0; j < 3; j++)         for (int k : v[a[x][j]]) q[cur].push({mxs[k], k});     }     for (i = 0; i < 2 * n; i++)       for (j = 0; j < 3; j++) printf( %d%c , a[r[i]][j], (j == 2) ?   n  :    );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[405][405];   int q[405 * 405], d[405], t, h, ans;   bool vis[405];   int main() {     int n, m;     scanf( %d %d , &n, &m);     while (m--) {       int u, v;       scanf( %d %d , &u, &v);       a[u][v] = a[v][u] = 1;     }     t = h = 0;     q[h++] = 1;     vis[1] = 1;     memset(d, -1, sizeof d);     d[1] = 0;     while (t < h) {       int u = q[t++];       for (int i = 1; i <= n; i++) {         if (vis[i] || a[u][i] == 0) continue;         d[i] = d[u] + 1;         vis[i] = 1;         q[h++] = i;       }     }     if (d[n] == -1) {       puts( -1 );       return 0;     }     ans = d[n];     t = h = 0;     q[h++] = 1;     memset(vis, 0, sizeof vis);     vis[1] = 1;     memset(d, -1, sizeof d);     d[1] = 0;     while (t < h) {       int u = q[t++];       for (int i = 1; i <= n; i++) {         if (vis[i] || a[u][i]) continue;         d[i] = d[u] + 1;         vis[i] = 1;         q[h++] = i;       }     }     if (d[n] == -1) {       puts( -1 );       return 0;     }     ans = max(ans, d[n]);     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   long long t, n;   char str[200005];   void solve() {     long long i, b = 0, c, s = 1, min = 0;     char a;     a = str[0];     str[n] = str[0];     for (i = 1; i < n; ++i) {       if (str[i] != a) {         b = i;         break;       } else {         s++;       }     }     if (b) {       for (i = n - 1; i >= b; --i) {         if (str[i] != a) {           c = i;           break;         } else {           s++;         }       }     }     if (b) {       min += s / 3;       a = str[b];       s = 1;       for (i = b + 1; i <= c; ++i) {         if (str[i] == a) {           s++;         } else {           a = str[i];           min += s / 3;           s = 1;         }       }       min += s / 3;       cout << min <<   n ;     } else {       min += (s + 2) / 3;       cout << min <<   n ;     }   }   void nhap() {     long long i;     cin >> n;     cin >> str;   }   int main() {     long long i;     cin >> t;     for (i = 1; i <= t; ++i) {       nhap();       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int main() {     scanf( %d%d , &n, &m);     if (n == 1 && m == 1) {       puts( YES n1 );       return 0;     }     if ((n == 1 && m <= 3) || (m == 1 && n <= 3) || (n == 2 && m <= 3) ||         (m == 2 && n <= 3)) {       puts( NO );       return 0;     }     if (n == 1 && m == 4) {       puts( YES n2 4 1 3 );       return 0;     }     if (n == 4 && m == 1) {       puts( YES n2 n4 n1 n3 );       return 0;     }     if (n == 1 && m >= 5) {       puts( YES );       for (int i = 1; i <= m; i += 2) printf( %d  , i);       for (int i = 2; i <= m; i += 2) printf( %d  , i);       return 0;     }     if (n >= 5 && m == 1) {       puts( YES );       for (int i = 1; i <= n; i += 2) printf( %d n , i);       for (int i = 2; i <= n; i += 2) printf( %d n , i);       return 0;     }     if (n == m && n == 3) {       puts( YES n6 1 8 n7 5 3 n2 9 4 );       return 0;     }     vector<vector<int> > g;     g.resize(n);     for (int i = 0, u = 1; i < n; i++) {       g[i].resize(m);       for (int j = 0; j < m; j++) g[i][j] = u++;     }     if (n <= m) {       for (int i = 0; i < n; i++)         if (i & 1) {           vector<int> tmp;           for (int j = 0; j < m; j++) tmp.push_back(g[i][(j + 2) % m]);           tmp.swap(g[i]);         }       for (int j = 0; j < m; j++)         if (j & 1) {           vector<int> tmp;           for (int i = 0; i < n; i++) tmp.push_back(g[(i - 1 + n) % n][j]);           for (int i = 0; i < n; i++) g[i][j] = tmp[i];         }     } else {       for (int i = 0; i < n; i++)         if (i & 1) {           vector<int> tmp;           for (int j = 0; j < m; j++) tmp.push_back(g[i][(j - 1 + m) % m]);           tmp.swap(g[i]);         }       for (int j = 0; j < m; j++)         if (j & 1) {           vector<int> tmp;           for (int i = 0; i < n; i++) tmp.push_back(g[(i + 2) % n][j]);           for (int i = 0; i < n; i++) g[i][j] = tmp[i];         }     }     puts( YES );     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) printf( %d  , g[i][j]);       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t = 0;     int l, r, n, m, x;     cin >> n >> x;     int sum;     int temp = 0;     for (int i = 1; i <= n; i++) {       cin >> l >> r;       sum = 0;       while (1) {         t = t + x;         if (t >= l) {           t = (t - x);           sum = sum + (r - t);           t = t + sum;           break;         }       }       temp = temp + sum;     }     cout << temp << endl;   }
#include <bits/stdc++.h>   int main() {     int n, k, l, c, d, p, nl, np, a, b, r;     scanf( %d %d %d %d %d %d %d %d , &n, &k, &l, &c, &d, &p, &nl, &np);     a = (k * l) / (n * nl);     b = c * d / n;     r = p / (n * np);     int x;     if ((a < 1) || (b < 1) || (r < 1))       x = 0;     else {       x = (a < b) ? ((a < r) ? a : r) : ((b < r) ? b : r);     }     printf( %d , x);     return 0;   }