malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   struct pkt {     int x, y;   };   int main() {     ios_base::sync_with_stdio(0);     int n, m;     cin >> n >> m;     vector<string> p(n);     for (int i = 0; i < n; ++i) cin >> p[i];     vector<vector<int> > a(n, vector<int>(m, -1));     for (int k = 0; k < n; ++k) {       for (int l = 1; l < m; ++l) {         if (p[k][l - 1] !=  . )           a[k][l] = l - 1;         else           a[k][l] = a[k][l - 1];       }     }     vector<vector<int> > b(n, vector<int>(m, -1));     for (int k = 0; k < n; ++k) {       for (int l = m - 2; l >= 0; --l) {         if (p[k][l + 1] !=  . )           b[k][l] = l + 1;         else           b[k][l] = b[k][l + 1];       }     }     vector<vector<int> > c(n, vector<int>(m, -1));     for (int l = 0; l < m; ++l) {       for (int k = 1; k < n; ++k) {         if (p[k - 1][l] !=  . )           c[k][l] = k - 1;         else           c[k][l] = c[k - 1][l];       }     }     vector<vector<int> > d(n, vector<int>(m, -1));     for (int l = 0; l < m; ++l) {       for (int k = n - 2; k >= 0; --k) {         if (p[k + 1][l] !=  . )           d[k][l] = k + 1;         else           d[k][l] = d[k + 1][l];       }     }     vector<vector<int> > L, P, G, D;     L = a;     P = b;     G = c;     D = d;     int odp = 0, il = 0;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (p[i][j] ==  . ) continue;         int dl = 1;         pkt akt = {i, j};         vector<pkt> z;         while (true) {           pkt nast;           if (p[akt.x][akt.y] ==  L )             nast = {akt.x, L[akt.x][akt.y]};           else {             if (p[akt.x][akt.y] ==  R )               nast = {akt.x, P[akt.x][akt.y]};             else {               if (p[akt.x][akt.y] ==  U )                 nast = {G[akt.x][akt.y], akt.y};               else {                 if (p[akt.x][akt.y] ==  D ) nast = {D[akt.x][akt.y], akt.y};               }             }           }           if (nast.x == -1 || nast.y == -1) break;           ++dl;           pkt le = {akt.x, L[akt.x][akt.y]};           pkt pr = {akt.x, P[akt.x][akt.y]};           if (le.x != -1 && le.y != -1) P[le.x][le.y] = pr.y;           if (pr.x != -1 && pr.y != -1) L[pr.x][pr.y] = le.y;           pkt go = {G[akt.x][akt.y], akt.y};           pkt dol = {D[akt.x][akt.y], akt.y};           if (dol.x != -1 && dol.y != -1) G[dol.x][dol.y] = go.x;           if (go.x != -1 && go.y != -1) D[go.x][go.y] = dol.x;           akt = nast;           z.push_back(le);           z.push_back(pr);           z.push_back(go);           z.push_back(dol);         }         if (dl > odp) {           odp = dl;           il = 1;         } else {           if (dl == odp) ++il;         }         for (int k = 0; k < z.size(); ++k) {           if (z[k].x == -1 || z[k].y == -1) continue;           L[z[k].x][z[k].y] = a[z[k].x][z[k].y];           P[z[k].x][z[k].y] = b[z[k].x][z[k].y];           G[z[k].x][z[k].y] = c[z[k].x][z[k].y];           D[z[k].x][z[k].y] = d[z[k].x][z[k].y];         }       }     }     cout << odp <<     << il;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct pt {     double x, y;   };   pt a[1005];   long long n;   double kv(double g) { return g * g; }   double dist(pt a, pt b) { return sqrt(kv(a.x - b.x) + kv(a.y - b.y)); }   double visina(pt a, pt b, pt c) {     return abs((b.y - a.y) * c.x - (b.x - a.x) * c.y + b.x * a.y - b.y * a.x) /            (dist(a, b));   }   int main() {     cout.precision(15);     cout << fixed;     cin >> n;     for (long i = 0; i < n; i++) {       cin >> a[i].x >> a[i].y;     }     double d = 1e17;     for (long i = 0; i < n; i++) {       pt x = a[i], y = a[(i + 1) % n], z = a[(i + 2) % n];       d = min(d, visina(x, y, z));       d = min(d, visina(y, z, x));       d = min(d, visina(z, x, y));     }     cout << d / 2 << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6;   int pref[MAXN + 1];   string S;   int n, k;   void fast_io() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   pair<int, int> query(int s) {     int lo = s, hi = n;     int res = -1;     while (lo <= hi) {       int mid = (lo + hi) >> 1;       if (pref[mid] - pref[s - 1] >= k)         res = mid, hi = mid - 1;       else         lo = mid + 1;     }     if (res == -1 or (pref[res] - pref[s - 1]) != k) return make_pair(-1, -1);     pair<int, int> p = make_pair(res, -1);     lo = res, hi = n;     res = -1;     while (lo <= hi) {       int mid = (lo + hi) >> 1;       if (pref[mid] - pref[s - 1] <= k)         res = mid, lo = mid + 1;       else         hi = mid - 1;     }     p.second = res;     return p;   }   int main() {     fast_io();     cin >> k >> S;     n = S.length();     S =  $  + S;     for (int i = 1; i <= n; i++) {       pref[i] = (S[i] -  0 ) + pref[i - 1];     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       pair<int, int> p = query(i);       if (p.first == -1) continue;       ans += (p.second - p.first + 1);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long ans = 0x3f3f3f3f3f3f, a[510];   void calc(long long val) {     if (!val) return;     long long res = 0;     for (int i = 1; i <= n; i++) {       long long p = a[i] / val, q = a[i] % val;       if (p < q) return;       res += p - (p - q) / (val + 1);     }     ans = min(ans, res);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);     sort(a + 1, a + 1 + n);     for (long long i = 1; i * i <= a[1]; i++)       calc(i), calc(a[1] / i), calc(a[1] / i - 1);     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T sqr(const T &a) {     return a * a;   }   int a, b, x, ans;   int main() {     scanf( %d%d , &a, &b);     while (1) {       ans += a;       x += a;       if (x / b == 0) break;       a = x / b;       x -= (x / b) * b;     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int z = 0, f = 0, n, a;     cin >> n;     while (n--) {       cin >> a;       a == 0 ? z++ : f++;     }     if (z > 0 && f > 8) {       f -= f % 9;       while (f--) cout <<  5 ;       while (z--) cout <<  0 ;       cout << endl;     } else if (z > 0)       cout <<  0 n ;     else       cout <<  -1 n ;   }
#include <bits/stdc++.h>   using namespace std;   int t, n;   string s;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> t;     while (t--) {       cin >> s;       n = s.length();       int m = n, M = -1;       for (int i = 0; i < n; i++) {         if (s[i] ==  1 ) {           m = min(m, i);           M = max(M, i);         }       }       if (m > M) {         cout << 0 << endl;       } else {         int cnt = 0;         for (int i = m; i < M; i++) {           cnt += (s[i] ==  0 );         }         cout << cnt << endl;       }     }   }
#include <bits/stdc++.h>   using ll = long long;   using namespace std;   int n, m;   int const N = 3e5 + 2;   int const M = (1 << 21);   struct Triple {     int fi;     int se;     int th;     Triple(int f, int s, int t) : fi(f), se(s), th(t) {}   };   struct Compare {     bool operator()(const Triple& f, const Triple& g) {       if (f.th != g.th)         return (f.th < g.th);       else         return pair<int, int>{f.fi, f.se} < pair<int, int>{g.fi, g.se};     }   };   vector<Triple> seg;   int t[M];   int lazy[M];   int const INF = 1e9;   void push(int v) {     t[v * 2] += lazy[v];     lazy[v * 2] += lazy[v];     t[v * 2 + 1] += lazy[v];     lazy[v * 2 + 1] += lazy[v];     lazy[v] = 0;   }   int query(int v, int l, int r, int tl, int tr) {     if (r < l) return INF;     if (l == tl && r == tr)       return t[v];     else {       push(v);       int mid = (tl + tr) / 2;       int qw1 = query(v * 2, l, min(mid, r), tl, mid);       int qw2 = query(v * 2 + 1, max(mid + 1, l), r, mid + 1, tr);       return min(qw1, qw2);     }   }   void update(int v, int l, int r, int tl, int tr, int val) {     if (r < l) return;     if (l == tl && r == tr) {       t[v] += val;       lazy[v] += val;     } else {       push(v);       int mid = (tl + tr) >> 1;       update(v << 1, l, min(mid, r), tl, mid, val);       update((v << 1) + 1, max(mid + 1, l), r, mid + 1, tr, val);       t[v] = min(t[v << 1], t[(v << 1) + 1]);     }   }   int main() {     scanf( %d %d , &n, &m);     for (int i = (1); i <= (n); i++) {       int l, r, w;       scanf( %d %d %d , &l, &r, &w);       seg.push_back(Triple({l, r, w}));     }     sort(seg.begin(), seg.end(), Compare());     int mu = 0;     int lb = seg[0].th;     int rb = seg[0].th;     int ans = INF;     for (int i = (1); i <= (n); i++) {       update(1, seg[i - 1].fi, seg[i - 1].se - 1, 1, m - 1, 1);       rb = seg[i - 1].th;       while (query(1, 1, m - 1, 1, m - 1) > 0) {         ans = min(ans, rb - lb);         update(1, seg[mu].fi, seg[mu].se - 1, 1, m - 1, -1);         mu++;         if (mu != n) lb = seg[mu].th;       }     }     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 11;   const long long mod = 1e9 + 7;   int n, m, k;   long long a[N], ans;   int par[N], sz[N];   pair<long long, pair<int, int> > b[N];   vector<pair<int, int> > v;   vector<int> cur;   bool u[N];   int find_set(int v) {     if (v == par[v]) return v;     return par[v] = find_set(par[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (sz[a] < sz[b]) swap(a, b);       par[b] = a;       sz[a] += sz[b];     }   }   long long binpow(int n) {     long long a = 2, res = 1;     while (n) {       if (n % 2 == 0) {         n /= 2;         a = (a * a) % mod;       } else {         n--;         res = (res * a) % mod;       }     }     return res;   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &b[i].second.first, &b[i].second.second);       b[i].first = a[b[i].second.first] ^ a[b[i].second.second];     }     sort(b + 1, b + m + 1);     int l = 0;     int uniq = 0, comp;     for (int i = 1; i <= m; i++) {       if (b[i].first == b[i + 1].first) continue;       comp = 0;       for (int j = l + 1; j <= i; j++) v.push_back(b[j].second);       for (auto i : v) {         par[i.first] = i.first;         par[i.second] = i.second;         sz[i.first] = sz[i.second] = 1;       }       for (auto i : v) {         if (!u[i.first]) cur.push_back(i.first);         if (!u[i.second]) cur.push_back(i.second);         u[i.first] = u[i.second] = true;       }       for (auto i : v) union_sets(i.first, i.second);       for (auto i : cur)         if (find_set(i) == i) comp++;       ans = (ans + binpow(n - (int)cur.size() + comp)) % mod;       for (auto i : cur) {         u[i] = false;       }       cur.clear();       v.clear();       l = i;       uniq++;     }     long long p = ((1ll << k) - uniq) % mod;     p = (p * binpow(n)) % mod;     ans = (ans + p) % mod;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100100;   int N, M;   long long h[MAX_N], p[MAX_N];   inline long long calc(long long start, long long from, long long to) {     if (to <= start) return start - from;     if (from >= start) return to - start;     long long left = start - from, right = to - start;     long long mn = min(left, right), mx = max(left, right);     return 2 * mn + mx;   }   inline bool check(long long dist) {     int firstTrack = 1, lastTrack;     for (int i = 1; i <= N; i += 1) {       if (abs(p[firstTrack] - h[i]) > dist) {         continue;       }       lastTrack = firstTrack;       while (lastTrack < M &&              calc(h[i], p[firstTrack], p[lastTrack + 1]) <= dist) {         lastTrack += 1;       }       if (lastTrack == M) {         return true;       }       firstTrack = lastTrack + 1;     }     return false;   }   int main() {     cin >> N >> M;     for (int i = 1; i <= N; i += 1) {       cin >> h[i];     }     for (int i = 1; i <= M; i += 1) {       cin >> p[i];     }     long long answer = -1;     for (long long bit = (1LL << 40); bit > 0; bit /= 2) {       if (!check(answer + bit)) {         answer += bit;       }     }     cout << answer + 1 << endl;     return 0;   }
#include<bits/stdc++.h>   #define N 200100   #define ll long long   using namespace std;   const ll inf=9223372036854775807;   ll n,m;   ll T;   ll a[N];   ll maxx=-inf;   ll ans;   ll anss;   int main(){    cin>>T;    while(T--){     ans=0;     maxx=-inf;     cin>>n;     scanf( %lld ,&a[1]);     for(int i=2;i<=n;i++){      scanf( %lld ,&a[i]);      //maxx=max(maxx,abs(a[i]-a[i-1]));      ans+=abs(a[i]-a[i-1]);     }     anss=ans;     anss=min(anss,ans-abs(a[2]-a[1]));     anss=min(anss,ans-abs(a[n]-a[n-1]));     for(int i=2;i<=n-1;i++){      anss=min(anss,ans-abs(a[i+1]-a[i])-abs(a[i-1]-a[i])+abs(a[i-1]-a[i+1]));      //anss=min(anss,ans-abs(a[i-1]-a[i])+abs(a[i-1]-a[i+1]));     }     cout<<anss<<endl;    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   long long n, s1, v1, s2, v2;   long long i, j, k, ans, p, q, m, num;   inline long long cal_1() { return i * v1 + (n - i * s1) / s2 * v2; }   inline long long cal_2() { return i * v2 + (n - i * s2) / s1 * v1; }   inline long long maxn(long long a, long long b) {     if (a > b)       return a;     else       return b;   }   int main() {     cin >> n >> v1 >> v2 >> s1 >> s2;     m = lcm(s1, s2);     num = n / m;     if (num > 0) num--;     if (v1 * s2 >= v2 * s1) {       num = m / s1 * num;       p = (n - num * s1) / s2;       ans = num * v1 + p * v2;       if (s1 >= s2) {         for (i = num; i * s1 <= n; i++) ans = maxn(ans, cal_1());       } else {         for (i = p; i >= 0; i--) ans = maxn(ans, cal_2());       }     } else {       num = m / s2 * num;       p = (n - num * s2) / s1;       ans = num * v2 + p * v1;       if (s2 >= s1) {         for (i = num; i * s2 <= n; i++) ans = maxn(ans, cal_2());       } else {         for (i = p; i >= 0; i--) ans = maxn(ans, cal_1());       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   pair<int, int> arr[100000 + 5];   int main() {     cin >> n;     int L = 0, R = 0;     for (int i = 1; i <= n; i++) {       cin >> arr[i].first >> arr[i].second;       L += arr[i].first;       R += arr[i].second;     }     int ans = abs(L - R);     int id = 0;     for (int i = 1; i <= n; i++) {       if (ans < abs(L - R + 2 * (arr[i].second - arr[i].first))) {         ans = abs(L - R + 2 * (arr[i].second - arr[i].first));         id = i;       }     }     cout << id << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long n, p;   long long w, d;   int main() {     cin >> n >> p >> w >> d;     long long x, y = -1, z = -1;     for (long long i = p / w; i >= max(p / w - d, (long long)0); i--) {       x = i;       if (p - x * w >= 0 && (p - x * w) % d == 0) {         y = (p - x * w) / d;       }       if (n - x - y >= 0) {         z = n - x - y;       }       if (y != -1 && z != -1) break;     }     if (y != -1 && z != -1 && x + y + z == n)       cout << x <<     << y <<     << z << endl;     else       cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   #define pb push   #define speed ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)   using namespace std;   typedef long long ll;   typedef long double ld;   typedef vector <ll> vll;   typedef pair<ll,ll> pll;   typedef vector <pair<ll,ll>> vpll;   typedef vector <vll> vvll;   ll n;      int get(int x,int y)   {       int val;       cout<< ?  <<x<<   <<y<<endl;       cin>>val;       return val;   }      int main()   {       speed;       cin>>n;       ll l,h,m,midx,i;       midx=get(1,n);              if(midx>1 && get(1,midx)==midx)       {           l=1;           h=midx-1;                      while(l<h)           {               m=(l+h+1)/2;               i=get(m,midx);               if(i==midx)                   l=m;               else                    h=m-1;           }           cout<< !  <<l<<endl;       }       else       {           l=midx+1;           h=n;                      while(l<h)           {               m=(l+h)/2;               i=get(midx,m);               if(i!=midx)                   l=m+1;               else                   h=m;           }           cout<< !  <<l<<endl;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     a %= b;     return gcd(b, a);   }   long long power(long long x, long long y, long long md = 1000000007) {     long long res = 1;     x %= md;     while (y > 0) {       if (y & 1) res = (res * x) % md;       x = (x * x) % md;       y = y >> 1;     }     return res % md;   }   bool sortbysec(const pair<long long, long long> &a,                  const pair<long long, long long> &b) {     return (a.second < b.second);   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long sum = 0, arr[n];     for (long long i = 0; i < n; i++) {       cin >> arr[i];       sum += arr[i];     }     if (arr[0] > sum / 2) {       cout << 1 <<   n ;       cout << 1 <<   n ;       return 0;     }     long long sum1 = arr[0];     std::vector<long long> v;     v.push_back(1);     for (long long i = 1; i < n; i++) {       if (arr[0] >= (2 * arr[i])) {         sum1 += arr[i];         v.push_back(i + 1);       }     }     if (sum1 > (sum / 2)) {       cout << v.size() <<   n ;       for (long long j = 0; j < v.size(); j++) {         cout << v[j] <<    ;       }       cout <<   n ;     } else {       cout << 0 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   bool check(int n, int l, int v1, int v2, int k, double mid) {     double t0 = (l - v2 * mid) / (v1 - v2);     if (t0 < -eps) return 0;     double tb = mid - t0;     if (tb < -eps) return 0;     double tback = (v2 - v1) * tb / (v2 + v1) + tb;     if (tback + eps < tb) return 0;     return (n - 1) / k * tback + tb < mid + eps &&            (n - 1) / k * (tb - tback) * v2 + tb * v2 < l + eps;   }   int main() {     int n, l, v1, v2, k;     scanf( %d%d%d%d%d , &n, &l, &v1, &v2, &k);     double ll = 0, rr = 1. * l / v1;     for (int i = 1; i <= 100000; i++) {       double mid = (ll + rr) / 2;       if (check(n, l, v1, v2, k, mid))         rr = mid;       else         ll = mid;     }     printf( %.10f , ll);   }
#include <bits/stdc++.h>   int main() {     int N, L, i, count = 0, vivos = 0;     int vet[1000000];     scanf( %d , &N);     for (i = N - 1; i >= 0; i--) {       scanf( %d , &vet[i]);     }     for (i = 0; i < N; i++) {       if (count == 0) {         count = vet[i];         vivos++;       } else {         count--;         if (vet[i] > count) {           count = vet[i];         }         if (count == 0) {           count = vet[i];         }       }     }     printf( %d n , vivos);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int q, n, m;     cin >> q;     while (q--) {       cin >> n >> m;       string s;       cin >> s;       long long int a[n + 1], b[n + 1], zero = 0, one = 0;       a[0] = 0;       b[0] = 0;       for (long long int i = 0; i < n; i++) {         if (s[i] ==  0 ) {           zero++;         } else {           one++;         }         a[i + 1] = zero;         b[i + 1] = one;       }       set<long long int> se;       long long int f = 1;       for (long long int i = 0; i < n; i++) {         long long int a1 = m + i - 2 * a[i];         long long int k = (zero - one);         if (k == 0) {           if (a1 == 0) f = 0;         } else if (a1 % k == 0) {           long long int a2 = a1 / k;           long long int len = n * a2 + i;           if (len >= 0) se.insert(len);         }       }       if (f) {         cout << se.size() << endl;       } else {         cout << -1 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       string ch;       cin >> ch;       long long int ans = 0;       int x = 0, y = 0;       set<pair<pair<int, int>, pair<int, int>>> s;       pair<int, int> prev = {0, 0};       for (int i = 0; i < (int)ch.size(); i++) {         if (ch[i] ==  N )           y++;         else if (ch[i] ==  S )           y--;         else if (ch[i] ==  W )           x--;         else           x++;         bool check = false;         if (s.count({prev, {x, y}}) || s.count({{x, y}, prev}))           ans++;         else {           s.insert({prev, {x, y}});           s.insert({{x, y}, prev});           ans += 5;         }         prev = {x, y};       }       cout << ans <<   n ;     }     return 0;   }
#include<bits/stdc++.h>   #define max(a,b) ((a)>(b)?(a):(b))   using namespace std;   int n,m,k,x,y,z,dp[500039],l[500039],r[500039],now,a[500039],siz[500039],g[500039],f[500039];   void work(int n){    for(int i=1;i<=n;i++)     scanf( %d ,&a[i]),siz[a[i]]++,l[a[i]]=l[a[i]]?l[a[i]]:i,r[a[i]]=i;    for(int i=1;i<=n;i++)     if(l[i])      g[l[i]]=i;   }   int main(){    register int i,j;    scanf( %d ,&n);    work(n);        for(i=n;i;i--){     dp[i]=dp[i+1];f[a[i]]++;     if(g[i]) dp[i]=max(dp[i],dp[r[g[i]]+1]+siz[g[i]]);     else dp[i]=max(dp[i],f[a[i]]);    }    cout<<n-dp[1]<<endl;   }