malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 10000;   const int M = 1000000;   const int inf = 1e9;   struct Edge {     int v, w, cost, next;   } g[M];   int head[N], st, ed, flow, cost, cnt;   void init() {     memset(head, -1, sizeof(head));     flow = cost = cnt = 0;   }   void addEdge(int u, int v, int w, int cost) {     g[cnt].v = v, g[cnt].w = w, g[cnt].cost = cost, g[cnt].next = head[u],     head[u] = cnt++;     g[cnt].v = u, g[cnt].w = 0, g[cnt].cost = -cost, g[cnt].next = head[v],     head[v] = cnt++;   }   bool spfaFlow(int n) {     vector<int> dis(n + 1, inf);     vector<bool> vis(n + 1, false);     vector<int> path(n + 1, -1);     dis[st] = 0;     vis[st] = true;     queue<int> q;     q.push(st);     while (!q.empty()) {       int u = q.front();       q.pop();       vis[u] = false;       for (int i = head[u]; ~i; i = g[i].next) {         int v = g[i].v;         if (g[i].w && dis[v] > dis[u] + g[i].cost) {           dis[v] = dis[u] + g[i].cost;           path[v] = i;           if (!vis[v]) {             vis[v] = true;             q.push(v);           }         }       }     }     int mxflow = inf;     if (path[ed] != -1) {       for (int u = ed; u != st; u = g[path[u] ^ 1].v) {         mxflow = min(mxflow, g[path[u]].w);       }       flow += mxflow;       for (int u = ed; u != st; u = g[path[u] ^ 1].v) {         g[path[u]].w -= mxflow;         g[path[u] ^ 1].w += mxflow;         cost += mxflow * g[path[u]].cost;       }     }     return path[ed] != -1;   }   int minCost(int n) {     while (spfaFlow(n))       ;     return cost;   }   int main(int argc, char *argv[]) {     int n;     cin >> n;     string s;     cin >> s;     vector<int> b(n + 1);     for (int i = 1; i <= n; i++) {       cin >> b[i];     }     int N = 26 + n * 26 + (n / 2) * 26 + (n / 2);     st = N + 1, ed = N + 2;     init();     vector<int> cnt(255);     for (auto c : s) {       cnt[c]++;     }     int Z = n * 26 + (n / 2) * 27;     for (int i =  a ; i <=  z ; i++) {       addEdge(st, Z + i -  a  + 1, cnt[i], 0);       for (int j = 1; j <= n; j++) {         addEdge(Z + i -  a  + 1, (i -  a ) * n + j, 1, i == s[j - 1] ? -b[j] : 0);       }     }     for (int i = 0; i < 26; i++) {       for (int j = 1; j <= n / 2; j++) {         int ux = i * n + j, uy = i * n + n + 1 - j;         addEdge(ux, n * 26 + (n / 2) * i + j, 1, 0);         addEdge(uy, n * 26 + (n / 2) * i + j, 1, 0);         addEdge(n * 26 + (n / 2) * i + j, n * 26 + (n / 2) * 26 + j, 1, 0);       }     }     for (int j = 1; j <= n / 2; j++) {       addEdge(n * 26 + (n / 2) * 26 + j, ed, 2, 0);     }     cout << -minCost(N + 2) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long double pi = 3.141592653589793238;   set<long long>::iterator it;   multiset<long long>::iterator itr;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long t;     t = 1;     while (t--) {       long long n;       cin >> n;       long long m, a, b;       cin >> m >> a >> b;       long long r = n % m;       cout << min(b * (r), a * (m - r)) << endl;     }   }
#include <bits/stdc++.h>   using ll = long long;   using namespace std;   const int MOD = 1e9 + 7;   map<int, vector<int>> nodecolor;   map<int, vector<int>> tree;   void dfs(vector<int>& vis, int node, vector<int>& color) {     int blue = 0, c = 0, red = 0;     if (color[node - 1] == 0) {       c += 1;     }     if (color[node - 1] == 1) {       red += 1;     }     if (color[node - 1] == 2) {       blue += 1;     }     for (auto x : tree[node]) {       if (vis[x] == 0) {         vis[x] = 1;         dfs(vis, x, color);         blue += nodecolor[x][0];         red += nodecolor[x][1];         c += nodecolor[x][2];       }     }     nodecolor[node] = {blue, red, c};   }   int dfs2(vector<int>& vis, int node, int b, int r, int c, int ans) {     int b1 = 0, r1 = 0, b2 = 0, r2 = 0;     for (auto x : tree[node]) {       if (vis[x] == 0) {         vis[x] = 1;         b1 = b - (nodecolor[x][0]);         b2 = nodecolor[x][0];         r1 = r - (nodecolor[x][1]);         r2 = nodecolor[x][1];         if ((b1 == 0 || r1 == 0) && (b2 == 0 || r2 == 0)) {           ans += 1;         }         ans += dfs2(vis, x, b, r, c, 0);       }     }     return ans;   }   int main(int argc, const char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, u, v;     cin >> n;     vector<int> color(n);     int b = 0, r = 0, c = 0;     for (int i = 0; i < n; i++) {       cin >> color[i];       b += (color[i] == 2);       c += (color[i] == 0);       r += (color[i] == 1);     }     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       tree[u].push_back(v);       tree[v].push_back(u);     }     int ans = 0;     vector<int> vis(n + 1, 0);     vis[1] = 1;     dfs(vis, 1, color);     for (int i = 0; i < n + 1; i++) {       vis[i] = 0;     }     vis[1] = 1;     ans = dfs2(vis, 1, b, r, c, 0);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long in() {     int32_t x;     scanf( %d , &x);     return x;   }   const long long maxn = 1e6 + 10;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 10;   long long dp[maxn];   long long ans[maxn];   int32_t main() {     dp[1] = 1;     for (long long i = 2; i < maxn; i++) dp[i] = dp[i / 2] * 2;     long long n = in();     if (n & 1) {       cout <<  NO n ;     } else {       long long nn = n;       while (n) {         long long maxPower = dp[n];         for (long long x = 1; x <= n - maxPower + 1; x++)           ans[maxPower - x] = maxPower + x - 1,                          ans[maxPower + x - 1] = maxPower - x;         n = 2 * maxPower - n - 2;       }       cout <<  YES n ;       for (long long i = 1; i <= nn; i++) cout << ans[i] <<    ;       cout <<   n ;       n = nn;     }     if (n <= 7) {       bool ok = false;       long long arr[7];       for (long long i = 0; i < n; i++) arr[i] = i + 1;       do {         bool fl = true;         for (long long i = 0; i < n; i++) {           if ((arr[i] & (i + 1)) && arr[i] != i + 1) continue;           fl = false;         }         if (fl) {           ok = true;           break;         }       } while (next_permutation(arr, arr + n));       if (ok) {         cout <<  YES n ;         for (long long i = 0; i < n; i++) cout << arr[i] <<    ;       } else         cout <<  NO n ;     } else {       if (__builtin_popcountll(n) == 1)         cout <<  NO n ;       else {         cout <<  YES n ;         long long arr[7];         for (long long i = 0; i < 7; i++) arr[i] = i + 1;         do {           bool fl = true;           for (long long i = 0; i < 7; i++) {             if ((arr[i] & (i + 1)) && arr[i] != i + 1) continue;             fl = false;           }           if (fl) break;         } while (next_permutation(arr, arr + 7));         for (long long i = 0; i < 7; i++) cout << arr[i] <<    ;         for (long long i = 8; i <= n; i *= 2) {           long long lim = min(2 * i - 1, n);           for (long long x = i; x <= lim; x++) {             if (x == i)               cout << lim <<    ;             else               cout << x - 1 <<    ;           }         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, t, ans = 0, i, a, b, m, x, y;     cin >> t;     while (t > 0) {       cin >> n;       vector<int> res;       long long es = 0, os = 0;       if ((n / 2) % 2 == 0) {         cout <<  YES  << endl;         for (i = 0; i < n / 2; i++) {           res.push_back(2 * (i + 1));           es += 2 * (i + 1);         }         for (i = 0; i < n / 2; i++) {           res.push_back(2 * (i + 1) - 1);           os += 2 * (i + 1) - 1;         }         res[n - 1] += es - os;         for (i = 0; i < n; i++) cout << res[i] <<    ;         cout << endl;       } else         cout <<  NO  << endl;       t--;     }   }
#include <bits/stdc++.h>   const int INF = 0x3f3f3f3f;   const int kN = 100 + 5;   const int MOD = (int)1e9 + 7;   int n, limit;   int graph[kN][kN];   std::vector<int> edges[kN];   int dp[kN][kN];   void dfs(int u, int fa) {     for (int w = 0; w < n; ++w)       if (graph[u][w] <= limit) dp[u][w] = 1;     for (int v : edges[u]) {       if (fa == v) continue;       dfs(v, u);     }     for (int w = 0; w < n; ++w) {       if (dp[u][w] == 0) continue;       for (int v : edges[u]) {         int slot = 0;         if (v == fa) continue;         for (int r = 0; r < n; ++r) {           if (dp[v][r] == 0) continue;           if (graph[u][r] < graph[u][w] || graph[u][r] == graph[u][w] && r < w)             continue;           if (graph[v][w] < graph[v][r] || graph[v][w] == graph[v][r] && w < r)             continue;           slot += dp[v][r];           if (slot >= MOD) slot -= MOD;         }         dp[u][w] = dp[u][w] * 1ll * slot % MOD;       }     }   }   int main() {     scanf( %d%d , &n, &limit);     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) graph[i][j] = i == j ? 0 : INF;     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       edges[a].emplace_back(b);       edges[b].emplace_back(a);       graph[a][b] = graph[b][a] = 1;     }     for (int k = 0; k < n; ++k)       for (int i = 0; i < n; ++i)         for (int j = 0; j < n; ++j)           graph[i][j] = std::min(graph[i][j], graph[i][k] + graph[k][j]);     dfs(0, -1);     int result = 0;     for (int v = 0; v < n; ++v) {       result = (result + dp[0][v]) % MOD;     }     printf( %d n , result);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1001;   int a[N][N], LR[N][N], RR[N][N], LC[N][N], RC[N][N];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       vector<int> v;       for (int j = 1; j <= m; ++j) {         cin >> a[i][j];         v.push_back(a[i][j]);       }       sort(v.begin(), v.end());       auto last = unique(v.begin(), v.end());       v.erase(last, v.end());       for (int j = 1; j <= m; ++j) {         int l = 0, r = (int)v.size() - 1;         while (l <= r) {           int m = (l + r) / 2;           if (v[m] >= a[i][j]) {             if (v[m] == a[i][j]) {               LR[i][j] = m;               RR[i][j] = (int)v.size() - 1 - m;             }             r = m - 1;           } else {             l = m + 1;           }         }       }     }     for (int i = 1; i <= m; ++i) {       set<int> s;       vector<int> v;       for (int j = 1; j <= n; ++j) {         v.push_back(a[j][i]);       }       sort(v.begin(), v.end());       auto last = unique(v.begin(), v.end());       v.erase(last, v.end());       for (int j = 1; j <= n; ++j) {         int l = 0, r = (int)v.size() - 1;         while (l <= r) {           int m = (l + r) / 2;           if (v[m] >= a[j][i]) {             if (v[m] == a[j][i]) {               LC[j][i] = m;               RC[j][i] = (int)v.size() - 1 - m;             }             r = m - 1;           } else {             l = m + 1;           }         }       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cout << max(LC[i][j], LR[i][j]) + 1 + max(RC[i][j], RR[i][j]) <<    ;       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   namespace fast_IO {   inline int read_int() {     register int ret = 0, f = 1;     register char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       ret = (ret << 1) + (ret << 3) + int(c - 48);       c = getchar();     }     return ret * f;   }   }  // namespace fast_IO   using namespace fast_IO;   int N, Q;   int a[200005];   char s[200005];   struct BIT {     int c[200005];     inline int lowbit(int x) { return x & (-x); }     inline void add(int pos, int w) {       while (pos <= N) c[pos] += w, pos += lowbit(pos);     }     inline int query(int pos) {       int res = 0;       while (pos) res += c[pos], pos -= lowbit(pos);       return res;     }   } BIT[3];   set<int> S[3];   inline int decode(char c) {     if (c ==  R ) return 0;     if (c ==  P ) return 1;     if (c ==  S ) return 2;   }   inline void init() {     N = read_int(), Q = read_int();     scanf( %s , s + 1);     for (register int i = 0; i < 3; i++)       S[i].insert(-0x3f3f3f3f), S[i].insert(0x3f3f3f3f);     for (register int i = 1; i <= N; i++) {       a[i] = decode(s[i]);       BIT[a[i]].add(i, 1);       S[a[i]].insert(i);     }   }   inline int calc() {     if (S[0].size() == N + 2 || S[1].size() == N + 2 || S[2].size() == N + 2)       return N;     if (S[0].size() == 2) return S[2].size() - 2;     if (S[1].size() == 2) return S[0].size() - 2;     if (S[2].size() == 2) return S[1].size() - 2;     set<int>::iterator it_min[3], it_max[3];     for (register int i = 0; i < 3; i++) it_min[i] = S[i].begin(), it_min[i]++;     for (register int i = 0; i < 3; i++)       it_max[i] = S[i].end(), it_max[i]--, it_max[i]--;     int ans = N;     for (register int i = 0; i < 3; i++) {       int l1 = (i + 1) % 3, r1 = (i + 2) % 3;       int L = *it_min[l1], R = *it_min[r1] - 1;       ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1));       L = *it_max[r1], R = *it_max[l1];       ans -= max(0, BIT[i].query(R) - BIT[i].query(L - 1));     }     return ans;   }   inline void ans() {     printf( %d n , calc());     register int pos, nw;     static char opt[20];     while (Q--) {       pos = read_int();       scanf( %s , opt);       nw = decode(opt[0]);       S[a[pos]].erase(pos);       BIT[a[pos]].add(pos, -1);       a[pos] = nw, S[a[pos]].insert(pos), BIT[a[pos]].add(pos, 1);       printf( %d n , calc());     }   }   int main() {     init();     ans();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxx = 500 + 7;   const int maxn = 100 + 7;   const long long mod = 1E9 + 7;   const int maxs = 2 * (1E7) + 5;   const int maxm = 100 + 3;   const long long maxk = 1e13;   long double dp[maxn][maxn];   int last[1000];   char seq[maxn];   long long n, k;   void init() {     cin >> n >> k;     cin >> seq;     memset(last, -1, sizeof(last));   }   void solve() {     dp[0][0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= i; j++) {         if (!j) {           dp[i][j] = 1;           continue;         }         dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];         if (last[seq[i - 1]] != -1) dp[i][j] -= dp[last[seq[i - 1]]][j - 1];       }       last[seq[i - 1]] = i - 1;     }     long long cost = 0;     long double kk = k;     for (long long i = n; i >= 0; i--) {       long double tt = min(kk, dp[n][i]);       cost += tt * (n - i);       kk -= min(tt, kk);     }     if (kk > 0)       cout << -1 << endl;     else       cout << (long long)cost << endl;   }   int main(void) {     init();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T& x) {     static char c;     static int f;     for (c = getchar(), f = 1; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = -f;     for (x = 0; c >=  0  && c <=  9 ; c = getchar()) x = x * 10 + (c & 15);     x *= f;   }   template <typename T>   void write(T x) {     static char q[65];     int cnt = 0;     if (x < 0) putchar( - ), x = -x;     q[++cnt] = x % 10, x /= 10;     while (x) q[++cnt] = x % 10, x /= 10;     while (cnt) putchar(q[cnt--] +  0 );   }   const int mod = 1000000007, maxn = 1000005;   int mo(const int x) { return x >= mod ? x - mod : x; }   int iac[maxn], fac[maxn], _n[maxn], _m[maxn];   int main() {     int n, m, a, b;     read(n), read(m), read(a), read(b);     int mx = max(n, m);     iac[0] = iac[1] = fac[0] = fac[1] = _n[1] = _m[0] = 1;     _m[1] = m;     for (int i = 2; i <= mx; ++i)       iac[i] = 1ll * (mod - mod / i) * iac[mod % i] % mod;     _n[0] = iac[n];     for (int i = 2; i <= mx; ++i)       iac[i] = 1ll * iac[i - 1] * iac[i] % mod,       fac[i] = 1ll * fac[i - 1] * i % mod, _m[i] = 1ll * _m[i - 1] * m % mod,       _n[i] = 1ll * _n[i - 1] * n % mod;     int ans = 0;     for (int i = 1; i <= n - 1 && i <= m; ++i)       ans = mo(ans + 1ll * (i + 1) * _n[n - 1 - i] % mod * _m[n - 1 - i] % mod *                          iac[n - 1 - i] % mod * iac[m - i] % mod * iac[i - 1] %                          mod);     ans = 1ll * ans * fac[n - 2] % mod * fac[m - 1] % mod;     write(ans), putchar(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int Set(int N, int pos) { return N = N | (1 << pos); }   int reset(int N, int pos) { return N = N & ~(1 << pos); }   bool check(int N, int pos) { return (bool)(N & (1 << pos)); }   string st, st2, st3, st4, ans;   int main() {     ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);     int i, j, a, b, n, m, x, y, ck, t, r, rem, cnt, lo, hi, sum, cs = 1;     cin >> n >> m;     cin >> st;     a = b = m / 2;     for (i = 0; i < n; i++) {       if (st[i] ==  (  && a > 0) ans +=  ( , a--;       if (st[i] ==  )  && b > 0) ans +=  ) , b--;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int ans = -1;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  R ) {           ans = i + 1;           break;         }       }       if (ans == -1) {         cout << s.size() + 1 << endl;         continue;       } else {         int last = ans - 1;         for (int i = ans; i < s.size(); i++) {           if (s[i] ==  R ) {             ans = max(i - last, ans);             last = i;           }         }         ans = max(ans, int(s.size()) - last);       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxk = 15;   const int maxn = 1e5 + 10;   long long D[maxk][maxn];   long long queryBIT(int k, int x) {     long long ret = 0;     while (x > 0) {       ret += D[k][x];       x -= x & -x;     }     return ret;   }   void addBIT(int k, int x, long long d) {     while (x < maxn) {       D[k][x] += d;       x += x & -x;     }   }   int main() {     int n, k;     cin >> n >> k;     while (n--) {       int a;       cin >> a;       for (int j = 11; j > 0; j--) {         addBIT(j, a, queryBIT(j - 1, a));       }       addBIT(0, a, 1);     }     cout << queryBIT(k, maxn - 1) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   vector<int> g[maxn];   int src[maxn], dest[maxn];   bool vis[maxn];   int main() {     int n, m;     int a, b;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d %d , &a, &b);       a--, b--;       g[a].push_back(b);       g[b].push_back(a);     }     queue<int> Q;     for (int i = 0; i < n; i++) {       scanf( %d , &dest[i]);       if (dest[i] == 0) {         Q.push(i);       }     }     int ans = 0;     while (!Q.empty()) {       int i = Q.front();       Q.pop();       if (dest[i] == src[i]) {         vis[i] = true;         ans++;         src[i]++;         int len = g[i].size();         for (int j = 0; j < len; j++) {           src[g[i][j]]++;           if (src[g[i][j]] == dest[g[i][j]]) {             Q.push(g[i][j]);           }         }       }     }     printf( %d n , ans);     for (int i = 0; i < n; i++) {       if (vis[i]) {         ans--;         printf( %d%c , i + 1, ans ?     :   n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int ls, cc;   char s[1111111], a[1111111];   int main() {     gets(s), ls = strlen(s), cc = 0, a[0] =  # ;     for (int i = 0; i < ls; ++i) {       a[++cc] = s[i];       while (cc > 1 && a[cc - 1] != a[cc]) cc -= 2;     }     for (int i = 1; i <= cc; ++i) putchar(a[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int sz = 100005;   int a[sz], b[sz], sum[sz];   int main() {     int n, m, c;     cin >> n >> m >> c;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) {       cin >> b[i];       sum[i] = (sum[i - 1] + b[i]) % c;     }     for (int i = 1; i <= n; i++) {       int l, r;       r = min(m, i);       l = max(1, m - (n - i + 1) + 1);       if (l <= r) a[i] += (sum[r] - sum[l - 1] + 3 * c);       a[i] %= c;       cout << a[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 105;   int n, res = 1e9, a[5][N][N];   void calc(int a[N][N], int b[N][N], int c[N][N], int d[N][N]) {     int tmp = 0, tmp2 = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         if (a[i][j] != (i + j) % 2)           ++tmp;         else           ++tmp2;         if (c[i][j] != (i + j) % 2)           ++tmp;         else           ++tmp2;         if (b[i][j] != (i + j) % 2)           ++tmp2;         else           ++tmp;         if (d[i][j] != (i + j) % 2)           ++tmp2;         else           ++tmp;       }     res = min(res, min(tmp, tmp2));   }   int main() {     cin >> n;     for (int i = 1; i <= 4; ++i)       for (int j = 1; j <= n; ++j)         for (int k = 1; k <= n; ++k) {           char z;           cin >> z;           a[i][j][k] = (int)(z -  0 );         }     vector<int> v;     v.push_back(1);     v.push_back(2);     v.push_back(3);     v.push_back(4);     do {       calc(a[v[0]], a[v[1]], a[v[2]], a[v[3]]);     } while (next_permutation(v.begin(), v.end()));     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   string name[101];   vector<string> seen;   bool see = false;   int m, n, i, j;   int main() {     cin >> n;     m = n;     i = 1;     int k = 0;     while (n > 0) {       cin >> name[i];       i++;       n--;     }     for (i = 1; i <= m; i++) {       see = false;       for (vector<string>::iterator it = seen.begin(); it != seen.end(); ++it) {         if (name[i] == *it) {           cout <<  YES  << endl;           see = true;           break;         }       }       if (see == false) {         cout <<  NO  << endl;         see = false;         seen.push_back(name[i]);       }     }     return 0;   }