malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using LL = long long;   const LL Inf = (LL)1e18;   const int N = 5000 + 5;   int s, e, n;   LL x[N], a[N], b[N], c[N], d[N];   LL dp[2][N];   inline LL lo(int p) { return d[p] - x[p]; }   inline LL ro(int p) { return x[p] + c[p]; }   inline LL li(int p) { return b[p] - x[p]; }   inline LL ri(int p) { return x[p] + a[p]; }   inline void _min(LL &a, LL b) {     if (a > b) a = b;   }   LL work() {     if (s > e) {       for (int i = 1; i <= n; ++i) {         x[i] = -x[i];         std::swap(a[i], b[i]);         std::swap(c[i], d[i]);       }       for (int i = 1; i < n + 1 - i; ++i) {         std::swap(x[i], x[n + 1 - i]);         std::swap(a[i], a[n + 1 - i]);         std::swap(b[i], b[n + 1 - i]);         std::swap(c[i], c[n + 1 - i]);         std::swap(d[i], d[n + 1 - i]);       }       s = n + 1 - s;       e = n + 1 - e;     }     for (int i = 0; i < 2; ++i) {       for (int j = 0; j < N; ++j) {         dp[i][j] = Inf;       }     }     int cur = 0, nex = 1;     dp[cur][1] = li(1) + lo(1);     for (int i = 2; i <= n; ++i) {       for (int left = 1; left <= i - 1; ++left) {         if (dp[cur][left] == Inf) continue;         for (int q = 0; q < 2; ++q) {           for (int w = 0; w < 2; ++w) {             if (i == s && w == 0) continue;             if (i == e && q == 1) continue;             if (s < i && i < e) {               if (left == 1 && q == 0) continue;             }             LL cost = 0;             cost += q == 0 ? ro(i) : lo(i);             cost += w == 0 ? ri(i) : li(i);             int n_left = left;             if (q == w) {               if (q == 0)                 n_left--;               else                 n_left++;             }             _min(dp[nex][n_left], dp[cur][left] + cost);           }         }       }       std::swap(cur, nex);       std::fill(dp[nex], dp[nex] + N, Inf);     }     LL ret = dp[cur][0];     if (s < e) {       ret -= li(s) + ro(e);     } else {       ret -= ri(s) + lo(e);     }     return ret;   }   int main() {     std::ios::sync_with_stdio(false);     std::cin >> n >> s >> e;     for (int i = 1; i <= n; ++i) std::cin >> x[i];     for (int i = 1; i <= n; ++i) std::cin >> a[i];     for (int i = 1; i <= n; ++i) std::cin >> b[i];     for (int i = 1; i <= n; ++i) std::cin >> c[i];     for (int i = 1; i <= n; ++i) std::cin >> d[i];     std::cout << work() << std::endl;   }
#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g;   bitset<(int)1e6> us;   bool isCyclic(int v, int p = -1) {     us.set(v);     for (int i = 0; i < g[v].size(); i++) {       if (us.test(g[v][i]) && g[v][i] != p) return 0;       if (us.test(g[v][i])) continue;       bool cures = isCyclic(g[v][i], v);       if (!cures) return 0;     }     return 1;   }   int main() {     int n, m;     cin >> n >> m;     g.resize(n + 1);     for (int i = 0; i < m; i++) {       int v, to;       cin >> v >> to;       g[to].push_back(v);       g[v].push_back(to);     }     int sum = 0;     for (int i = 1; i <= n; i++) {       if (us.test(i)) continue;       sum += isCyclic(i);     }     cout << sum << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k;   long long num[5005], sum[5005], dp[5005][5005];   int main() {     int i, j;     while (scanf( %d%d%d , &n, &m, &k) != EOF) {       for (i = 0; i < n; i++) {         scanf( %I64d , &num[i]);       }       memset(sum, 0, sizeof(sum));       for (i = 0; i <= m - 1; i++) {         sum[0] += num[i];       }       for (i = 1; i + m <= n; i++) {         sum[i] = sum[i - 1];         sum[i] -= num[i - 1];         sum[i] += num[i + m - 1];       }       memset(dp, 0, sizeof(dp));       for (j = 1; j <= k; j++) {         for (i = n - m * j; i >= 0; i--) {           dp[i][j] = max(dp[i + 1][j], dp[i + m][j - 1] + sum[i]);         }       }       long long res = 0;       for (i = 0; i <= n - m * k; i++) {         res = max(dp[i][k], res);       }       cout << res << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const int N = 200005;   int vis[N], tab[N], i, a, b, sum, n, m, vis0[N], vis1[N];   vector<int> graf0[N], graf1[N];   string s;   bool cyc(int w) {     vis[w] = 1;     for (auto& w2 : graf0[w])       if (vis[w2] == 1)         return true;       else if (vis[w2] == 0 && cyc(w2))         return true;     vis[w] = 2;     return false;   }   void go(int w, vector<int> graf[N], int* vis) {     tab[w] = 1;     vis[w] = 1;     for (auto& w2 : graf[w])       if (!vis[w2]) go(w2, graf, vis);   }   int main() {     scanf( %d %d , &n, &m);     while (m--) {       scanf( %d %d , &a, &b);       graf0[a].push_back(b);       graf1[b].push_back(a);     }     for (i = 1; i <= n; i++)       if (vis[i] == 0 && cyc(i)) {         printf( -1 n );         return 0;       }     sum = 0;     for (i = 1; i <= n; i++)       if (tab[i] == 0) {         go(i, graf0, vis0);         go(i, graf1, vis1);         s +=  A ;         sum++;       } else {         s +=  E ;         go(i, graf0, vis0);         go(i, graf1, vis1);       }     printf( %d n%s n , sum, s.c_str());     return 0;   }
#include <bits/stdc++.h>   using ll = long long;   using ld = long double;   using namespace std;   const int MAXN = 2000228;   const ll INF = (1ll << 50);   set<pair<ll, ll>> s;   void ins(ll x, ll y) {}   int main() {     ios_base::sync_with_stdio(false);     cout.setf(ios::fixed);     cout.precision(20);     cout.tie(nullptr);     cin.tie(nullptr);     int n;     cin >> n;     ll p = INF;     s.emplace(p - 1, 0);     for (int i = 0; i < n; ++i) {       ll x;       cin >> x;       if (x >= p) {         continue;       }       p = x;       vector<pair<ll, ll>> rmed;       while (!s.empty()) {         auto val = *s.rbegin();         if (val.first < x - 1) {           break;         }         s.erase(val);         rmed.push_back(val);       }       pair<ll, ll> tmp{x, -1};       s.emplace(tmp);       ll last = 0;       for (auto& val : rmed) {         ll dv = val.first / x;         last = max(last, val.second + x * (dv - 1) * i);         ll md = val.first % x;         ll nvl = x * dv * i + val.second;         auto it = s.lower_bound(pair<ll, ll>{md, nvl});         if (it->second >= nvl) {           continue;         }         auto lit = it;         while (lit != s.begin()) {           auto nit = lit;           --nit;           if (nit->second > nvl) {             break;           }           lit = nit;         }         s.erase(lit, it);         s.emplace(md, nvl);       }       while (!s.empty() && s.rbegin()->second <= last) {         auto it = s.end();         --it;         s.erase(it);       }       s.emplace(x - 1, last);     }     ll ans = 0;     for (auto& val : s) {       ans = max(ans, val.first * n + val.second);     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t-- > 0) {       int n;       cin >> n;       vector<vector<int>> a;       int b[n + 1];       for (int i = 0; i <= n; i++) b[i] = 0;       int count = 0;       int s = 0;       for (int i = 0; i < n; i++) {         vector<int> x;         int k;         cin >> k;         int c = 0;         for (int j = 0; j < k; j++) {           int q;           cin >> q;           x.push_back(q);           if (c == 0) {             if (b[q] == 0) {               b[q]++;               count++;               c = 1;             }           }         }         if (c == 0) s = i;         a.push_back(x);       }       if (count == n) {         cout <<  OPTIMAL  << endl;       } else {         s++;         int f = 0;         for (int i = 0; i < n; i++) {           if (b[i + 1] == 0) {             f = i;           }         }         f++;         cout <<  IMPROVE  << endl;         cout << s <<     << f << endl;       }     }     return 0;   }
//Hare Krishna   //author: Dipjoy Basak   //dip_10      #include <bits/stdc++.h>   using namespace std;      #define endl   n    #define ll long long   #define int ll   #define ld long double   #define pb push_back   #define mp make_pair   #define ff first   #define ss ssecond   #define vi vector<int>   #define pi pair<int, int>   #define vpi vector<pair<int, int>>   #define rep(i, l, r) for (int i = l; i <= r; i++)   #define rrep(i, r, l) for (int i = r; i >= l; i--)   #ifndef ONLINE_JUDGE   #define debug(x)              cerr << #x <<    ;        _print(x);                cerr << endl;   #else   #define debug(x) ;   #endif   #define maxn 1000005   #define INF INT_MAX   const int mod = 1000000007;      void _print(int t) { cerr << t; }   void _print(string t) { cerr << t; }   void _print(char t) { cerr << t; }   void _print(ld t) { cerr << t; }   void _print(double t) { cerr << t; }      template <class T, class V>   void _print(pair<T, V> p);   template <class T>   void _print(vector<T> v);   template <class T>   void _print(set<T> v);   template <class T, class V>   void _print(map<T, V> v);   template <class T>   void _print(multiset<T> v);   template <class T, class V>   void _print(pair<T, V> p)   {       cerr <<  { ;       _print(p.ff);       cerr <<  , ;       _print(p.ss);       cerr <<  } ;   }   template <class T>   void _print(vector<T> v)   {       cerr <<  [  ;       for (T i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }   template <class T>   void _print(set<T> v)   {       cerr <<  [  ;       for (T i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }   template <class T>   void _print(multiset<T> v)   {       cerr <<  [  ;       for (T i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }   template <class T, class V>   void _print(map<T, V> v)   {       cerr <<  [  ;       for (auto i : v)       {           _print(i);           cerr <<    ;       }       cerr <<  ] ;   }      //---------------------------------------------------------------------------------------------------------------------------------------      void solve()   {       int n;       cin >> n;       int a[n];       int idx = -1, mn = INT_MAX;       rep(i, 0, n - 1)       {           cin >> a[i];           if (a[i] < mn)           {               mn = a[i];               idx = i;           }       }       cout << n - 1 << endl;       int cnt = 1;       rep(i, idx + 1, n - 1)       {           cout << idx + 1 <<     << i + 1 <<     << mn <<     << mn + cnt << endl;           cnt++;       }       cnt = 1;       rrep(i, idx - 1, 0)       {           cout << idx + 1 <<     << i + 1 <<     << mn <<     << mn + cnt << endl;           cnt++;       }   }   int32_t main()   {       ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);       cout << setprecision(15) << fixed;       int t;       t = 1;       cin >> t;       while (t--)       {           solve();       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   double dp[15][15];   int n, t, i, j, ans = 0;   void work() {     double t;     dp[1][1] += 1;     for (i = 1; i <= n; i++) {       for (j = 1; j <= i; j++) {         if (dp[i][j] > 1) {           t = dp[i][j] - 1;           dp[i][j] = 1;           dp[i + 1][j] += t / 2;           dp[i + 1][j + 1] += t / 2;         }       }     }   }   int main() {     scanf( %d%d , &n, &t);     while (t--) {       work();     }     for (i = 1; i <= n; i++) {       for (j = 1; j <= i; j++) {         if (abs(1 - dp[i][j]) < 0.0000001) {           ans++;         }       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   const double pi = 3.14159265358979323846;   using namespace std;   long long fastexp(long long a, long long b) {     long long ans = 1;     long long temp = a;     while (b > 0) {       if (b & 1) ans *= (temp);       temp *= temp;       b >>= 1;     }     return ans;   }   long long GCD(long long a, long long b) {     if (a == 0) return b;     return GCD(b % a, a);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     long long* A = new long long[n];     for (long long i = 0; i < n; i++) cin >> A[i];     long long m;     cin >> m;     long long* B = new long long[m];     for (long long i = 0; i < m; i++) cin >> B[i];     long long count = 0;     long long i = 0;     long long j = 0;     bool f = true;     while (i < n && j < m) {       long long cum1 = A[i++];       long long cum2 = B[j++];       bool flag = false;       while (i < n && j < m) {         if (cum1 == cum2) {           flag = true;           break;         }         if (cum1 > cum2) {           cum2 += B[j];           j++;         } else {           cum1 += A[i];           i++;         }       }       if (i == n) {         while (j < m) cum2 += B[j++];       }       if (j == m) {         while (i < n) cum1 += A[i++];       }       if (cum1 == cum2) flag = true;       if (!flag) {         f = false;         break;       }       count++;     }     if (!f)       cout <<  -1             <<   n ;     else       cout << count <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int x = 0;     for (int i = 2; i <= n; ++i) {       if (i & 1)         x = (x * 3 - 3) % 1000000007;       else         x = (x * 3 + 3) % 1000000007;     }     cout << x << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N;   char s[500010];   int c[30][500010];   int then(int x) { return x & -x; }   void in(int o, int x, int k) {     while (x <= N) {       c[o][x] += k;       x += then(x);     }   }   int out(int o, int x) {     int re = 0;     while (x > 0) {       re += c[o][x];       x -= then(x);     }     return re;   }   int main() {     int Q;     scanf( %s%d , s + 1, &Q);     N = strlen(s + 1);     for (int i = 1; i <= N; i++) in(s[i] -  a , i, 1);     for (int i = 1; i <= Q; i++) {       int o;       scanf( %d , &o);       if (o == 1) {         int k;         char m;         scanf( %d %c , &k, &m);         in(s[k] -  a , k, -1);         in(m -  a , k, 1);         s[k] = m;       } else {         int x, y;         scanf( %d%d , &x, &y);         int give = 0;         for (int i = 0; i <= 25; i++)           if (out(i, y) - out(i, x - 1)) give++;         printf( %d n , give);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s =   ;     vector<int> fib;     fib.push_back(1);     fib.push_back(1);     for (int i = 2; i < 17; ++i) {       fib.push_back(fib[i - 1] + fib[i - 2]);     }     for (int i = 1; i <= n; ++i) {       if (binary_search(fib.begin(), fib.end(), i) == 1) {         s.append(1u,  O );       } else {         s.append(1u,  o );       }     }     cout << s <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     int l = b.size();     int st = a.size() - 1;     int ar[l + 3];     memset(ar, -1, sizeof ar);     ar[l] = st + 1;     for (int i = l - 1; i >= 0; i--) {       while (st >= 0 && a[st] != b[i]) {         st--;       }       if (st >= 0) {         ar[i] = st;         st--;       } else         break;     }     int ans = 0;     int pre = 0;     for (int i = 0; i < a.size(); i++) {       int la = ar[pre];       if (la != -1) ans = max(ans, la - i);       if (pre < l && a[i] == b[pre]) {         pre++;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 60 + 10, M = (1 << 16) + 10, MOD = 1e9 + 7,                       INF = 2e9 + 10;   const long double PI = acos(-1);   int n, a[N], in[N], fac[N], msk[N], all;   long long int dp[M][N], ans = 1;   vector<int> adj[N], S, T;   bool mrk[N], ad[N][N];   long long int Power(long long int x, long long int y) {     if (y == 0) {       return 1ll;     }     return ((y % 2 ? x : 1) * Power((x * x) % MOD, y / 2)) % MOD;   }   void Dfs(int u) {     mrk[u] = 1;     if (in[u])       T.push_back(u);     else       S.push_back(u);     for (int v : adj[u]) {       if (!mrk[v]) {         Dfs(v);       }     }     return;   }   void Solve() {     memset(dp, 0, sizeof(dp));     int t = T.size(), s = S.size();     if (t < 2) return;     for (int i = 0; i < t; i++) {       msk[i] = 0;       for (int j = 0; j < s; j++) {         if ((a[T[i]] % a[S[j]]) == 0) {           msk[i] |= (1 << j);         }       }       dp[msk[i]][1]++;     }     for (int k = 1; k < t; k++) {       for (int mask = 1; mask < (1 << s); mask++) {         int cnt = 0;         for (int i = 0; i < t; i++) {           if ((msk[i] | mask) == mask) {             cnt++;           } else if (0 < (msk[i] & mask)) {             dp[mask | msk[i]][k + 1] += dp[mask][k];             dp[mask | msk[i]][k + 1] %= MOD;           }         }         if (k < cnt) {           dp[mask][k + 1] += (cnt - k) * dp[mask][k];           dp[mask][k + 1] %= MOD;         }       }     }     ans = (ans * dp[(1 << s) - 1][t]) % MOD;     ans = (ans * Power(fac[t - 1], MOD - 2)) % MOD;     all += (t - 1);     return;   }   void Dfs_All() {     for (int i = 0; i < n; i++) {       if (!mrk[i]) {         Dfs(i);         Solve();         S.clear();         T.clear();       }     }     return;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     fac[0] = 1;     for (int i = 1; i < N; i++) {       fac[i] = (fac[i - 1] * i) % MOD;     }     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       for (int j = 0; j < i; j++) {         int u = i, v = j;         if (a[v] < a[u]) swap(u, v);         if ((a[v] % a[u]) == 0) {           adj[u].push_back(v);           adj[v].push_back(u);           in[v]++;         }       }     }     Dfs_All();     ans = (ans * fac[all]) % MOD;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int MAXN = 3e6 + 9;   long long n, k;   long long two(long long x) {     long long base = 1, ans = 0;     while (base * x <= n) {       long long f = min(n, base * (x + 1) - 1);       ans += f - base * x + 1;       base *= 2;     }     return ans;   }   long long one(long long x) {     if (x % 2 == 0) return two(x + 1) + two(x);     return two(x);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int t = 1;     while (t--) {       cin >> n >> k;       long long N = n, ans = 1;       while (N) {         for (long long i = 0; i <= 10; i++) {           if (i >= N) break;           if (one(N - i) >= k) ans = max(ans, N - i);         }         N /= 2;       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool isused[26];   string s, t;   vector<pair<int, int> > v;   int abs(int num) {     if (num < 0)       return -num;     else       return num;   }   int main(void) {     cin >> s >> t;     int slen = s.size();     for (int i = 0; i < slen; i++) isused[(s[i] -  a )] = true;     int tlen = t.size();     for (int i = 0; i < tlen; i++) {       if (isused[(t[i] -  a )] == false) {         cout << -1 << endl;         return 0;       }     }     for (int i = 0; i < tlen;) {       pair<int, int> temp = make_pair(-1, -1);       for (int j = 0; j < slen; j++) {         if (t[i] == s[j]) {           int k1 = 0;           while (i + k1 + 1 < tlen && j + k1 + 1 < slen &&                  t[i + k1 + 1] == s[j + k1 + 1])             k1++;           int k2 = 0;           while (i + k2 + 1 < tlen && j - k2 - 1 >= 0 &&                  t[i + k2 + 1] == s[j - k2 - 1])             k2++;           if (k1 >= k2) {             if (temp.first == -1)               temp = make_pair(j, j + k1);             else if (abs(temp.second - temp.first) < k1)               temp = make_pair(j, j + k1);           } else {             if (temp.first == -1)               temp = make_pair(j, j - k2);             else if (abs(temp.first - temp.second) < k2)               temp = make_pair(j, j - k2);           }         }       }       if (temp.first <= temp.second)         i += (temp.second - temp.first) + 1;       else         i += (temp.first - temp.second) + 1;       v.push_back(make_pair(temp.first + 1, temp.second + 1));     }     cout << v.size() << endl;     for (int i = 0; i < v.size(); i++)       cout << v[i].first <<     << v[i].second << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, totx, tot, hangx[2005], liex[2005];   double ans;   char s[2005];   long long func(int n, int m, int a[]) {     long long ret = 0;     for (int i = 1; i <= n; i++) {       long long sum = 0, cnt = m - a[i];       for (int j = 1; j <= n; j++) {         if (a[j])           sum += (m - 1) * abs(i - j);         else           sum += m * abs(i - j);       }       if (a[i])         ret += (m - 1) * sum;       else         ret += m * sum;       if (a[i]) {         int l = i - 1, r = i + 1;         while (a[l + 1] < a[l]) {           cnt += m - a[l];           l--;         }         while (a[r - 1] < a[r]) {           cnt += m - a[r];           r++;         }         ret += 4 * cnt * (a[i] - 1);       }     }     return ret;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> s;       for (int j = 1; j <= m; j++) {         if (s[j - 1] ==  X ) {           hangx[i] = j;           liex[j] = i;           totx++;         }       }     }     tot = n * m - totx;     ans = (func(n, m, hangx) + func(m, n, liex)) * 1.0 / tot / tot;     cout << fixed << setprecision(12) << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, a[1000005], ans = 0;   inline long long read() {     long long red = 0, f_f = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f_f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) red = red * 10 + ch -  0 , ch = getchar();     return red * f_f;   }   signed main() {     n = (1ll << read()), m = read();     for (long long i = 0; i < n; i++) a[i] = read(), ans += a[i];     printf( %.6lf n , 1.0 * ans / n);     while (m--) {       long long x = read(), y = read();       ans -= a[x];       a[x] = y;       ans += a[x];       printf( %.6lf n , 1.0 * ans / n);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false), cin.tie(nullptr);     int n, k;     cin >> n >> k;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     int res = 0;     for (int i = 0; i < k; i++) {       int x = 0, y = 0;       for (int j = i; j < n; j += k) {         if (a[j] == 1)           x++;         else           y++;       }       res += min(x, y);     }     cout << res <<   n ;     return 0;   }