malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int a[n];       long long int sum = 0;       for (int i = 0; i < n; i++) {         cin >> a[i];         if (!(i & 1)) sum += a[i];       }       long long int best = 0;       long long int cur = 0;       long long int z, z1;       if (n & 1)         z = n - 1;       else         z = n;       for (int i = 0; i < z; i += 2) {         cur = max(0ll, cur + a[i + 1] - a[i]);         best = max(cur, best);       }       cur = 0;       if (!(n & 1))         z1 = n - 1;       else         z1 = n;       for (int i = 1; i < z1; i += 2) {         cur = max(0ll, cur + a[i] - a[i + 1]);         best = max(cur, best);       }       cout << sum + best << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 35000 + 10;   const int MAXK = 55;   int tree[MAXN << 2], info[MAXN << 2];   int dp[MAXK][MAXN];   int nums[MAXN];   int pre[MAXN], cur[MAXN];   void build(int node, int start, int end, int i) {     if (start == end) {       tree[node] = dp[i][start - 1];       info[node] = 0;       return;     }     int mid = (start + end) >> 1;     build((node << 1), start, mid, i);     build(((node << 1) | 1), mid + 1, end, i);     tree[node] = max(tree[(node << 1)], tree[((node << 1) | 1)]);     info[node] = 0;   }   void update(int node, int start, int end, int i, int j, int val) {     if (end < i || j < start) return;     if (i <= start && end <= j) {       tree[node] += val;       info[node] += val;       return;     }     int mid = (start + end) >> 1;     if (info[node] != 0) {       update((node << 1), start, mid, start, mid, info[node]);       update(((node << 1) | 1), mid + 1, end, mid + 1, end, info[node]);       info[node] = 0;     }     update((node << 1), start, mid, i, j, val);     update(((node << 1) | 1), mid + 1, end, i, j, val);     tree[node] = max(tree[(node << 1)], tree[((node << 1) | 1)]);   }   int query(int node, int start, int end, int i, int j) {     if (end < i || j < start) return INT_MIN;     if (i <= start && end <= j) {       return tree[node];     }     int mid = (start + end) >> 1;     if (info[node] != 0) {       update((node << 1), start, mid, start, mid, info[node]);       update(((node << 1) | 1), mid + 1, end, mid + 1, end, info[node]);       info[node] = 0;     }     return max(query((node << 1), start, mid, i, j),                query(((node << 1) | 1), mid + 1, end, i, j));   }   int main() {     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       scanf( %d , &nums[i]);       pre[i] = cur[nums[i]];       cur[nums[i]] = i;     }     for (int i = 1; i <= k; i++) {       build(1, 1, n, i - 1);       for (int j = i; j <= n; j++) {         update(1, 1, n, pre[j] + 1, j, 1);         dp[i][j] = query(1, 1, n, 1, j);       }     }     cout << dp[k][n] << endl;   }
#include <bits/stdc++.h>   using namespace std;   typedef struct {     int x;     int y;     long long z;     long long o;   } H;   long long b, g, i, j, k, sz, t, p[200040], w[200040];   int pv[200040];   H e[400005];   int C(H a, H b) { return a.x < b.x; }   void BP(void);   void S(void);   long long dfs(int v);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     for (cin >> t; t--;) {       S();       BP();       k++;       for (i = 0; i++ < k;) {         if (w[i] < 2 && !p[e[pv[i]].y]) {           dfs(pv[i]);           p[e[pv[i]].y] = 1;         }       }       for (b = g = i = 0; i < sz; i++) {         if (!e[i].o) {           if (w[e[i].y] < 2)             e[i].o = 1;           else             e[i].o = k - 1;         }         if (e[i].o * 2 < k || e[i].o * 2 == k && e[i].x < e[i].y) {           g += e[i].o % 2 * e[i].z;           b += e[i].o * e[i].z;         }       }       cout << g <<     << b <<   n ;       for (i = 0; i++ < k + 1;) p[i] = pv[i] = w[i] = 0;       for (i = 0; i < sz + 1; i++) e[i].o = 0;     }   }   void BP(void) {     for (int i = 0; ++i < sz;) {       if (e[i].x > e[i - 1].x) {         for (int j = e[i - 1].x; j++ < e[i].x;) {           pv[j] = i;         }       }     }     for (int j = e[sz - 1].x; j++ < k + 2;) pv[j] = sz;   }   void S(void) {     cin >> k;     k = 2 * k - 1;     for (int i = 0; i < k; i++) {       cin >> e[i].x >> e[i].y >> e[i].z;       e[i + k].x = e[i].y;       e[i + k].y = e[i].x;       e[i + k].z = e[i].z;       w[e[i].x]++;       w[e[i].y]++;     }     sz = 2 * k;     sort(e, e + sz, C);   }   long long dfs(int v) {     long long s = 0;     if (e[v].o) return e[v].o;     for (int i = pv[e[v].y]; i < pv[e[v].y + 1]; i++) {       if (e[i].y != e[v].x) s += dfs(i);     }     return e[v].o = s + 1;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     if ((a >= 0 && b < 0) || (a < 0 && b >= 0)) {       cout <<  -1 ;       return 0;     }     if (a == 0 && b == 0) {       cout <<  -1 ;       return 0;     }     if (a < 0 && b < 0) {       cout <<  -1 ;       return 0;     }     if (b == 0 && a > 0) {       cout <<  -1 ;       return 0;     }     if (a == b) {       cout << a;       return 0;     }     if (a > 0 && b > 0) {       if (b > a) {         cout <<  -1 ;         return 0;       } else {         int k1 = (a - b) / (2 * b);         double x1 = (double)(a - b) / (2 * k1);         int k2 = (a + b) / (2 * b);         double x2 = (double)(a + b) / (2 * k2);         cout << setprecision(10) << min(x1, x2);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int fr[301][301];     int al[301];     int i, j, n;     cin >> n;     for (i = 1; i <= n; ++i)       for (j = 1; j <= n; ++j) cin >> fr[i][j];     for (i = 1; i <= n; ++i) cin >> al[i];     int k, best[301], best_time[301], cur;     for (j = 1; j <= n; ++j) {       best[j] = 301;       best_time[j] = 301;       for (i = 1; i <= n; ++i) {         if (j == 1 && i == 1) continue;         for (k = 1; k <= n; ++k)           if (al[k] <= i && al[k] != j) {             cur = al[k];             break;           }         for (k = 1; k <= n; ++k) {           if (cur == fr[j][k]) {             if (k < best[j]) {               best[j] = k;               best_time[j] = i;             }             break;           }         }       }     }     for (i = 1; i <= n; ++i) cout << best_time[i] <<   t ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int x = 0, xi = INT_MIN, q = 0;     vector<int> v;     for (int i = 0; i < n; i++) {       int y;       cin >> y;       q = x;       v.push_back(y);       xi = max(x, v[i]);       if (x != q) x = i;     }     long long ans = 0;     for (int i = 0; i < n; i++) {       int z = (abs(x - i) + i + x) * 2;       ans += (z * v[i]);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int min(int _a, int _b) { return _a < _b ? _a : _b; }   inline int max(int _a, int _b) { return _a > _b ? _a : _b; }   template <class _T>   inline void rd(_T &_a) {     int _f = 0, _ch = getchar();     _a = 0;     while (_ch <  0  || _ch >  9 ) {       if (_ch ==  - ) _f = 1;       _ch = getchar();     }     while (_ch >=  0  && _ch <=  9 ) _a = _a * 10 + _ch -  0 , _ch = getchar();     if (_f) _a = -_a;   }   const int inf = 0x3f3f3f3f;   const double eps = 1e-8;   const int mod = 1e9 + 7, N = 15;   int mp[N][N], f[N][N], ct[N], n, m, k, ans;   void dfs(int x, int y) {     if (y > m) x++, y = 1;     if (x > n) {       ans++;       ans %= mod;       return;     }     int st = f[x - 1][y] | f[x][y - 1], pre, d = -1;     if (n + m - x - y + 1 > k - __builtin_popcount(st)) return;     for (int i = 1; i <= k; i++)       if (!(st >> i - 1 & 1) && (!mp[x][y] || mp[x][y] == i)) {         f[x][y] = st | (1 << i - 1);         ct[i]++;         if (ct[i] == 1) {           if (~d)             ans += d;           else {             pre = ans;             dfs(x, y + 1);             d = ans - pre;           }         } else           dfs(x, y + 1);         ans %= mod;         ct[i]--;       }   }   int main() {     rd(n);     rd(m);     rd(k);     if (n + m - 1 > k) return puts( 0 ), 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         rd(mp[i][j]);         if (mp[i][j]) ct[mp[i][j]]++;       }     dfs(1, 1);     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a, b, n, m;   int A[100010], B[100010], L[100010];   double dis(int a, int b, int c, int d) {     return sqrt(         double((a - c + 0.0) * (a - c + 0.0) + (b - d + 0.0) * (b - d + 0.0)));   }   double getK(int y) { return 1.0 * y * a / (b * 1.0); }   int find(double xx) {     int x = xx;     return upper_bound(A, A + n, x) - A;   }   int main() {     int ll, rr, id;     double ans = 0, tmp;     scanf( %d%d%d%d , &n, &m, &a, &b);     for (int i = 0; i < n; i++) scanf( %d , A + i);     for (int i = 0; i < m; i++) scanf( %d , B + i);     for (int i = 0; i < m; i++) scanf( %d , L + i);     ll = rr = 0;     ans = dis(0, 0, a, A[0]) + dis(a, A[0], b, B[0]) + L[0];     for (int i = 0; i < m; i++) {       double k = getK(B[i]);       id = find(k);       id = max(0, id - 2);       for (int j = id; j < n && j <= id + 4; j++) {         tmp = dis(0, 0, a, A[j]) + dis(a, A[j], b, B[i]) + 1.0 * L[i];         if (tmp < ans) ans = tmp, ll = j, rr = i;       }     }     printf( %d %d n , ll + 1, rr + 1);   }
#include <bits/stdc++.h>   using namespace std;   long long int base(long long int n, long long int k) {     string res =   ;     long long int temp = 0, i;     while (n >= k) {       res += to_string(n % k);       n /= k;     }     res += to_string(n);     reverse(res.begin(), res.end());     for (i = 0; i < res.length(); i++) {       temp = temp * 10 + (res[i] -  0 );     }     return temp;   }   int main() {     long long int n, i, j, flag = 0;     cin >> n;     flag = 9;     for (i = 1; i <= n - 1; i++) {       for (j = 1; j <= n - 1; j++) {         cout << base(i * j, n) <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int adj[205][205];   int rgraph[205][205];   int n, m;   int parent[205];   bool visited[205];   int a[105];   int b[105];   bool bfs(int s, int t) {     memset(visited, 0, sizeof(visited));     queue<int> q;     q.push(s);     visited[s] = true;     parent[s] = -1;     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = 0; i <= 2 * n + 1; i++) {         if (!visited[i] && rgraph[u][i] > 0) {           q.push(i);           parent[i] = u;           visited[i] = true;         }       }     }     return visited[t] == true;   }   int solve(int s, int t) {     for (int i = 0; i <= 2 * n + 1; i++) {       for (int j = 0; j <= 2 * n + 1; j++) {         rgraph[i][j] = adj[i][j];       }     }     int max_flow = 0;     while (bfs(0, 2 * n + 1)) {       int path = 2000000000;       for (int i = t; i != s; i = parent[i]) {         int u = parent[i];         path = min(path, rgraph[u][i]);       }       for (int i = t; i != s; i = parent[i]) {         int u = parent[i];         rgraph[u][i] -= path;         rgraph[i][u] += path;       }       max_flow += path;     }     return max_flow;   }   int main() {     cin >> n >> m;     int suma = 0, sumb = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];       adj[0][i] = a[i];       suma += a[i];     }     for (int i = 1; i <= n; i++) {       cin >> b[i];       adj[n + i][2 * n + 1] = b[i];       sumb += b[i];       adj[i][n + i] = 1000000;     }     for (int i = 0; i < m; i++) {       int c, d;       cin >> c >> d;       adj[c][n + d] = 1000000;       adj[d][n + c] = 1000000;     }     int ans = solve(0, 2 * n + 1);     if (ans == suma && ans == sumb) {       printf( YES n );       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           if (rgraph[i][n + j] != 0)             printf( %d  , 1000000 - rgraph[i][n + j]);           else             printf( %d  , rgraph[i][n + j]);         }         printf(  n );       }     } else {       printf( NO n );     }   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     for (long long test = 1; test <= t; test++) {       long long n;       cin >> n;       vector<long long> a;       for (long long i = 0; i <= n - 1; i++) {         long long x;         cin >> x;         if (x != 0) a.push_back(x);       }       long long bits[61] = {0};       n = a.size();       for (long long i = 0; i <= n - 1; i++) {         long long temp = a[i];         long long j = 0;         while (temp > 0) {           if (temp & 1) bits[j]++;           temp = temp >> 1;           j++;         }       }       for (long long i = 0; i <= 60; i++)         if (bits[i] >= 3) {           cout << 3;           exit(0);         }       long long cyclec = -1;       vector<vector<long long>> adj(n);       for (long long i = 0; i < n; i++)         for (long long j = i + 1; j < n; j++)           if ((a[i] & a[j]) != 0) {             adj[i].push_back(j);             adj[j].push_back(i);           }       for (long long i = 0; i <= n - 1; i++)         if (adj[i].size() != 0) {           vector<long long> dis(n, -1);           vector<long long> par(n, -1);           queue<long long> q;           q.push(i);           dis[i] = 0;           par[i] = -1;           while (!q.empty()) {             auto x = q.front();             q.pop();             for (auto y : adj[x])               if (dis[y] == -1) {                 dis[y] = 1 + dis[x];                 par[y] = x;                 q.push(y);               } else if (y != par[x]) {                 if (cyclec == -1)                   cyclec = (dis[y] + dis[x] + 1);                 else                   cyclec = min(cyclec, dis[y] + dis[x] + 1);               }           }         }       cout << cyclec;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 200001;   std::vector<long long> g[N];   long long s[N];   long long h[N];   long long a[N] = {0};   bool visited[N] = {false};   void dfs1(long long n, long long parent, long long depth) {     h[n] = depth;     for (long long v : g[n]) {       if (v != parent) {         dfs1(v, n, depth + 1);       }     }   }   void dfs2(long long n, long long parent) {     if (h[n] % 2 == 0) {       for (long long v : g[n]) {         if (v != parent) {           a[n] = s[v] - s[parent];         }       }       for (long long v : g[n]) {         if (v != parent) {           a[n] = min(s[v] - s[parent], a[n]);         }       }       s[n] = a[n] + s[parent];     } else {       a[n] = s[n] - s[parent];     }     for (long long v : g[n]) {       if (v != parent) {         dfs2(v, n);       }     }   }   int main() {     long long n;     cin >> n;     for (long long i = 2; i <= n; i++) {       long long parent;       cin >> parent;       g[parent].push_back(i);       g[i].push_back(parent);     }     for (long long i = 1; i <= n; i++) {       cin >> s[i];     }     dfs1(1, -1, 1);     a[1] = s[1];     dfs2(1, n + 1);     long long sum = 0;     for (long long i = 1; i <= n; i++) {       if (a[i] < 0) {         cout <<  -1  << endl;         return 0;       }       sum += a[i];     }     cout << sum << endl;   }
#include <bits/stdc++.h>   template <typename C>   int sz(const C &c) {     return c.size();   }   using namespace std;   int main() {     iostream::sync_with_stdio(false);     int64_t n;     cin >> n;     int k;     cin >> k;     int64_t maxV = 0;     int maxI = 1;     int64_t maxC = 0;     for (int i = 1; i <= k; ++i) {       int64_t a;       cin >> a;       if (a * (n / a) > maxV) {         maxV = a * (n / a);         maxI = i;         maxC = n / a;       }     }     cout << maxI <<     << maxC <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long a[10], rest[10];   int n, res = 1e9, pre[10], tmp, size[10], flag = 0;   vector<int> ve[10];   map<long long, long long> mp;   int count(long long x) {     if (mp[x]) return mp[x];     long long tmp = x;     long long res = 0;     for (long long i = 2; i * i <= x; i++) {       if (x % i == 0) {         while (x % i == 0) {           x /= i;           res++;         }       }     }     if (x > 1) res++;     return mp[tmp] = res;   }   bool prime(long long x) {     if (x == 2 || x == 3) return true;     for (long long i = 2; i * i <= x; i++)       if (x % i == 0) return false;     return true;   }   int isprime[10] = {0};   void cal(int now) {     tmp++;     if (size[now] == 0) {       if (!isprime[now]) tmp += count(a[now]);       return;     }     if (now) {       if (rest[now]) tmp += count(rest[now]);     }     for (int i = 0; i < size[now]; i++) {       cal(ve[now][i]);       if (flag) return;     }   }   void dfs(int pos) {     if (pos == n + 1) {       tmp = 0;       flag = 0;       for (int i = 0; i <= n; i++) {         ve[i].clear();         size[i] = 0;       }       for (int i = 1; i <= n; i++) {         size[pre[i]]++;         ve[pre[i]].push_back(i);       }       cal(0);       if (size[0] == 1) {         tmp -= 1;       }       if (flag) tmp = 1e9;       if (tmp < res) {         res = tmp;       }       return;     }     for (int i = 0; i < pos; i++) {       if (i && rest[i] % a[pos]) continue;       rest[i] = rest[i] / a[pos];       pre[pos] = i;       dfs(pos + 1);       rest[i] *= a[pos];     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %lld , a + i);     sort(a + 1, a + n + 1, greater<long long>());     for (int i = 1; i <= n; i++) {       rest[i] = a[i];       if (prime(a[i])) isprime[i] = 1;     }     dfs(1);     printf( %d n , res);   }
#include <bits/stdc++.h>   using namespace std;   int n, m, INF = 2e9;   vector<vector<int>> value;   void shift_column(int j, int k) {     vector<int> res(n);     for (int i = 0; i < n; i++) res[(i - k + n) % n] = value[i][j];     for (int i = 0; i < n; i++) value[i][j] = res[i];   }   void run() {     cin >> n >> m;     value.resize(n, vector<int>(m));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> value[i][j];       }     }     int ans = 0;     for (int j = 0; j < m; j++) {       map<int, int> pos;       vector<int> cnt(n);       for (int i = 0; i < n; i++) pos[1 + j + i * m] = i;       for (int i = 0; i < n; i++) {         if (pos.find(value[i][j]) == pos.end()) continue;         int shift = (i - pos[value[i][j]] + n) % n;         cnt[shift]++;       }       auto best_shift =           distance(cnt.begin(), max_element(cnt.begin(), cnt.end()));       shift_column(j, best_shift);       int best_add = INF;       for (int i = 0; i < n; i++) best_add = min(best_add, n - cnt[i] + i);       ans += best_add;     }     cout << ans;   }   signed main() {     cout.setf(ios::fixed);     cout.precision(10);     cin.tie(0);     cout.tie(0);     ios::sync_with_stdio(0);     run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, a[300100], b[300100], pos[300100];   vector<pair<int, int> > ans;   inline void ws(int xx, int yy) { ans.push_back(make_pair(xx, yy)); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       pos[a[i]] = i;     }     if (a[1] != 1) {       int p = pos[1];       int x = a[1];       if (p - 1 >= n / 2) {         ws(1, p);         a[1] = 1;         pos[1] = 1;         pos[x] = p;         a[p] = x;       } else if (n - p >= n / 2) {         ws(p, n);         ws(1, n);         ws(p, n);         a[1] = 1;         pos[1] = 1;         pos[x] = p;         a[p] = x;       }     }     if (a[n] != n) {       int p = pos[n];       int x = a[1];       int z = a[n];       if (p - 1 >= n / 2) {         ws(1, p);         ws(1, n);         ws(1, p);         a[p] = z;         pos[z] = p;         pos[n] = n;         a[n] = n;       } else if (n - p >= n / 2) {         ws(p, n);         a[p] = z;         pos[z] = p;         pos[n] = n;         a[n] = n;       }     }     for (int i = 2; i < n; ++i) {       if (pos[i] == i) continue;       int p = pos[i];       int x = a[1];       int y = a[i];       int z = a[n];       if (p - 1 >= n / 2 && i - 1 >= n / 2) {         ws(1, p);         ws(1, i);         ws(1, p);         swap(a[i], a[pos[i]]);         pos[y] = p;         pos[i] = i;       } else if (p - 1 >= n / 2 && n - i >= n / 2) {         ws(1, p);         ws(1, n);         ws(i, n);         ws(1, n);         ws(1, p);         swap(a[i], a[p]);         pos[y] = p;         pos[i] = i;       } else if (n - p >= n / 2 && i - 1 >= n / 2) {         ws(p, n);         ws(1, n);         ws(1, i);         ws(1, n);         ws(p, n);         swap(a[i], a[p]);         pos[y] = p;         pos[i] = i;       } else if (n - p >= n / 2 && n - i >= n / 2) {         ws(p, n);         ws(i, n);         ws(p, n);         swap(a[i], a[p]);         pos[y] = p;         pos[i] = i;       }     }     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); ++i) {       printf( %d %d n , ans[i].first, ans[i].second);     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( no-stack-protector )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native )   #pragma GCC optimize( fast-math )   using namespace std;   inline void ForSpeed() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const int MAXN = int(1e5 + 5);   const int inf = 1e9 + 7;   const int MOD = 998244353;   const long long INF = (1e18 + 7);   long long b, k, a[MAXN];   int main() {     ForSpeed();     cin >> b >> k;     for (int i = 0; i < k; i++) {       cin >> a[i];     }     if (k == 1) {       if (a[0] % 2 != 0 && b % 2 != 0) {         cout <<  odd ;         return 0;       }     }     if (b % 2 == 0 && a[k - 1] % 2 == 0) {       cout <<  even ;       return 0;     } else if (b % 2 == 0 && a[k - 1] % 2 != 0) {       cout <<  odd ;       return 0;     }     long long ch = 0;     if (a[0] % 2 == 0) {       ch = 1;     }     for (int i = 1; i < k - 1; i++) {       if (ch == 0 && a[i] % 2 != 0) {         ch = 1;       } else if (ch == 1 && a[i] % 2 == 0) {         continue;       } else {         ch = 0;       }     }     if (ch == 1 && a[k - 1] % 2 == 0) {       cout <<  even ;     } else if (ch == 0 && a[k - 1] % 2 != 0) {       cout <<  even ;     } else {       cout <<  odd ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod =  1000000007;   const int LIM = 100005;   #define int long long   #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)   #define mem(a,b) memset(a,b,sizeof(a))   #define all(a) a.begin() , a.end()   #define pii pair<int,int>   #define vi vector<int>   #define endl   n    #define pb push_back   #define sp <<   <<   #define ss second   #define ff first   int power(int x, int n){       int res = 1;       while(n){             if(n&1){                   res = res * x % mod;             }             x = x * x % mod;             n>>=1;       }       return (res%mod);   }   /*bool prime[1000006];   void sieve(int n)   {         memset(prime, true, sizeof(prime));         int rootn = (int)sqrt(n);         for (int p = 2; p <= rootn; p++)               if(prime[p] == true)                     for(int i = p*p; i <= n; i += p)                           prime[i] = false;         prime[1] = 0;      }*/   /*   int fac[300005];   int ncr(int n,int r)   {         return fac[n]*((power(fac[n-r], mod-2)*power(fac[r], mod-2))%mod)%mod;   }   */   bool check(int a[],int n)   {    for(int i=1;i<n;i++)    {     if(a[i]<a[i-1])return false;    }    return true;   }      int32_t main()   {     IOS;     int tt=1,n,x,k,y,z,i,j;     cin>>tt;     while(tt--){       cin>>n>>x;     int a[n];     for(int i=0;i<n;i++)     {      cin>>a[i];     }     int ans=0;     for(int i=0;i<=n;i++)     {      if(check(a,n))break;      ans++;      for(int j=0;j<n;j++)      {       if(a[j]>x)       {        swap(a[j],x);        break;       }      }     }     if(check(a,n))cout<<ans<<endl;     else cout<<-1<<endl;     }   }