malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m, k, arr[105][105];   int check(int c[105], int ctC, int secRow) {     int ans = INT_MAX;     if (ctC > 10) return ans;     for (int i = 0; i < (1 << ctC); i++) {       int ct = 0;       for (int r = 2; r <= n; r++) {         if (r != secRow) {           int same = 0;           for (int j = 1; j <= m; j++) {             int row1El =                 c[j] >= 0 && (i & (1 << c[j])) ? 1 - arr[1][j] : arr[1][j];             if (row1El == arr[r][j]) same++;           }           ct += min(same, m - same);         }       }       ans = min(ctC + ct, ans);     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         cin >> arr[i][j];       }     }     if (n == 1 || m == 1) {       cout << 0 << endl;       return 0;     }     int ans = INT_MAX;     for (int secRow = 2; secRow <= min(15, n); secRow++) {       int ctA = 0, ctB = 0;       int a[105], b[105];       memset(a, -1, sizeof(a));       memset(b, -1, sizeof(b));       for (int j = 1; j <= m; j++) {         if ((arr[1][j] ^ arr[secRow][j]) == 1) {           a[j] = ctA;           ctA++;         } else {           b[j] = ctB;           ctB++;         }       }       ans = min(check(a, ctA, secRow), check(b, ctB, secRow));     }     cout << (ans > k ? -1 : ans) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #define ll long long   #define f(i,n) for(int i=0;i<n;i++)   #define endl (  n )   #define pb push_back   #define mp make_pair   #define pl pair<ll, ll>   #define vl vector<ll>   #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);      #define M 1000000007    const int N=2e5+2;    ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}   ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}   ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}   vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}   ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}   ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}   ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}   ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m      int fact[N];              void precalc(){               fact[0]=1;               for(int i=1;i<N;i++){                   fact[i]=mod_mul(fact[i-1],i,M);               }           }    long long mod(long long x){               return ((x%M + M)%M);           }           ll inv(ll x){               return expo(x,M-2,M);           }              ll divide(ll a, ll b){               return mod_mul(a,inv(b),M);           }              ll nCr(ll n, ll r){               return divide(fact[n],mod_mul(fact[r],fact[n-r],M));           }          int main()   {       fastIO     ll t;     cin>>t;     while(t--){         ll a,b;         cin>>a>>b;         ll d=abs(a-b);         ll e=max(a,b);         if(a==b){             cout<<0<<   <<0<<endl;             continue;         }                 ll k=e%d;         ll v;         if(d-k>=0 and d-k<k) v=d-k;         else  v=k;            cout<<d<<   <<v<<endl;     }               }
#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 7;   const int M = 60;   const int inf = 1e9 + 7;   const long long base = 1e18;   const double pi = acos(-1);   const double ep = 1e-18;   int n;   int a[N];   void solve() {     cin >> n;     for (int i = 0; i < n; i++) scanf( %d , a + i);     int last = 0;     while (last + 1 < n && a[last + 1] == a[0]) last++;     int i = last + 1;     int ans = 0;     while (i < n) {       int j = i;       while (j < n && a[j] == a[i]) j++;       if (i != n - 1 && j - i == 1) {         i = j;         continue;       } else {         ans = max(ans, (i - last) / 2);         for (int u = last + 1, v = i - 1; u <= v; u++, v--) {           a[u] = a[last];           a[v] = a[i];         }         i = j;         last = i - 1;       }     }     cout << ans << endl;     for (int i = 0; i < n; i++) printf( %d  , a[i]);   }   int main() { solve(); }
#include <bits/stdc++.h>   int main() {     long long a, b, c;     scanf( %lld %lld , &a, &b);     c = (b > (a + 1) / 2) ? 2 * (b - (a + 1) / 2) : 2 * b - 1;     printf( %lld , c);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long x;   int n, d;   struct cmp {     bool operator()(pair<int, int> x, pair<int, int> y) {       return x.first > y.first;     }   };   set<pair<int, int>, cmp> que;   set<pair<int, int>, cmp>::iterator it;   vector<int> num;   int a[100005], c[100005];   bool b[100005];   long long next() {     x = (x * 37 + 10007) % 1000000007;     return x;   }   void initAB() {     for (int i = 0; i < n; i++) a[i] = i + 1;     for (int i = 0; i < n; i++) swap(a[i], a[next() % (i + 1)]);     for (int i = 0; i < n; i++) {       if (i < d)         b[i] = 1;       else         b[i] = 0;     }     for (int i = 0; i < n; i++) swap(b[i], b[next() % (i + 1)]);   }   int main() {     scanf( %d%d%I64d , &n, &d, &x);     initAB();     if (d < 1000) {       for (int i = 0; i < n; i++) {         if (b[i]) num.push_back(i);         for (int j = 0; j < num.size(); j++) c[i] = max(c[i], a[i - num[j]]);       }     } else {       for (int i = 0; i < n; i++) {         que.insert(make_pair(a[i], i));         for (it = que.begin(); it != que.end(); it++) {           if (b[i - it->second]) {             c[i] = it->first;             break;           }         }       }     }     for (int i = 0; i < n; i++) cout << c[i] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string yes =  YES , no =  NO ;   void solve() {     long long n, k;     scanf( %lli%lli , &n, &k);     long long qwe = n - k + 1;     long long rty = n - 2 * k + 2;     if (qwe > 0 and qwe % 2 == 1) {       cout << yes << endl;       for (int i = 0; i < k - 1; i++) {         printf( 1  );       }       printf( %lli n , qwe);       return;     }     if (rty > 0 && rty % 2 == 0) {       cout << yes << endl;       for (int i = 0; i < k - 1; i++) {         printf( 2  );       }       printf( %lli n , rty);       return;     }     cout << no << endl;   }   int main() {     int t;     scanf( %d , &t);     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 400;   int n, m, wynik, A[N], B[N];   map<double, bitset<120> > mapa;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> A[i];     for (int i = 1; i <= m; i++) cin >> B[i];     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         double pom = (A[i] + B[j]) / 2.0;         mapa[pom][i - 1] = mapa[pom][n + j - 1] = true;       }     }     for (auto u : mapa)       for (auto v : mapa) wynik = max(wynik, (int)(u.second | v.second).count());     cout << wynik << endl;     getchar();     getchar();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long double pi = acos(-1);   const long long maxn = 200001;   template <class T>   ostream &operator<<(ostream &out, vector<T> &A) {     for (auto x : A) out << x <<    ;     return out;   }   template <class T1, class T2>   T1 powr(T1 a, T2 b) {     T1 res = 1;     for (long long i = 1; i < b + 1; i++) res = res * a;     return res;   }   int32_t main() {     long long Q;     cin >> Q;     while (Q--) {       long long n, pos;       cin >> n;       vector<long long> A(n);       for (long long i = 0; i < n; i++) {         cin >> A[i];         if (A[i] == 1) pos = i;       }       bool okl = true, okr = true;       for (long long j = 1; j < n; j++) {         okl &= (A[(pos - j + n) % n] == j + 1);         okr &= (A[(pos + j + n) % n] == j + 1);       }       puts(okl or okr ?  YES  :  NO );     }   }
#include <bits/stdc++.h>   using namespace std;   int mp[2123][2123];   int prehb[2123][2123];   int prehs[2123][2123];   int prelb[2123][2123];   int prels[2123][2123];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       map<int, int> lis;       for (int j = 1; j <= m; j++) {         cin >> mp[i][j];         lis[mp[i][j]] = 1;       }       int cnt = 0;       for (auto &j : lis) {         j.second = ++cnt;       }       int len = lis.size();       for (int j = 1; j <= m; j++) {         prehb[i][j] = len - lis[mp[i][j]];         prehs[i][j] = lis[mp[i][j]] - 1;       }     }     for (int i = 1; i <= m; i++) {       map<int, int> lis;       for (int j = 1; j <= n; j++) {         lis[mp[j][i]] = 1;       }       int cnt = 0;       for (auto &j : lis) {         j.second = ++cnt;       }       int len = lis.size();       for (int j = 1; j <= n; j++) {         prelb[j][i] = len - lis[mp[j][i]];         prels[j][i] = lis[mp[j][i]] - 1;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int base = max(prehs[i][j], prels[i][j]);         int big = max(prehb[i][j], prelb[i][j]) + base + 1;         cout << big <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   int type[200010];   int weight[200010];   map<int, long long int> M;   long long int power(long long int a, int b) {     a %= mod;     long long int res = 1;     while (b) {       if (b & 1) res = res * a % mod;       a = a * a % mod;       b >>= 1;     }     return res;   }   long long int inv(int a) {     if (a <= 0) return 0;     if (M.find(a) != M.end()) return M[a];     return M[a] = power(a, mod - 2);   }   long long int dp[3001][3001];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, w1 = 0, w2 = 0;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> type[i];     for (int i = 1; i <= n; i++) {       cin >> weight[i];       if (!type[i])         w1 += weight[i];       else         w2 += weight[i];     }     dp[0][0] = 1;     for (int i = 1; i <= m; i++) {       for (int j = 0; j < i; j++)         dp[i][j] = dp[i - 1][j] * (w1 - (i - 1 - j)) % mod *                    inv(w1 - (i - 1 - j) + w2 + j) % mod;       for (int j = 1; j <= i; j++)         dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] * (w2 + j - 1) % mod *                                    inv(w1 - (i - 1 - (j - 1)) + w2 + (j - 1))) %                    mod;     }     long long int c1 = 0, c2 = 0;     for (int i = 0; i <= m; i++) c2 = (c2 + dp[m][i] * (w2 + i)) % mod;     c2 = c2 * inv(w2) % mod;     for (int i = 0; i <= m; i++) c1 = (c1 + dp[m][i] * (w1 - (m - i))) % mod;     c1 = c1 * inv(w1) % mod;     for (int i = 1; i <= n; i++)       if (type[i])         cout << weight[i] * c2 % mod <<   n ;       else         cout << weight[i] * c1 % mod <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline int size(T x) {     return x.size();   }   long long k[3], t[3];   int num_done[100005] = {};   queue<int> waiters[3];   set<pair<long long, int> > Q;   void try_putinto(int pid, int qnr, long long tijd) {     if (!k[qnr]) {       waiters[qnr].push(pid);     } else {       num_done[pid]++;       k[qnr]--;       Q.insert(make_pair(tijd + t[qnr], pid));     }   }   void onefree(int qnr, long long tijd) {     k[qnr]++;     if (!waiters[qnr].empty()) {       int pid_next = waiters[qnr].front();       waiters[qnr].pop();       try_putinto(pid_next, qnr, tijd);     }   }   int main() {     cin >> k[0] >> k[1] >> k[2];     cin >> t[0] >> t[1] >> t[2];     int n;     cin >> n;     vector<long long> arrival;     for (int i = 0; i < n; i++) {       int tmp;       cin >> tmp;       arrival.push_back(tmp);     }     for (int i = 0; i < n; i++) {       Q.insert(make_pair(arrival[i], i));     }     long long t_res = 0;     while (!Q.empty()) {       pair<long long, int> pp = *Q.begin();       long long tijd = pp.first;       int pid = pp.second;       Q.erase(Q.begin());       if (num_done[pid] > 0) {         onefree(num_done[pid] - 1, tijd);       }       if (num_done[pid] == 3) {         t_res = max(t_res, tijd - arrival[pid]);       } else {         try_putinto(pid, num_done[pid], tijd);       }     }     cout << t_res << endl;   }
#include <bits/stdc++.h>   using namespace std;   struct point {     double x;     double y;   };   int compare(const void *a, const void *b) {     point *aa, *bb;     aa = (point *)a;     bb = (point *)b;     if (aa->x > bb->x)       return 1;     else if (aa->x < bb->x)       return -1;     else       return aa->y > bb->y;   }   int main(void) {     double y1, y2, y3, x1, x2, x3;     set<int> X, Y;     int status(0);     point points[8];     for (int i = 0; i < 8; i++) {       std::cin >> points[i].x >> points[i].y;       if (points[i].x < 0 || points[i].y < 0) status = -1;       X.insert(points[i].x);       Y.insert(points[i].y);     }     if (status == -1 || X.size() != 3 || Y.size() != 3) {       std::cout <<  ugly ;       return 0;     }     set<int>::iterator itx = X.begin();     set<int>::iterator ity = Y.begin();     y1 = *ity;     ity++;     y2 = *ity;     ity++;     y3 = *ity;     x1 = *itx;     itx++;     x2 = *itx;     itx++;     x3 = *itx;     if (!(y1 < y2 && y2 < y3 && x1 < x2 && x2 < x3)) {       std::cout <<  ugly ;       return 0;     }     qsort(points, 8, sizeof(point), compare);     if (x1 == points[0].x && x1 == points[1].x && x1 == points[2].x &&         x2 == points[3].x && x2 == points[4].x && x3 == points[5].x &&         x3 == points[6].x && x3 == points[7].x && y1 == points[0].y &&         y1 == points[3].y && y1 == points[5].y && y2 == points[1].y &&         y2 == points[6].y && y3 == points[2].y && y3 == points[4].y &&         y3 == points[7].y)       std::cout <<  respectable ;     else       std::cout <<  ugly ;   }
#include <bits/stdc++.h>   using namespace std;   bool check = false;   string s;   int maxi, n, i, j, a;   int main() {     cin >> n;     vector<int> v;     for (i = 0; i < n; i++) {       maxi = 0;       for (j = 0; j < n; j++) {         cin >> a;         maxi = max(maxi, a);       }       if (check == false && maxi == n - 1) {         maxi = n;         check = true;       }       v.push_back(maxi);     }     for (i = 0; i < v.size(); i++) {       cout << v[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, p[200005];   int ans[200005];   queue<int> q;   int main() {     int Q;     cin >> Q;     while (Q--) {       memset(ans, 0, sizeof(ans));       cin >> n;       for (int i = 1; i <= n; i++) cin >> p[i];       for (int i = 1; i <= n; i++) {         if (ans[i]) continue;         int step = 1;         q.push(i);         for (int j = p[i]; j != i; j = p[j]) {           q.push(j);           step++;         }         while (!q.empty()) {           ans[q.front()] = step;           q.pop();         }       }       for (int i = 1; i <= n; i++) cout << ans[i] <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(51);   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     deque<pair<long long, long long>> q;     for (long long i = 0; i < n; i++) {       if (q.size() == 0) {         q.push_back({a[i], 1});       } else {         if (q.back().first == a[i]) {           q.back().second++;           if (q.size() > 1 && q[q.size() - 2].first >= q.back().first) {             long long now = q.back().second;             q.pop_back();             q.back().second += now;           }         } else if (q.back().first > a[i]) {           q.push_back({a[i], 1});         } else {           if (q.back().second % 2 == 1) {             cout <<  NO n ;             return 0;           } else {             q.push_back({a[i], 1});           }         }       }     }     while (q.size() > 1 && ((q[q.size() - 2].second % 2 == 0 &&                              q[q.size() - 2].first < q.back().first) ||                             (q.back().second % 2 == 0 &&                              q.back().first < q[q.size() - 2].first))) {       long long now = q.back().second;       long long mx = max(q.back().first, q[q.size() - 2].first);       q.pop_back();       q.back().first = mx;       q.back().second += now;     }     if (q.size() == 1) {       cout <<  YES n ;     } else {       cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, k;     cin >> n >> k;     string s;     cin >> s;     vector<long long> l_r, r_l;     long long count = k + 1;     for (long long i = 0; i < n; i++) {       if (s[i] ==  1 ) {         count = 0;         l_r.push_back(0);       } else if (s[i] ==  0 ) {         count++;         if (count > k)           l_r.push_back(1);         else           l_r.push_back(0);       }     }     count = k + 1;     for (long long i = n - 1; i >= 0; i--) {       if (s[i] ==  1 ) {         count = 0;         r_l.push_back(0);       } else if (s[i] ==  0 ) {         count++;         if (count > k)           r_l.push_back(1);         else           r_l.push_back(0);       }     }     reverse(r_l.begin(), r_l.end());     long long i = 0, ans = 0;     while (i < n) {       if (l_r[i] && r_l[i]) {         ans++;         i += k;       }       i++;     }     cout << ans <<   n ;     return;   }   int main() {     long long t;     cin >> t;     for (long long i = 0; i < t; i++) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, i, a, b, aux1, aux2, cs, cd, mij, rsp;   char s[200005];   int u[200005], r[200005], l[200005], d[200005];   int main() {     ios::sync_with_stdio(false);     cin >> n;     cin >> s;     cin >> a >> b;     if (abs(a) + abs(b) > n) {       cout << -1;       return 0;     }     if ((abs(a) + abs(b)) % 2 != n % 2) {       cout << -1;       return 0;     }     for (i = 0; i < n; i++) {       if (s[i] ==  U ) u[i + 1]++;       if (s[i] ==  D ) d[i + 1]++;       if (s[i] ==  L ) l[i + 1]++;       if (s[i] ==  R ) r[i + 1]++;     }     for (i = 1; i <= n; i++) {       u[i] += u[i - 1];       r[i] += r[i - 1];       d[i] += d[i - 1];       l[i] += l[i - 1];     }     if (u[n] - d[n] == b && r[n] - l[n] == a) {       cout << 0;       return 0;     }     rsp = n;     for (i = 1; i <= n; i++) {       cs = i;       cd = n;       while (cs <= cd) {         mij = (cs + cd) >> 1;         aux1 = r[i - 1];         aux1 -= l[i - 1];         aux1 += r[n] - r[mij];         aux1 -= l[n] - l[mij];         aux2 = u[i - 1];         aux2 -= d[i - 1];         aux2 += u[n] - u[mij];         aux2 -= d[n] - d[mij];         if (abs(aux1 - a) + abs(aux2 - b) <= mij - i + 1) {           rsp = min(rsp, mij - i + 1);           cd = mij - 1;         } else           cs = mij + 1;       }     }     cout << rsp;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       string ans;       ans = s;       sort(ans.begin(), ans.end());       int cnt = 0;       for (int i = 0; i < n; i++) {         if (ans[i] != s[i]) cnt++;       }       cout << cnt << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   vector<long long int> factors(long long int n) {     vector<long long int> v;     for (long long int i = 1; i * i <= n; i++) {       if (n % i == 0) {         if (i * i == n)           v.push_back(i);         else {           v.push_back(i);           v.push_back(n / i);         }       }     }     sort(v.begin(), v.end());     return v;   }   long long gcd(long long a, long long b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     std::ios_base::sync_with_stdio(false);     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int a[n], i, j;       for (i = 0; i < n; i++) {         cin >> a[i];       }       sort(a, a + n);       map<long long int, long long int> m;       long long int temp = a[n - 1];       vector<long long int> sol;       m[n - 1] = 1;       sol.push_back(temp);       long long int ans = temp, index;       for (i = 0; i < n; i++) {         long long int temp_gcd = INT_MIN;         for (j = 0; j < n; j++) {           if (m[j] == 0) {             long long int temp = gcd(ans, a[j]);             if (temp > temp_gcd) {               temp_gcd = temp;               index = j;             }           }         }         m[index] = 1;         sol.push_back(a[index]);         ans = temp_gcd;       }       for (i = 0; i < n; i++) {         cout << sol[i] <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  ||  9  < ch) f = (ch ^  - ) ? f : -1, ch = getchar();     while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return x * f;   }   int const N = 310;   double const EPS = 1e-8;   bool equal0(double x) { return -EPS < x && x < EPS; }   int n, m, T;   double a[N][N];   int b[N], id[N << 1];   void pivot(int x, int y) {     swap(id[n + x], id[y]);     double t = a[x][y];     a[x][y] = 1;     for (int i = 0; i <= n; i++) a[x][i] /= t;     for (int i = 0; i <= m; i++) {       if (i == x || equal0(a[i][y])) continue;       t = a[i][y], a[i][y] = 0;       for (int j = 0; j <= n; j++) a[i][j] -= a[x][j] * t;     }   }   bool simplex() {     while (true) {       int x = 0, y = 0;       for (int j = 1; !y && j <= n; j++)         if (a[0][j] > EPS) y = j;       if (!y) return true;       double minX = 1e18;       for (int i = 1; i <= m; i++)         if (a[i][y] > EPS && minX > a[i][0] / a[i][y])           minX = a[i][0] / a[i][y], x = i;       if (!x) {         puts( Unbounded );         return false;       }       pivot(x, y);     }   }   bool solve() {     for (int i = 1; i <= n; i++) id[i] = i;     while (true) {       int x = 0, y = 0;       for (int i = 1; i <= m; i++)         if (a[i][0] < -EPS && (!x || rand() & 1)) x = i;       if (!x) return simplex();       for (int j = 1; j <= n; j++)         if (a[x][j] < -EPS && (!y || rand() & 1)) y = j;       if (!y) {         puts( Infeasible );         return false;       }       pivot(x, y);     }   }   double ans[N];   int main() {     scanf( %d%d , &n, &m);     int _a, _b, _d;     memset(a, 0, sizeof a);     for (int i = 1; i <= m; i++) {       a[i][0] = -1;     }     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &_a, &_b, &_d);       a[0][_a] -= _d;       a[0][_b] += _d;       a[i][_a] = -1;       a[i][_b] = 1;     }     T = 1;     if (solve()) {       if (!T) return 0;       for (int i = 1; i <= m; i++) ans[id[n + i]] = a[i][0];       for (int i = 1; i <= n; i++) printf( %d  , (int)ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   multiset<int, greater<int> > s1;   multiset<int> s2;   int bs1, bs2, n, a[500500], b[500500], ct, nomb[500500], cr, noma[500500];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       noma[a[i]] = i;     }     for (int i = 0; i < n; i++) {       scanf( %d , &b[i]);       nomb[b[i]] = i;     }     s1.clear();     s2.clear();     bs1 = 0;     bs2 = 0;     for (int i = 0; i < n; i++) {       ct = nomb[a[i]] - i;       if (ct <= 0)         s1.insert(ct);       else         s2.insert(ct);     }     for (int i = 0; i < n; i++) {       cr = 1000000000;       if (!s1.empty()) {         int qt = *s1.begin();         cr = min(cr, (-(qt - bs1)));       }       if (!s2.empty()) cr = min(cr, (*s2.begin()) - bs2);       printf( %d n , cr);       ct = 0 - noma[b[i]] + bs1;       s1.erase(s1.find(ct));       ct = n - 1 - noma[b[i]] + bs2 + 1;       s2.insert(ct);       bs1++;       bs2++;       while ((!s2.empty()) && (*s2.begin() == bs2)) {         s2.erase(s2.begin());         s1.insert(0 + bs1);       }     }   }