malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int dx[] = {1, 0, -1, 0};   const int dy[] = {0, 1, 0, -1};   struct Point {     int x, y;   } Q[2000 * 2000], P, np;   int n, m;   int b[2000][2000];   int mark[2000][2000], T;   int L[2000 * 2000];   int flag[2000][2000];   bool out(int x, int y) { return x < 0 || y < 0 || x >= n || y >= m; }   void Mark(int x, int y) {     int head, tail;     int d, i, j, k;     head = tail = 0;     Q[tail].x = x;     Q[tail].y = y;     tail++;     while (head < tail) {       P = Q[head++];       for (d = 0; d < 4; d++) {         np.x = P.x + dx[d];         np.y = P.y + dy[d];         if (out(np.x, np.y) || b[np.x][np.y] == 0) continue;         if (mark[np.x][np.y] != T) {           mark[np.x][np.y] = T;           Q[tail++] = np;         }       }     }   }   int count(int x, int y) {     int i, j, nx, ny;     int cnt(0);     int dx, dy;     for (i = -2; i <= 2; i++)       for (j = -2; j <= 2; j++) {         nx = x + i;         ny = y + j;         if (out(nx, ny)) continue;         if (mark[nx][ny] == mark[x][y]) cnt++;       }     if (cnt != 9) return false;     for (dx = -1; dx <= 1; dx += 2)       for (dy = -1; dy <= 1; dy += 2) {         for (i = 0; i < 3; i++) {           for (j = 0; j < 3; j++) {             nx = x + dx * i;             ny = y + dy * j;             if (out(nx, ny) || b[nx][ny] == 0) break;             if (mark[nx][ny] != mark[x][y]) break;           }           if (j < 3) break;         }         if (i == 3) return true;       }     return false;   }   int remove(int x, int y) {     int head, tail;     int d, i, j, k;     head = tail = 0;     Q[tail].x = x;     Q[tail].y = y;     tail++;     flag[x][y] = true;     for (k = 0; k < 10; k++) {       for (i = tail; head < i; head++) {         P = Q[head];         for (d = 0; d < 4; d++) {           np.x = P.x + dx[d];           np.y = P.y + dy[d];           if (out(np.x, np.y) || b[np.x][np.y] == 0) continue;           if (mark[np.x][np.y] != mark[x][y]) continue;           if (flag[np.x][np.y] == false) {             flag[np.x][np.y] = true;             Q[tail++] = np;           }         }       }     }     return tail <= 40;   }   int main() {     int i, j, k, d;     int cnt, x, y;     scanf( %d%d , &n, &m);     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) scanf( %d , b[i] + j);     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (b[i][j] == 0 || mark[i][j]) continue;         T++;         Mark(i, j);       }     }     printf( %d n , T);     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         cnt = 0;         if (!mark[i][j] || flag[i][j]) continue;         for (d = 0; d < 4; d++) {           x = i + dx[d];           y = j + dy[d];           if (out(x, y)) continue;           cnt += b[x][y];         }         if (cnt >= 3) continue;         if (count(i, j) && remove(i, j)) {           L[mark[i][j]]++;         }       }     }     sort(L + 1, L + T + 1);     for (i = 1; i <= T; i++) {       if (i > 1) printf(   );       printf( %d , L[i]);     }     printf(  n );   }
#include <bits/stdc++.h>   using namespace std;   const int mn = 55;   const int mm = 2505;   const int mod = 1000000007;   int n, m, T;   int t[mm], g[mm];   int a[mn][mm];   int bc[mn][mn][mm];   int way[mn][mn][mn][3];   int cnt[3];   void init_a() {     memset(a, -2, sizeof a);     a[0][0] = 1;     for (int k = 1; k <= n; k++) {       if (g[k]) continue;       for (int i = cnt[0]; i >= 0; i--) {         for (int j = T - t[k]; j >= 0; j--) {           if (a[i][j] <= 0) continue;           int &ans = a[i + 1][j + t[k]];           if (ans < 0) ans = 0;           ans += a[i][j];           if (ans >= mod) ans -= mod;         }       }       cnt[0]++;     }   }   void init_bc() {     memset(bc, -2, sizeof bc);     bc[0][0][0] = 1;     for (int k = 1; k <= n; k++) {       if (!g[k]) continue;       for (int i = cnt[1]; i >= 0; i--) {         for (int j = cnt[2]; j >= 0; j--) {           for (int l = T - t[k]; l >= 0; l--) {             if (bc[i][j][l] <= 0) continue;             int &ans = bc[i + (g[k] == 1)][j + (g[k] == 2)][l + t[k]];             if (ans < 0) ans = 0;             ans += bc[i][j][l];             if (ans >= mod) ans -= mod;           }         }       }       if (g[k] == 1)         cnt[1]++;       else         cnt[2]++;     }   }   void inc(int &a, int b) {     if (a < 0) a = 0;     a += b;     if (a >= mod) a -= mod;   }   void init_way() {     memset(way, -2, sizeof(way));     way[1][0][0][0] = way[0][1][0][1] = way[0][0][1][2] = 1;     for (int i = 0; i <= cnt[0]; i++) {       for (int j = 0; j <= cnt[1]; j++) {         for (int k = 0; k <= cnt[2]; k++) {           for (int last = 0; last < 3; last++) {             for (int nxt = 0; nxt < 3; nxt++) {               if (nxt == last || way[i][j][k][last] <= 0) continue;               int uans = way[i][j][k][last];               if (nxt == 0)                 inc(way[i + 1][j][k][0], uans);               else if (nxt == 1)                 inc(way[i][j + 1][k][1], uans);               else                 inc(way[i][j][k + 1][2], uans);             }           }         }       }     }   }   inline int getway(int i, int j, int k) {     int ans = 0;     for (int last = 0; last < 3; last++) {       if (way[i][j][k][last] <= 0) continue;       inc(ans, way[i][j][k][last]);     }     return ans;   }   int fact[105];   int main() {     scanf( %d%d , &n, &T);     for (int i = 1; i <= n; i++) scanf( %d%d , &t[i], &g[i]), g[i]--;     fact[0] = 1;     for (int i = 1; i <= 55; i++) fact[i] = (long long)fact[i - 1] * i % mod;     init_a();     init_bc();     init_way();     long long ans = 0;     for (int i = 0; i <= cnt[0]; i++) {       for (int j = 0; j <= cnt[1]; j++) {         for (int k = 0; k <= cnt[2]; k++) {           for (int t = 0; t <= T; t++) {             if (a[i][t] <= 0 || bc[j][k][T - t] <= 0) continue;             ans = (ans + (long long)a[i][t] * bc[j][k][T - t] % mod *                              getway(i, j, k) % mod * fact[i] % mod * fact[j] %                              mod * fact[k]) %                   mod;           }         }       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     sort(arr, arr + n);     int i;     for (i = 0; i < n && arr[i] + k <= 5; i++)       ;     cout << (i) / 3;   }
#include <bits/stdc++.h>   using namespace std;   bool isPrime(int n) {     for (int i = 2; (i * i) <= n; i++) {       if (n % i == 0) return 0;     }     return 1;   }   int nerPr(int n) {     for (int i = n;; i++) {       if (isPrime(i)) return i;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> a;     int mn = (int)1e9;     for (int i = 0; i < n; i++) {       int d;       cin >> d;       a.push_back(d);       mn = min(mn, d);       if (d == 1) {         cout <<  -1 n ;         exit(0);       }     }     cout << 1;   }
#include <bits/stdc++.h>   using namespace std;   const long long linf = 4000000000000000000LL;   const long long inf = 1000000007;   const long double pi = 3.1415926535;   void pv(vector<int> a) {     for (auto& x : a) cout << x <<    ;     cout <<   n ;   }   void pv(vector<long long> a) {     for (auto& x : a) cout << x <<    ;     cout <<   n ;   }   void pv(vector<vector<int> > a) {     for (int i = (0); i < (int(a.size())); ++i) {       cout << i <<   n ;       pv(a[i]);       cout <<   n ;     }   }   void pv(vector<vector<long long> > a) {     for (int i = (0); i < (int(a.size())); ++i) {       cout << i <<   n ;       pv(a[i]);     }     cout <<   n ;   }   void pv(vector<string> a) {     for (auto& x : a) cout << x <<   n ;     cout <<   n ;   }   void setIO(string second) {     ios_base::sync_with_stdio(0);     cin.tie(0);     if (int(second.size())) {       freopen((second +  .in ).c_str(),  r , stdin);       if (second !=  test1 ) freopen((second +  .out ).c_str(),  w , stdout);     }   }   int n;   vector<long double> a;   vector<vector<int> > adj;   vector<int> parent;   void dfs(int i, int p) {     parent[i] = p;     for (auto& x : adj[i]) {       if (x == p) continue;       dfs(x, i);     }   }   int main() {     setIO(  );     cin >> n;     a.resize(n);     for (auto& x : a) cin >> x, x = 1 - x;     adj.resize(n);     for (int i = (0); i < (n - 1); ++i) {       int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     parent.resize(n);     dfs(0, -1);     vector<long double> b(n, 1);     for (int i = (0); i < (n); ++i)       for (auto& x : adj[i])         if (x != parent[i]) b[i] -= a[x];     long double ans = 0;     for (int i = (0); i < (n); ++i) ans += a[i] * b[i];     int testc;     cin >> testc;     while (testc--) {       int i;       long double x;       cin >> i >> x;       x = 1 - x;       long double y = a[i];       if (parent[i] != -1) {         b[parent[i]] -= x - y;         ans -= (x - y) * a[parent[i]];       }       ans += (x - y) * b[i];       a[i] = x;       cout << fixed << setprecision(20) << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, ans;   int p[100010], q[100010], mn[100010];   vector<int> t[100010], v[100010];   bool flag[100010];   inline int dist(int x, int y) { return abs(p[x] - q[y]); }   int main() {     scanf( %d%d%d%d , &n, &m, &x, &y);     memset(mn, 63, sizeof(mn));     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= m; i++) scanf( %d , &q[i]);     q[m + 1] = 1e9;     q[0] = -1e9;     int pos = 1;     for (int i = 1; i <= n; i++) {       while (pos <= m && q[pos] <= p[i]) pos++;       pos--;       if (dist(i, pos) == dist(i, pos + 1)) {         t[i].push_back(pos);         t[i].push_back(pos + 1);       } else if (dist(i, pos) < dist(i, pos + 1)) {         t[i].push_back(pos);       } else {         t[i].push_back(pos + 1);       }     }     for (int i = 1; i <= n; i++)       if (t[i].size() == 1) {         int t1 = t[i][0];         v[t1].push_back(i);         mn[t1] = min(mn[t1], dist(i, t1));       }     for (int i = 1; i <= n; i++)       if (t[i].size() == 1) {         int t1 = t[i][0];         if (dist(i, t1) > mn[t1]) flag[i] = 1;       }     for (int i = 1; i <= n; i++)       if (t[i].size() == 2) {         bool flag1 = 0, flag2 = 0;         int cnt1 = 0, cnt2 = 0;         int t1 = t[i][0], t2 = t[i][1], d = dist(i, t1);         for (int j = 0; j < v[t1].size(); j++) {           if (dist(v[t1][j], t1) < d) flag1 = 1;           if (d < dist(v[t1][j], t1))             if (flag[v[t1][j]] == 0) cnt1++;         }         for (int j = 0; j < v[t2].size(); j++) {           if (dist(v[t2][j], t2) < d) flag2 = 1;           if (d < dist(v[t2][j], t2))             if (flag[v[t2][j]] == 0) cnt2++;         }         if (cnt1 + flag1 <= cnt2 + flag2) {           flag[i] = flag1;           for (int j = 0; j < v[t1].size(); j++)             if (d < dist(v[t1][j], t1)) flag[v[t1][j]] = 1;           mn[t1] = min(mn[t1], dist(i, t1));           v[t1].push_back(i);         } else {           flag[i] = flag2;           for (int j = 0; j < v[t2].size(); j++)             if (d < dist(v[t2][j], t2)) flag[v[t2][j]] = 1;           mn[t2] = min(mn[t2], dist(i, t2));           v[t2].push_back(i);         }       }     for (int i = 1; i <= n; i++) ans += flag[i];     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void reset(int& n, int first) { n = n & ~(1 << first); }   bool check(int n, int first) { return (bool)(n & (1 << first)); }   set<int> ax, ay;   multiset<int> bx, by;   set<int>::iterator l, r;   int main() {     int i, j, k, n, w, h;     while (scanf( %d%d%d , &w, &h, &n) == 3) {       ax.clear();       ay.clear();       bx.clear();       by.clear();       ax.insert(0);       ax.insert(h);       ay.insert(0);       ay.insert(w);       bx.insert(h);       by.insert(w);       for (i = 0; i < n; i++) {         char ch;         int v;         scanf( %c%c%d , &ch, &ch, &v);         if (ch ==  H ) {           r = ax.lower_bound(v);           l = --r;           r++;           bx.erase(bx.find(*r - *l));           ax.insert(v);           bx.insert(v - *l);           bx.insert(*r - v);         } else {           r = ay.lower_bound(v);           l = --r;           r++;           by.erase(by.find(*r - *l));           ay.insert(v);           by.insert(v - *l);           by.insert(*r - v);         }         long long ans = (long long)(*bx.rbegin()) * (*by.rbegin());         cout << ans << endl;       }     }   }
#include <bits/stdc++.h>   int main() {     int a, b, c = 0, d, e, i;     scanf( %d , &a);     for (i = 1; i <= a; i++) {       scanf( %d , &b);       if (b == 1) {         c++;       }     }     if (c > 0) {       printf( HARD n );     } else {       printf( EASY n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mini[10], maxi[10], dist[10];   int main() {     int n, i, total;     scanf( %d , &n);     total = 0;     for (i = 0; i < 3; ++i) {       scanf( %d %d , &mini[i], &maxi[i]);       total += mini[i];       dist[i] = mini[i];     }     i = 0;     while (n - total > 0) {       int available, pot;       available = maxi[i] - mini[i];       pot = n - total;       dist[i] += min(available, pot);       total += min(available, pot);       i++;     }     for (i = 0; i < 2; ++i) {       printf( %d  , dist[i]);     }     printf( %d n , dist[2]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 200002;   int val[M];   vector<int> vec[33];   set<int> st;   struct Nod {     int v;     int id;     bool operator<(const Nod &cmp) const {       if (v == cmp.v) return id < cmp.id;       return v > cmp.v;     }   } p[M];   int n;   void init() {     for (int i = 0; i < 32; i++) {       for (int j = 0; j < n; j++) {         if (val[j] & (1 << i)) {           vec[i].push_back(j);         }       }     }   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &val[i]);       p[i].v = val[i];       p[i].id = i;     }     sort(p, p + n);     init();     long long ans = 0;     for (int i = 0; i < n; i++) {       Nod u = p[i];       set<int>::iterator it = st.upper_bound(u.id);       int L, R;       if (it == st.end()) {         R = n - 1;       } else         R = *it - 1;       if (it == st.begin()) {         L = 0;       } else {         it--;         L = *it + 1;       }       int l = L, r = R;       for (int j = 0; j < 32; j++) {         if ((u.v & (1 << j)) == 0) {           int tl = upper_bound(vec[j].begin(), vec[j].end(), u.id) -                    vec[j].begin() - 1;           if (tl >= 0) l = max(l, vec[j][tl] + 1);           int tr =               upper_bound(vec[j].begin(), vec[j].end(), u.id) - vec[j].begin();           if (tr < vec[j].size()) r = min(r, vec[j][tr] - 1);         }       }       ans += 1ll * (u.id - l + 1) * (r - u.id + 1);       st.insert(u.id);     }     printf( %I64d n , 1ll * n * (n + 1) / 2 - ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[12][2001];   int dp[2][4096];   int sos[4096];   bool use[2001];   pair<int, int> b[2001];   void solve() {     cin >> n >> m;     for (int j = 1; j <= m; j++) b[j].first = 0;     for (int i = 0; i < n; i++) {       for (int j = 1; j <= m; j++) {         cin >> a[i][j];         b[j].first = max(b[j].first, a[i][j]);         b[j].second = j;       }     }     sort(b + 1, b + m + 1);     for (int j = m; j >= max(1, m - n + 1); j--) use[b[j].second] = true;     for (int l = 0; l < (1 << n); l++) dp[0][l] = 0;     int c = 0, p = 1;     for (int j = 1; j <= m; j++) {       if (!use[j]) continue;       c ^= 1;       p ^= 1;       for (int l = 0; l < (1 << n); l++) dp[c][l] = 0;       for (int k = 0; k < n; k++) {         int tmp = a[0][j];         for (int l = 0; l < n - 1; l++) a[l][j] = a[l + 1][j];         a[n - 1][j] = tmp;         for (int l = 0; l < (1 << n); l++) sos[l] = dp[p][l];         for (int i = 0; i < n; i++) {           for (int l = 0; l < (1 << n); l++) {             if (l & (1 << i)) sos[l] = max(sos[l], sos[l ^ (1 << i)] + a[i][j]);           }         }         for (int l = 0; l < (1 << n); l++) dp[c][l] = max(dp[c][l], sos[l]);       }     }     for (int j = m; j >= max(1, m - n + 1); j--) use[b[j].second] = false;     cout << dp[c][(1 << n) - 1] <<   n ;   }   int main() {     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   int x, y, n;   int xs[30], ys[30];   int mem[16777227];   int ALL;   int dt[30][30];   int calc(int mask) {     if (mask == ALL) return 0;     int& res = mem[mask];     if (res != -1) return res;     res = ((int)1e9);     int f = -1;     for (int i = 0; i < n; ++i) {       if ((mask >> i) & 1) continue;       if (f == -1) {         f = i;         res = min(res, calc(mask | (1 << i)) + 2 * dt[n][i]);       } else {         int d = dt[n][i] + dt[n][f] + dt[i][f];         res = min(res, calc(mask | (1 << i) | (1 << f)) + d);       }     }     return res;   }   int sz;   int sol[50];   void build(int mask) {     if (mask == ALL) return;     int& res = mem[mask];     for (int i = 0; i < n; ++i) {       if ((mask >> i) & 1) continue;       if (res == calc(mask | (1 << i)) + 2 * dt[n][i]) {         printf(  %d 0 , i + 1);         build(mask | (1 << i));         return;       }       for (int j = 0; j < n; ++j) {         if (i == j) continue;         if ((mask >> j) & 1) continue;         int d = dt[n][i] + dt[n][j] + dt[i][j];         if (res == calc(mask | (1 << i) | (1 << j)) + d) {           printf(  %d %d 0 , i + 1, j + 1);           build(mask | (1 << i) | (1 << j));           return;         }       }     }     return;   }   inline int dist(int x1, int y1, int x2, int y2) {     return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);   }   int main() {     while (scanf( %d%d , &x, &y) != -1) {       scanf( %d , &n);       ALL = (1 << n) - 1;       for (int i = 0; i < n; ++i) {         scanf( %d%d , xs + i, ys + i);         dt[i][n] = dt[n][i] = dist(xs[i], ys[i], x, y);       }       for (int i = 0; i < n; ++i) {         for (int j = i + 1; j < n; ++j) {           dt[i][j] = dt[j][i] = dist(xs[i], ys[i], xs[j], ys[j]);         }       }       memset(mem, -1, sizeof mem);       int res = calc(0);       printf( %d n0 , res);       build(0);       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n;     cin >> n;     string s;     cin >> s;     long long int mx[n], mn[n];     for (long long int i = 0; i < n; i++) {       mx[i] = i + 1, mn[i] = n - i;     }     long long int j = 0;     for (long long int i = 0; i < n - 1; i = j) {       while (j < n && s[i] == s[j]) j++;       if (s[i] ==  < )         reverse(mn + i, mn + j + 1);       else         reverse(mx + i, mx + j + 1);     }     for (long long int x : mn) cout << x <<    ;     cout << endl;     for (long long int x : mx) cout << x <<    ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long int T = 1;     cin >> T;     while (T--) {       solve();       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, k;     cin >> n >> k;     vector<vector<pair<long long, long long>>> ans(k);     long long gr = k, cnt = 0;     bool f = false;     vector<pair<pair<long long, long long>, long long>> a(n);     long double c = 0;     for (long long i = 1; i <= n; ++i) {       pair<long long, long long> x;       cin >> x.first >> x.second;       if (x.second == 1 && gr > 1) gr--;       if (x.second == 1) cnt++;       if (x.second == 2) f = true;       a[i - 1] = {x, i};     }     sort(a.begin(), a.end(),          greater<pair<pair<long long, long long>, long long>>());     k--;     long long free = 0;     for (long long i = 0; i < n; ++i) {       if (k == -1) {         for (long long j = i; j < n; ++j) {           ans[free].push_back({a[j].first.first, a[j].second});           if (free + 1 < gr) free++;         }         break;       }       if (a[i].first.second == 1) {         ans[k].push_back({a[i].first.first, a[i].second});         k--;       } else {         ans[free].push_back({a[i].first.first, a[i].second});         if (free + 1 < gr) free++;       }     }     cout << fixed << setprecision(1);     k = ans.size();     for (long long i = 0; i < ans.size(); ++i) {       long long mn = 1e9;       sort(ans[i].begin(), ans[i].end());       for (long long j = 0; j < ans[i].size(); ++j) {         if (j == 0 && k - cnt - 1 < i)           c += (long double)ans[i][j].first / 2;         else           c += ans[i][j].first;       }     }     cout << c <<   n ;     for (auto i : ans) {       cout << i.size() <<    ;       for (auto j : i) cout << j.second <<    ;       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     map<int, int> m;     int ma = -1;     int ans = 0;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (m[x] > ma) {         ma = m[x];         ans = x;       }       m[x]++;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   string s;   string change(string s, int l, int r, char c1, char c2) {     for (int i = l - 1; i < r; i++) {       if (s[i] == c1) {         s[i] = c2;       }     }     return s;   }   int main() {     cin >> n >> m;     cin >> s;     for (int i = 0; i < m; i++) {       int l, r;       char c1, c2;       cin >> l >> r >> c1 >> c2;       s = change(s, l, r, c1, c2);     }     cout << s;   }