malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(nullptr);     int n;     std::cin >> n;     std::vector<std::vector<std::pair<int, int>>> g(n);     for (int i = 1; i < n; ++i) {       int u, v, w;       std::cin >> u >> v >> w;       --u;       --v;       g[u].push_back(std::make_pair(v, w));       g[v].push_back(std::make_pair(u, w));     }     std::vector<int> sz(n);     std::vector<long long> w(n);     long long ans = 0;     std::function<void(int, int)> dfs = [&](int u, int fa) {       sz[u] = 1;       std::vector<int> adj;       for (auto p : g[u]) {         int v = p.first, l = p.second;         if (v == fa) {           continue;         }         w[v] += l * 2;         dfs(v, u);         w[u] += w[v];         sz[u] += sz[v];         ans += l * sz[v];         adj.push_back(v);       }       std::sort(adj.begin(), adj.end(),                 [&](int u, int v) { return w[u] * sz[v] < w[v] * sz[u]; });       long long sum = 0;       for (auto v : adj) {         ans += sum * sz[v];         sum += w[v];       }     };     dfs(0, -1);     std::cout << std::setprecision(20) << 1.0 * ans / (n - 1) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   void sherlock(int t) {     int n;     cin >> n;     string s;     cin >> s;     string t1, t2, ans;     int mink = 1;     ans = s;     for (int k = 1; k <= n; k++) {       t1 = s.substr(k - 1, n - k + 1);       t2 = s.substr(0, k - 1);       if ((n - k + 1) % 2 == 1) reverse(t2.begin(), t2.end());       t1 += t2;       if (t1 < ans) {         mink = k;         ans = t1;       }     }     cout << ans << endl << mink << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int tt;     cin >> tt;     for (int t = 1; t <= tt; t++) sherlock(t);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct dato {     int i, j, val;   };   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<dato> input(k);     for (int t = 0; t < k; t++) {       dato &d = input[t];       cin >> d.i >> d.j >> d.val;       d.i--;       d.j--;     }     long long int p;     cin >> p;     if (n > m) {       for (int t = 0; t < k; t++) {         dato &d = input[t];         swap(d.i, d.j);       }       swap(n, m);     }     vector<int> numcol(m, 0);     vector<int> prodcol(m, 1);     for (int t = 0; t < k; t++) {       dato &d = input[t];       numcol[d.j]++;       prodcol[d.j] *= d.val;     }     bool error = false;     for (int j = 0; j < m and not error; j++)       error = numcol[j] == n and prodcol[j] == 1;     if (error or n % 2 != m % 2)       cout << 0 << endl;     else {       int numllenas = 0;       for (int j = 0; j < m; j++)         if (numcol[j] == n) numllenas++;       int e = (n - 1) * (m - 1) - k + numllenas;       long long int r = 2;       long long int a = 1;       while (e > 0) {         if (e % 2 == 0) {           r = (r * r) % p;           e /= 2;         } else {           a = (a * r) % p;           e--;         }       }       cout << a << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   const int INF = 0x3f3f3f3f;   const long long int LINF = 0x3f3f3f3f3f3f3f3f;   const int NINF = -INF - 1;   const long long int mod = 1e9 + 7;   int pre[MAXN];   int find(int x) { return pre[x] == x ? x : pre[x] = find(pre[x]); }   int T, n, k;   char s[MAXN];   int cnt[MAXN][28];   int main() {     cin >> T;     while (T--) {       scanf( %d%d , &n, &k);       scanf( %s , s);       vector<int> vec;       for (int i = 0; i <= n + 1; i++) pre[i] = i;       for (int i = 0; i < n; i++) {         if (i + k < n) {           int a = find(i), b = find(i + k);           if (a != b) pre[a] = b;         }       }       for (int i = 0; i < n / 2; i++) {         int a = find(i), b = find(n - i - 1);         if (a != b) pre[a] = b;       }       for (int i = 0; i < n; i++) {         cnt[find(i)][s[i] -  a ]++;       }       int ans = 0;       for (int i = 0; i < n; i++) {         if (pre[i] != i) continue;         int mm = 0, tot = 0;         for (int j = 0; j < 26; j++) {           mm = max(cnt[i][j], mm);           tot += cnt[i][j];         }         for (int j = 0; j < 26; j++) cnt[i][j] = 0;         ans += tot - mm;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 16;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     vector<int> ans(maxn, -1);     ans[0] = 0;     for (int i = 1; i < maxn; ++i) {       for (auto j : vector<int>{4, 6, 9}) {         if (i >= j && ans[i - j] != -1) {           ans[i] = max(ans[i], ans[i - j] + 1);         }       }     }     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       int n;       cin >> n;       if (n < maxn) {         cout << ans[n] <<   n ;       } else {         int t = (n - maxn) / 4 + 1;         cout << t + ans[n - 4 * t] <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, by, ay;   double mxy, l, r, m, D, x1[100005], x2[100005], x[100005], y[100005], mx1, mx2;   bool b;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i];       if (y[i] > 0) ay = 1;       if (y[i] < 0) by = 1;       y[i] = abs(y[i]);       mxy = max(mxy, y[i]);     }     if (ay + by == 2) {       cout << -1 << endl;       return 0;     }     l = mxy / 2;     r = 1000000000000003;     for (int w = 0; w < 200; w++) {       b = 0;       mx1 = -1000000000000000;       mx2 = 1000000000000000;       m = (l + r) / 2;       for (int i = 0; i < n; i++) {         D = 2 * m * y[i] - y[i] * y[i];         if (D < 0) {           b = 1;           break;         }         x1[i] = x[i] - sqrt(D);         x2[i] = x[i] + sqrt(D);         mx1 = max(mx1, x1[i]);         mx2 = min(mx2, x2[i]);       }       if (mx1 > mx2 || b)         l = m;       else         r = m;     }     printf( %f , l);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000000 + 7;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n;     cin >> n;     n -= 10;     if (n >= 1 && n <= 11 && n != 10) {       cout << 4;     } else if (n == 10) {       cout << 15;     } else {       cout << 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long a[n];       long long i, j, k, l;       for (i = 0; i < n; i++) {         cin >> a[i];       }       long long diff;       bool value = true;       for (i = 0; i < n - 1; i++) {         diff = abs(a[i] - a[i + 1]);         if (diff == 1) {           continue;         }         if (diff > 1) {           if (a[i] > a[i + 1]) {             continue;           }           if (a[i] < a[i + 1]) {             cout <<  NO                   <<   n ;             value = false;             break;           }         }       }       if (value) {         cout <<  YES               <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int f[55], s[55];   long double p[55];   long double dp[55][5005];   bool vis[55][5005];   int main() {     int n, r;     scanf( %d%d , &n, &r);     for (int i = 1; i <= n; i++) {       scanf( %d%d%Lf , &f[i], &s[i], &p[i]);       p[i] /= 100;     }     long double lo = 0, hi = 1e18, ans = 0;     for (int i = 0; i < 200; i++) {       long double mid = (lo + hi) / 2;       memset(vis, 0, sizeof(vis));       memset(dp, 0, sizeof(dp));       vis[n + 1][0] = 1;       for (int i = n; i >= 1; i--) {         for (int j = 0; j <= r; j++) {           long double res = 0;           if (j >= s[i]) {             res += (f[i] + dp[i + 1][j - f[i]]) * p[i];             res += (s[i] + dp[i + 1][j - s[i]]) * (1 - p[i]);             res = min(res, mid);             dp[i][j] = res;             vis[i][j] = 1;           } else if (j >= f[i]) {             res += (f[i] + dp[i + 1][j - f[i]]) * p[i];             res += (s[i] + mid) * (1 - p[i]);             res = min(res, mid);             dp[i][j] = res;             vis[i][j] = 1;           } else {             res += (f[i] + mid) * p[i];             res += (s[i] + mid) * (1 - p[i]);             res = min(res, mid);             dp[i][j] = mid;             vis[i][j] = mid;           }         }       }       bool ok = 0;       for (int j = 0; j <= r; j++) {         if (!vis[1][j]) continue;         if (dp[1][j] < mid) ok = 1;       }       if (ok)         ans = mid, hi = mid;       else         lo = mid;     }     printf( %.20Lf n , ans);   }
#include <bits/stdc++.h>   using namespace std;   long long n, x, d;   int sad;   string s;   int main() {     cin >> n >> x;     while (n--) {       string s;       long long d;       cin >> s >> d;       if (s[0] ==  + ) {         x += d;       } else {         if (d <= x) {           x -= d;         } else           sad++;       }     }     cout << x <<     << sad <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k, s;     scanf( %I64d%I64d%I64d , &n, &k, &s);     if (k * (n - 1) < s || s < k)       printf( NO n );     else {       long long sum = s, step = k;       long long dis = n - 1;       printf( YES n );       long long now = 1;       for (int i = 1; i <= k; i++) {         if ((step - 1) <= sum - dis) {           if (now == 1) {             if (i == 1)               printf( %d , n);             else               printf(  %d , n);             now = n;           } else {             if (i == 1)               printf( %d , 1);             else               printf(  %d , 1);             now = 1;           }           step -= 1;           sum -= dis;         } else {           long long d = sum - step + 1;           if (now - d <= 0) {             if (i == 1)               printf( %d , d + now);             else               printf(  %d , d + now);             now = d + now;           } else {             if (i == 1)               printf( %d , now - d);             else               printf(  %d , now - d);             now = now - d;           }           step -= 1;           sum -= d;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int n;     cin >> n;     if (n % 2 == 0) return cout <<  NO , 0;     long long int arr[2 * n + 1];     long long int a = 1, b = n + 1;     for (long long int i = 1; i < 2 * n + 1; i += 2) {       if (a % 2) {         arr[a++] = i;         arr[b++] = i + 1;       } else {         arr[b++] = i;         arr[a++] = i + 1;       }     }     cout <<  YES n ;     for (long long int i = 1; i < 2 * n + 1; i++) cout << arr[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int a[100005], b[100005];   int main() {     int i, j, n, p, k;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b[i] = a[i];     }     sort(b + 1, b + 1 + n);     for (i = 1; i <= n; i++) {       if (b[i] != a[i]) break;     }     p = i;     if (i == n + 1) {       printf( yes n );       printf( 1 1 );       return 0;     }     for (j = n; j >= 1; j--) {       if (b[j] != a[j]) break;     }     k = j;     int flag = 0;     for (i = p, j = k; i <= k && j >= p; i++, j--) {       if (b[i] != a[j]) {         flag = 1;         break;       }     }     if (flag) {       printf( no n );     } else {       printf( yes n );       printf( %d %d n , p, k);     }   }
#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = numeric_limits<int>::max() / 2 - 1;   inline int run() {     int n, a, b, k;     cin >> n >> a >> b >> k;     string s;     cin >> s;     int cur = 0;     vector<int> ans;     for (int i = 0; i < n; ++i) {       if (s[i] ==  0 ) cur++;       if (s[i] ==  1 ) cur = 0;       if (cur == b) ans.push_back(i), cur = 0;     }     for (int i = 0; i < a - 1 && ans.size() > 1; ++i) {       ans.pop_back();     }     cout << ans.size() << endl;     for (auto i : ans) cout << i + 1 <<    ;     return 0;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       string s;       cin >> s;       bool ans = true;       for (long long i = 0; i < k; i++) {         char temp = s[i];         for (long long j = i; j < n; j += k) {           if (temp ==  ?  && s[j] != temp) {             temp = s[j];             s[i] = temp;           }           if (temp != s[j] && s[j] !=  ? ) {             ans = false;           }         }       }       long long cnt1 = 0, cnt0 = 0, cnt = 0;       for (long long i = 0; i < k; i++) {         if (s[i] ==  1 ) {           cnt1++;         } else if (s[i] ==  0 ) {           cnt0++;         } else {           cnt++;         }       }       long long x = cnt - abs(cnt0 - cnt1);       if (x < 0 || x % 2 == 1) {         ans = false;       }       if (ans) {         cout <<  YES               <<   n ;       } else {         cout <<  NO               <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   inline long long read() {     long long 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 << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   long long die = 0;   const double eps = 1e-8;   const long long mod = 1e9 + 7;   const long long N = 1e6 + 5;   long long n;   long long a[N];   long long x, y;   bool mp[N];   long long fac[N];   long long inv[N];   long long C(long long y, long long i) {     return fac[y] * inv[i] % mod * inv[y - i] % mod;   }   void sol1() {     x = y = 0;     for (long long i = (1); i <= (n); i++)       if (a[i] == -1) {         if (mp[i])           x++;         else           y++;       }     n = x + y;     inv[0] = inv[1] = fac[0] = 1;     for (long long i = (1); i <= (n); i++) fac[i] = fac[i - 1] * i % mod;     for (long long i = (2); i <= (n); i++)       inv[i] = (mod - mod / i) * inv[mod % i] % mod;     for (long long i = (2); i <= (n); i++) (inv[i] *= inv[i - 1]) %= mod;     long long ans = fac[n];     for (long long i = (1); i <= (y); i++)       (ans += ((i & 1) ? -1 : 1) * C(y, i) * fac[n - i] % mod) %= mod;     (ans += mod) %= mod;     printf( %I64d n , ans);   }   void rd() {     n = read();     for (long long i = (1); i <= (n); i++) {       a[i] = read();       if (a[i] != -1) mp[a[i]] = 1;     }     sol1();   }   signed main() { rd(); }
#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v;     vector<int>::iterator j;     int n, i;     cin >> n;     n = 2 * n;     int cnt = 0;     int x[n];     int cnt2 = 0;     for (i = 0; i < n; i++) {       cin >> x[i];       if (i % 2 == 0) {         if (x[i] < 0) {           cnt++;         } else if (x[i] > 0) {           cnt2++;         }       }     }     if (cnt <= 1 || cnt2 == 1) {       cout <<  Yes ;       return 0;     } else if (cnt == n / 2 || cnt2 == n / 2) {       cout <<  Yes ;       return 0;     }     cout <<  No ;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 5;   char s[MAXN];   int lst = 1, cnt = 1, len[MAXN];   int fa[MAXN], ch[MAXN][26];   int siz[MAXN], c[MAXN], ord[MAXN];   inline int read() {     int x = 0;     char ch = getchar();     while (!isdigit(ch)) ch = getchar();     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x;   }   inline void insert(int c) {     int p = lst, np = lst = ++cnt;     len[np] = len[p] + 1, siz[np] = 1;     while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];     if (!p) return fa[np] = 1, void();     int q = ch[p][c];     if (len[q] == len[p] + 1) return fa[np] = q, void();     int nq = ++cnt;     fa[nq] = fa[q], fa[q] = fa[np] = nq;     len[nq] = len[p] + 1, memcpy(ch[nq], ch[q], sizeof(ch[q]));     while (ch[p][c] == q) ch[p][c] = nq, p = fa[p];   }   inline void clear() {     lst = cnt = 1;     memset(c, 0, sizeof(c));     memset(fa, 0, sizeof(fa));     memset(ch, 0, sizeof(ch));     memset(siz, 0, sizeof(siz));   }   inline long long solve() {     long long ans = 0;     for (int i = 1; i <= cnt; i++) c[len[i]]++;     for (int i = 1; i <= cnt; i++) c[i] += c[i - 1];     for (int i = cnt; i; i--) ord[c[len[i]]--] = i;     for (int i = cnt; i; i--) siz[fa[ord[i]]] += siz[ord[i]];     for (int i = 1; i <= cnt; i++)       ans += 1ll * (len[i] - len[fa[i]]) * siz[i] * siz[i];     return ans;   }   int main() {     int t = read();     while (t--) {       clear();       scanf( %s , s + 1);       int n = strlen(s + 1);       for (int i = 1; i <= n; i++) insert(s[i] -  a );       printf( %lld n , solve());     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<pair<int, int>>> g;   vector<int> used;   vector<int> tout;   int timer = 0;   bool dfs(int v, int cost) {     timer++;     used[v] = 1;     for (auto to : g[v]) {       if (to.second > cost) {         if (used[to.first] == 0) {           if (!dfs(to.first, cost)) {             return false;           }         } else {           if (used[to.first] == 1) {             return false;           }         }       }     }     used[v] = 2;     ++timer;     tout[v] = timer;     return true;   }   struct edge {     int a, b, c, index;   };   int main() {     int n, m;     int a, b, c;     cin >> n >> m;     g.resize(n);     tout.resize(n);     vector<pair<int, int>> index;     vector<edge> E;     for (int i = 0; i < m; i++) {       cin >> a >> b >> c;       --a;       --b;       g[a].push_back(make_pair(b, c));       index.push_back(make_pair(i + 1, c));       edge e;       e.a = a;       e.b = b;       e.c = c;       e.index = i + 1;       E.push_back(e);     }     int L = -1;     int R = 1e9;     while (R - L > 1) {       int M = (R + L) / 2;       used.clear();       used.resize(n, 0);       bool q = true;       for (int i = 0; i < n; i++) {         if (used[i] == 0) {           if (!dfs(i, M)) {             q = false;             break;           }         }       }       if (q) {         R = M;       } else {         L = M;       }     }     int k = 0;     timer = 0;     used.clear();     used.resize(n, 0);     bool q = true;     for (int i = 0; i < n; i++) {       if (used[i] == 0) {         if (!dfs(i, R)) {           q = false;           break;         }       }     }     vector<int> ans;     for (int i = 0; i < m; i++) {       if (E[i].c <= R && tout[E[i].a] <= tout[E[i].b]) {         k += 1;         ans.push_back(E[i].index);       }     }     cout << R <<     << k << endl;     for (int i = 0; i < k; i++) {       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 210, M = 50 * 1000 + 10;   int G, S, n, m, par[N];   int get_par(int v) { return par[v] == v ? v : par[v] = get_par(par[v]); }   struct edge {     int v = 0, u = 0, g = 0, s = 0;     bool operator<(edge b) const {       return make_pair(make_pair(s, v), u) < make_pair(make_pair(b.s, b.v), b.u);     }   } e[M];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> m >> G >> S;     for (int i = 0; i < m; i++) cin >> e[i].v >> e[i].u >> e[i].g >> e[i].s;     sort(e, e + m, [](edge x, edge y) { return x.g < y.g; });     long long ans = 1LL << 62;     set<edge> E;     for (int i = 0; i < m; i++) {       E.insert(e[i]);       iota(par, par + n + 1, 0);       edge goodbye;       for (auto e : E) {         int v = e.v, u = e.u;         v = get_par(v), u = get_par(u);         if (v == u)           goodbye = e;         else           par[u] = v;       }       E.erase(goodbye);       if ((int)(E.size()) == n - 1)         ans = min(1LL * G * e[i].g + 1LL * S * (*--E.end()).s, ans);     }     cout << (ans < 1LL << 62 ? ans : -1) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, a[100001], b[100001], flag = 1;       cin >> n;       vector<int> v(2, 0);       for (int i = 0; i < n; i++) cin >> a[i];       for (int j = 0; j < n; j++) cin >> b[j];       for (int i = 0; i < n; i++) {         if (a[i] < b[i] && v[1] != 1) {           flag = 0;           break;         } else if (a[i] > b[i] && v[0] != -1) {           flag = 0;           break;         }         if (a[i] == 1) v[1] = 1;         if (a[i] == -1) v[0] = -1;       }       if (flag == 0)         cout <<  NO n ;       else         cout <<  YES n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int arr[10005];   bool readl[10005];   int read_pages(int ini) {     int curr = ini;     readl[ini] = 1;     while (curr != arr[curr]) {       curr = arr[curr];       readl[curr] = true;     }     return curr;   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> arr[i];     }     int days = 0;     for (int i = 1; i <= n; i++) {       int curr = read_pages(i);       for (int j = i; j <= curr; j++) {         if (!readl[j]) {           curr = max(read_pages(j), curr);         }       }       i = curr;       days++;     }     cout << days;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     set<char> ST;     for (char x : s) ST.insert(x);     if (k > n) {       for (int i = 0; i < n; i++) cout << s[i];       for (int i = n; i < k; i++) cout << *ST.begin();       return;     }     for (int i = k - 1; i >= 0; i--) {       if (s[i] < *ST.rbegin()) {         string f =   ;         for (int j = 0; j < i; j++) f += s[j];         char mnThatWorkz =  z ;         for (char c : ST) {           if (c > s[i] && mnThatWorkz > c) mnThatWorkz = c;         }         f += mnThatWorkz;         while ((int)f.size() < k) f += *ST.begin();         cout << f <<   n ;         return;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       solve();     }   }