malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chkmax(T &x, T y) {     return x < y ? x = y, true : false;   }   template <typename T>   bool chkmin(T &x, T y) {     return x > y ? x = y, true : false;   }   int readint() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   struct node {     int val, id;     node(int val = 0, int id = 0) : val(val), id(id) {}     bool operator<(const node c) const { return val < c.val; }   };   int n, m, g, r;   int a[10005];   int d[10005][1005];   bool vis[10005][1005];   priority_queue<node> q;   int id(int x, int y) { return (x - 1) * g + y - 1; }   void dij() {     memset(d, 0x3f, sizeof(d));     q.push(node(0, id(1, g)));     d[1][g] = 0;     while (!q.empty()) {       int tid = q.top().id;       q.pop();       pair<int, int> t = make_pair(tid / g + 1, tid % g + 1);       if (vis[t.first][t.second]) continue;       if (t.first == m) {         if (t.second == g)           printf( %d n , d[t.first][t.second] - r);         else           printf( %d n , d[t.first][t.second]);         return;       }       vis[t.first][t.second] = 1;       if (t.first > 1) {         int tx = t.first - 1, ty = t.second - a[t.first] + a[t.first - 1];         if (ty > 0) {           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first] - a[t.first - 1]))             q.push(node(-d[tx][ty], id(tx, ty)));         }         if (t.second == a[t.first] - a[t.first - 1]) {           ty = g;           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first] - a[t.first - 1] + r))             q.push(node(-d[tx][ty], id(tx, ty)));         }       }       if (t.first < m) {         int tx = t.first + 1, ty = t.second - a[t.first + 1] + a[t.first];         if (ty > 0) {           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first + 1] - a[t.first]))             q.push(node(-d[tx][ty], id(tx, ty)));         }         if (t.second == a[t.first + 1] - a[t.first]) {           ty = g;           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first + 1] - a[t.first] + r))             q.push(node(-d[tx][ty], id(tx, ty)));         }       }     }     printf( -1 n );   }   int main() {     n = readint();     m = readint();     for (int i = 1; i <= m; i++) a[i] = readint();     sort(a + 1, a + m + 1);     g = readint();     r = readint();     dij();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   bool vis[N];   pair<int, int> pt[N];   int dp[N];   int main() {     memset(dp, 0x3f, sizeof(dp));     int a, m, n;     cin >> a >> n >> m;     for (int i = 1; i <= n; i++) {       int l, r;       scanf( %d %d , &l, &r);       for (int j = l + 1; j <= r; j++) {         vis[j] = true;       }     }     for (int i = 1; i <= m; i++) {       scanf( %d %d , &pt[i].first, &pt[i].second);     }     sort(pt + 1, pt + 1 + m);     for (int i = 1; i <= pt[1].first; i++) {       if (vis[i] == true) {         puts( -1 );         return 0;       }     }     dp[0] = 0;     int cnt = 1;     for (int i = 1; i <= a; i++) {       while (cnt <= m && pt[cnt].first < i) cnt++;       if (vis[i] == false)         dp[i] = dp[i - 1];       else {         for (int j = 1; j < cnt; j++) {           dp[i] = min(dp[i], dp[pt[j].first] + pt[j].second * (i - pt[j].first));         }       }     }     printf( %d n , dp[a]);     return 0;   }
#include<bits/stdc++.h>   #define ll long long   #define R return   #define pb push_back   #define SI size()      using namespace std;      long long t,n,a[100030],ans,w[5030];      int main(){       cin >> t;       while(t--){           ans = 0;           memset(w,0,sizeof w);           cin >> n;           for(int i=0;i<n;i++)               cin >> a[i];           for(int i=0;i<n;i++){               if(a[i] - w[i] > 1)                   ans += a[i] - w[i] - 1;               if(a[i] - w[i] < 1)                   w[i+1] += 1 - a[i] + w[i];               if(a[i] > 1)                   for(int j=i+2;j<=min(i+a[i],n);j++) w[j]++;           }           cout << ans << endl;       }       return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:60000000 )   using namespace std;   const long double eps = 1e-9;   const int inf = (1 << 30) - 1;   const long long inf64 = ((long long)1 << 62) - 1;   const long double pi = 3.1415926535897932384626433832795;   const string task =   ;   template <class T>   T sqr(T x) {     return x * x;   }   int n, m;   int a[100500];   int last[100500];   char ans[100500];   int x[100500], y[100500];   void solve() {     scanf( %d%d , &n, &m);     for (int i = 0; i < (int)(m); i++) {       scanf( %d , &a[i]);       ans[i] =  N ;       last[i] = -1;     }     for (int i = 0; i < (int)(n - 1); i++) {       scanf( %d%d , &x[i], &y[i]);       x[i]--;       if (x[i] >= 0) last[x[i]] = i;     }     int s = 0;     bool first = 1;     for (int i = 0; i < (int)(n - 1); i++) {       if (first && y[i] == 1) {         first = 0;         int mn = inf;         for (int j = 0; j < (int)(m); j++)           if (a[j] <= s && last[j] < i) {             ans[j] =  Y ;             mn = min(mn, a[j]);           }         s -= mn;       }       if (x[i] != -1) {         a[x[i]]--;       } else {         s++;       }     }     cerr << s << endl;     for (int i = 0; i < (int)(m); i++)       if (a[i] <= s) ans[i] =  Y ;     for (int i = 0; i < (int)(m); i++) {       printf( %c , ans[i]);     }     printf(  n );   }   int main() {     int tt;     scanf( %d , &tt);     for (int ii = 0; ii < (int)(tt); ii++) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5, oo = 0x3f3f3f3f;   pair<int, int> stop[maxn];   int nxt[maxn];   int main(void) {     int d, n, m;     scanf( %d%d%d , &d, &n, &m);     int x, y;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       stop[i] = pair<int, int>(x, y);     }     stop[0] = pair<int, int>(0, 0);     stop[m + 1] = pair<int, int>(d, 0);     sort(stop, stop + m + 1);     stack<int> s;     memset(nxt, -1, sizeof(nxt));     for (int i = m; i >= 1; i--) {       while (!s.empty() && stop[i].second <= stop[s.top()].second) s.pop();       if (s.empty())         nxt[i] = m + 1;       else         nxt[i] = s.top();       s.push(i);     }     nxt[0] = s.top();     int after;     long long ans = 0;     int now = n;     int dist;     for (int i = 0; i <= m + 1; i++) {       if (now < 0) return puts( -1 ), 0;       dist = stop[nxt[i]].first - stop[i].first;       if (dist > now)         after = dist;       else         after = now;       if (after > n) after = n;       ans += (after - now) * 1ll * stop[i].second;       after -= stop[i + 1].first - stop[i].first;       now = after;     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool cmp(int a, int b) { return a > b; }   int h[2005];   int main() {     int i, n, a, b, ans = 0;     scanf( %d %d %d , &n, &a, &b);     for (i = 0; i < n; ++i) scanf( %d , &h[i]);     sort(h, h + n, cmp);     if (h[a - 1] > h[a]) ans = h[a - 1] - h[a];     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool debug = 0;   long long a[26], dp[26][26];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tc = 1;     while (tc--) {       string s;       cin >> s;       int n = s.length();       long long mx = 0;       for (int i = 0; i < n; i++) {         int x = s[i] -  a ;         for (int j = 0; j < 26; j++) {           dp[j][x] += a[j];           mx = mx > dp[j][x] ? mx : dp[j][x];         }         a[x]++;         mx = mx > a[x] ? mx : a[x];       }       cout << mx;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool pal(string& s) {     int j = s.size() - 1;     int i = 0;     while (i < j) {       if (s[i++] != s[j--]) return false;     }     return true;   }   int main() {     string s;     cin >> s;     int n = s.size();     for (int len = n; len >= 0; len--) {       for (int start = 0; start + len - 1 < n; start++) {         string sub = s.substr(start, len);         if (pal(sub))           continue;         else {           cout << len << endl;           return 0;         }       }     }     cout << 0 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1e9 + 7;   bool isPoTwo(long long int x) { return x && (!(x & (x - 1))); }   long long int power(long long int x, unsigned long long int y,                       long long int p) {     long long int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   void solve() {     long long int n, x, y, d;     cin >> n >> x >> y >> d;     long long int ans = INT_MAX, ans1 = INT_MAX, ans2 = INT_MAX;     if ((y - x) % d == 0) ans = (abs(y - x) / d);     if ((y - 1) % d == 0 || (n - y) % d == 0) {       if ((y - 1) % d == 0) {         ans1 = (y - 1) / d;         ans1 += ((x - 1) / d) + 1;       }       if ((n - y) % d == 0) {         ans2 = (n - y) / d;         ans2 += ((n - x) / d) + 1;       }       ans = min(ans, ans2);       ans = min(ans, ans1);     }     if (ans == INT_MAX) ans = -1;     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, mn = INT_MAX, ans = INT_MAX;     cin >> n;     map<int, int> mp;     map<int, int> mp_ans;     for (int i = 1; i <= n; i++) {       cin >> a;       if (mp[a] == 0)         mp[a] = i;       else {         if (a <= mn) {           mn = a;           if (mp_ans[a] != 0) {             ans = min(mp_ans[a], i - mp[a]);             mp_ans[a] = ans;           } else {             mp_ans[a] = i - mp[a];           }         }         mp[a] = i;       }     }     cout << mp_ans[mn];   }
#include <bits/stdc++.h>   using namespace std;   long long pow(int a, int b, long long MOD) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y);         if (x > MOD) x %= MOD;       }       y = (y * y);       if (y > MOD) y %= MOD;       b /= 2;     }     return x;   }   long long InverseEuler(int n, long long MOD) { return pow(n, MOD - 2, MOD); }   int hcf(int n1, int n2) {     if (n2 != 0)       return hcf(n2, n1 % n2);     else       return n1;   }   int p[100090] = {};   void prime() {     for (int i = 2; i * i <= 100009; i++)       if (p[i] == 0)         for (int j = i + i; j <= 100009; j += i) p[j] = 1;     p[0] = 1;     p[1] = 1;   }   int main() {     int a[100002];     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int b[100002], c[100002], f = -1;     for (int i = 1; i < n; i++) {       b[i] = abs(a[i] - a[i - 1]) * f;       f *= -1;     }     f = -1;     for (int i = 1; i < n; i++) {       c[i] = b[i] * -1;     }     long long sum = 0, mx = INT_MIN, k = 1;     for (int i = 1; i < n; i++) {       sum += b[i];       if (sum < 0) sum = 0;       mx = max(mx, sum);     }     sum = 0;     long long mx1 = INT_MIN, j = 1;     for (int i = 1; i < n; i++) {       sum += c[i];       if (sum < 0) sum = 0;       mx1 = max(mx1, sum);     }     cout << max(mx, mx1);   }
#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   int n, m;   int f[55][405][405], s[405];   void DP() {     int i, j, k, mx, my, ans = 0;     memset(f, 0, sizeof(f));     for (i = 1, s[0] = 0; i <= n; i++) {       scanf( %d , &s[i]);       s[i] += s[i - 1];     }     for (k = 2; k <= m; k++) {       mx = my = -INF;       for (i = 1; i <= n; i++) {         for (j = i; j <= n; j++)           f[k][i][j] = mx + s[j] - s[i - 1] > my - (s[j] - s[i - 1])                            ? mx + s[j] - s[i - 1]                            : my - (s[j] - s[i - 1]);         for (j = 1; j <= i; j++) {           mx = mx > f[k - 1][j][i] - (s[i] - s[j - 1])                    ? mx                    : f[k - 1][j][i] - (s[i] - s[j - 1]);           my = my > f[k - 1][j][i] + (s[i] - s[j - 1])                    ? my                    : f[k - 1][j][i] + (s[i] - s[j - 1]);         }       }     }     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++) ans = ans > f[m][i][j] ? ans : f[m][i][j];     printf( %d n , ans);   }   int main() {     while (~scanf( %d%d , &n, &m)) DP();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int fact(int n) {     long long ans = n;     for (int i = n - 1; i > 0; --i) {       ans = (ans * i) % MOD;     }     return ans;   }   int pow2(int n) {     long long ans = 1, b = 2;     while (n > 0) {       if (n % 2) {         ans = (ans * b) % MOD;       }       b = (b * b) % MOD;       n /= 2;     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     cout << (fact(n) - pow2(n - 1) + MOD) % MOD;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000 + 10;   vector<int> v[maxn];   int dp[maxn];   int a[10][maxn];   int main() {     int n, K;     cin >> n >> K;     for (int i = 0; i < K; i++) {       for (int j = 0; j < n; j++) scanf( %d , &a[i][j]), v[a[i][j]].push_back(j);     }     memset(dp, 0, sizeof(dp));     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < i; j++) {         int p = 0;         for (int k = 0; k < K; k++)           if (v[a[0][j]][k] > v[a[0][i]][k]) {             p = 1;             break;           }         if (p == 0) dp[i] = max(dp[i], dp[j]);       }       dp[i]++;       ans = max(ans, dp[i]);     }     printf( %d n , ans);     return 0;   }
#include <iostream>   #include <cmath>   #include <vector>   #include <algorithm>   #include <set>   #include <cstdio>   #include <map>   #include <queue>   #include <ctime>   #include <iomanip>   #include <stack>   #include <assert.h>   #include <unordered_map>      using namespace std;      #define F first   #define ll long long   #define S second   #define ld long double   #define all(x) x.begin(), x.end()   #define rep(i, n) for (int i = 0; i < (int)n; ++i)   #define PI acos(-1)      ll a, b, c, d, e, fl, maxn, cnt, k, s;   vector <ll> ar;            int main()   {       ios_base::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);       ll t = 1;       cin >> t;       while(t--)       {           cin >> a;           ll last_time = -1;           ll last_pos = 0;           ll nap = 0;           ll pos = 0;           ll ans = 0;           ll lst = 0, lsp = 0;           queue <pair <ll, ll> > q;           rep(i, a + 1)           {               ll p, p1;               if (i != a)                   cin >> p >> p1;               else                   p = (ll)1e15;               if (!q.empty())               {                   pair <ll, ll> pp = q.front();                   q.pop();                   ll st = last_pos + nap * (pp.F - last_time);                   ll fin = last_pos + nap * (min(p, lst) - last_time);                   //cout << i <<     << st <<     << fin <<     << pp.S <<   n ;                   if (min(st, fin) <= pp.S && max(st, fin) >= pp.S)                       ++ans;               }               if (i == a)                   continue;               if (p >= lst)               {                   last_time = p;                   last_pos = lsp;                   lsp = p1;                   lst = abs(last_pos - p1) + p;                   if (last_pos > p1)                       nap = -1;                   else                       nap = 1;               }               q.push({p, p1});           }           cout << ans <<   n ;       }   }
#include <bits/stdc++.h>   using namespace std;   long long a[1005], b[1005];   int main() {     int n, l, r;     cin >> n;     long long bans = 1, aans = 1, mx = 0;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     for (int l = 0; l < n; l++) {       for (int r = l; r < n; r++) {         aans = 0;         bans = 0;         for (int i = l; i <= r; i++) {           aans |= a[i];           bans |= b[i];           mx = max(mx, aans + bans);         }       }     }     cout << mx << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, col[111111], used[111111], size;   vector<int> g[111111], L, R, mat[111111];   void paint(int id, int c) {     col[id] = c;     c == 1 ? L.push_back(id) : R.push_back(id);     for (vector<int>::iterator ii = g[id].begin(); ii != g[id].end(); ii++)       if (!col[*ii]) paint(*ii, c ^ 3);   }   void construct() {     int now = ++size, cnt = 0;     for (vector<int>::iterator ii = L.begin(); ii != L.end(); ii++) {       if (used[*ii]) continue;       used[*ii] = now;       cnt++;       if (cnt == 3) {         cnt = 0;         now = ++size;       }     }     for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++) {       if (used[*ii]) continue;       used[*ii] = now;       cnt++;       if (cnt == 3) {         cnt = 0;         now = ++size;       }     }   }   void print() {     printf( YES n );     for (int i = 1; i <= n; i++) printf( %d  , used[i]);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }     for (int i = 1; i <= n; i++)       if (!col[i]) paint(i, 1);     if (L.size() % 3 == 0 && R.size() % 3 == 0) {       construct();       print();       return 0;     } else {       if (L.size() % 3 == 2 && R.size() % 3 == 1) L.swap(R);       sort(L.begin(), L.end());       sort(R.begin(), R.end());       for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());       for (vector<int>::iterator ii = L.begin(); ii != L.end(); ii++)         if (R.size() - g[*ii].size() > 1) {           int cnt = 0;           used[*ii] = 1;           vector<int>::iterator pp = g[*ii].begin();           for (vector<int>::iterator jj = R.begin(); jj != R.end() && cnt < 2;                jj++) {             while (pp != g[*ii].end() && *jj > *pp) pp++;             if (pp != g[*ii].end() && *pp == *jj) continue;             used[*jj] = 1;             cnt++;           }           size = 1;           construct();           print();           return 0;         }       for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++)         if (L.size() - g[*ii].size() > 1) {           int cnt = 0;           vector<int>::iterator pp = g[*ii].begin();           for (vector<int>::iterator jj = L.begin(); jj != L.end() && cnt < 2;                jj++) {             while (pp != g[*ii].end() && *jj > *pp) pp++;             if (pp != g[*ii].end() && *pp == *jj) continue;             cnt++;             mat[*ii].push_back(*jj);           }         }       for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++)         if (mat[*ii].size() > 1)           for (vector<int>::iterator jj = ii + 1; jj != R.end(); jj++)             if (mat[*jj].size() > 1) {               used[*ii] = used[mat[*ii][0]] = used[mat[*ii][1]] = 1;               used[*jj] = used[mat[*jj][0]] = used[mat[*jj][1]] = 2;               size = 2;               construct();               print();               return 0;             }     }     printf( NO n );   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long m, sum, p;     cin >> m;     for (int i = 5; i < 400006; i++) {       p = 5, sum = 0;       while (i / p) sum += i / p, p *= 5;       if (sum == m) {         cout <<  5 n               << i <<     << i + 1 <<     << i + 2 <<     << i + 3 <<     << i + 4;         return 0;       }     }     cout << 0;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using lint = long long;   using ulint = unsigned long long;   const int N = 1500 + 1;   int a[N];   int bit[N];   int back[N];   int lsb(int x) { return x & -x; }   int query(int r) {     int res = 0;     for (; r > 0; r -= lsb(r)) {       res = max(res, bit[r]);     }     return res;   }   void update(int r, int k, int id) {     for (; r < N; r += lsb(r)) {       if (k > bit[r]) {         bit[r] = k;         back[r] = id;       }     }   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];     }     map<int, vector<pair<int, int>>> sums;     for (int l = 1; l <= n; l++) {       for (int r = l; r <= n; r++) {         sums[a[r] - a[l - 1]].emplace_back(l, r);       }     }     int best = -1, best_sum = -1;     for (auto &sum : sums) {       fill(begin(bit), end(bit), 0);       auto &vec = sum.second;       for (int i = 0; i < vec.size(); i++) {         auto [l, r] = vec[i];         update(r, query(l - 1) + 1, i);       }       if (query(n) > best) {         best = query(n);         best_sum = sum.first;       }     }     cout << best <<   n ;     fill(begin(bit), end(bit), 0);     fill(begin(back), end(back), -1);     auto &vec = sums[best_sum];     for (int i = 0; i < vec.size(); i++) {       auto [l, r] = vec[i];       int k = query(l - 1) + 1;       update(r, k, i);       if (k == best) {         l = n + 1;         for (int j = r; j >= 0; j--) {           if (best > 0 && bit[j] == best && vec[back[j]].second < l) {             cout << vec[back[j]].first <<     << vec[back[j]].second <<   n ;             best--;             l = vec[back[j]].first;           }         }         break;       }     }   }