malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long int nodes_size;   long long int edge_size;   class edge {    public:     long long int node;     long long int cost;     edge(long long int i, long long int j) {       node = i;       cost = j;     }   };   class graph {    public:     long long int node;     vector<edge> edges;     long long int parent;     long long int cost;     vector<long long int> children;   } nodes[1005];   long long int visited[1005];   class point {    public:     long long int x;     long long int y;     point() {}     point(long long int i, long long int j) {       x = i;       y = j;     }   };   void fill_array(long long int ar[], long long int value, long long int size) {     for (long long int i = 0; i < size; i++) ar[i] = value;   }   void print_ll_vector(vector<long long int> v) {     for (long long int i = 0; i < v.size(); i++) cout << v[i] <<    ;     cout << endl;   }   void input_graph(bool is_undirected, bool is_cost) {     cin >> nodes_size;     edge_size = nodes_size - 1;     for (long long int i = 0; i < edge_size; i++) {       long long int a, b, c;       cin >> a >> b;       if (is_cost) {         cin >> c;         nodes[a].edges.push_back(edge(b, c));         if (is_undirected) nodes[b].edges.push_back(edge(a, c));       } else {         nodes[a].edges.push_back(edge(b, 1));         if (is_undirected) nodes[b].edges.push_back(edge(a, 1));       }     }   }   vector<pair<long long int, long long int> > destroyed_edges;   long long int ctr = 0, c;   vector<long long int> ans;   void dfs(long long int node, long long int parent) {     visited[node] = 1;     for (long long int i = 0; i < nodes[node].edges.size(); i++) {       if (nodes[node].edges[i].node != parent) {         if (visited[nodes[node].edges[i].node]) {           if (node > nodes[node].edges[i].node) {             if (c > 0 || ctr == 1) {               destroyed_edges.push_back(                   make_pair(node, nodes[node].edges[i].node));               c++;             } else {               c++;               ans.push_back(node);               ans.push_back(nodes[node].edges[i].node);             }           }         } else           dfs(nodes[node].edges[i].node, node);       }     }   }   vector<long long int> new_edges;   int main() {     input_graph(true, false);     fill_array(visited, 0, nodes_size + 1);     for (long long int i = 1; i <= nodes_size; i++) {       if (!visited[i]) {         c = 0;         ctr++;         dfs(i, -1);         if (c == 0 && i != 1) new_edges.push_back(i);       }     }     cout << (ans.size() / 2) + new_edges.size() << endl;     for (long long int i = 0; i < ans.size();) {       cout << ans[i] <<     << ans[i + 1] <<     << ans[i] <<     << 1 << endl;       i = i + 2;     }     for (long long int i = 0; i < new_edges.size(); i++)       cout << destroyed_edges[i].first <<     << destroyed_edges[i].second <<                << destroyed_edges[i].first <<     << new_edges[i] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   int main(void) {     ios_base::sync_with_stdio(false);     int N;     cin >> N;     long long minx = inf, maxx = -inf, miny = inf, maxy = -inf;     vector<pair<long long, long long>> A(N);     for (int i = 0; i < N; ++i) {       cin >> A[i].first >> A[i].second;       minx = min(minx, A[i].first);       miny = min(miny, A[i].second);       maxx = max(maxx, A[i].first);       maxy = max(maxy, A[i].second);     }     long long ans4 = 2 * (maxx - minx) + 2 * (maxy - miny);     long long ans3 = 0;     for (int s = 0; s < (1 << 6); ++s) {       vector<int> cntx(3, 0), cnty(3, 0);       for (int i = 0; i < 3; ++i) {         if (s & (1 << i)) {           cntx[i]++;           cntx[(i + 1) % 3]--;         } else {           cntx[i]--;           cntx[(i + 1) % 3]++;         }         if (s & (1 << (3 + i))) {           cnty[i]++;           cnty[(i + 1) % 3]--;         } else {           cnty[i]--;           cnty[(i + 1) % 3]++;         }       }       long long perim = 0;       for (int i = 0; i < 3; ++i) {         long long best = -1e18;         for (auto& p : A) {           best = max(best, p.first * cntx[i] + p.second * cnty[i]);         }         perim += best;       }       ans3 = max(ans3, perim);     }     cout << ans3;     for (int i = 4; i <= N; ++i) {       cout <<     << ans4;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int INF = LONG_LONG_MAX;   const long long int NINF = LONG_LONG_MIN;   const long long int mmod = 998244353;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long int t;     cin >> t;     while (t-- > 0) {       long long int c, m, x;       cin >> c >> m >> x;       long long int a = min(c, m);       cout << min(a, (c + m + x) / 3) << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 5;   long long mod = 1000000007;   vector<long long> gr[200001];   vector<long long> grev[200001];   long long a[200005];   void solve() {     long long n, d, a, b;     cin >> n >> d >> a >> b;     vector<pair<long long, long long> > ans;     for (long long i = int(0); i < int(n); i++) {       long long x, y;       cin >> x >> y;       long long val = x * a + y * b;       ans.push_back({val, i + 1});     }     sort(ans.begin(), ans.end());     long long val = 0;     vector<long long> ind;     long long i = 0;     for (i = 0; i < n; i++) {       val += ans[i].first;       if (val <= d)         ind.push_back(ans[i].second);       else         break;     }     cout << i <<   n ;     for (long long i = int(0); i < int(ind.size()); i++) {       cout << ind[i] <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = int(2e5) + 9;   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int n = s.size();       int m1 = 0, m2 = 0;       bool on = false;       int n1 = 0, n0 = 0, N1 = 0, N0 = 0;       for (int i = 0; i < n; i++) {         n0 += (s[i] ==  0 );         n1 += (s[i] ==  1 );       }       int ans = n;       for (int i = 0; i < n; i++) {         N0 += (s[i] ==  0 );         N1 += (s[i] ==  1 );         n0 -= (s[i] ==  0 );         n1 -= (s[i] ==  1 );         ans = min(ans, N0 + n1);         ans = min(ans, N1 + n0);       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e4 + 50;   const int INF = 1e8 + 100;   int val[MAXN], vec[MAXN];   int dp[MAXN][100];   int n, t;   bool check(int x) { return x == ((1 << 3) - 1); }   int solve(int id, int suco) {     if (check(suco)) return 0;     if (id >= n) return (INF);     if (dp[id][suco] != -1) return dp[id][suco];     int ans = INF;     ans = min(solve(id + 1, (suco | vec[id])) + val[id], solve(id + 1, suco));     return dp[id][suco] = ans;   }   int32_t main() {     cin >> n;     string s;     for (int i = 0; i < n; i++) {       int aux = 0;       cin >> t >> s;       for (auto c : s) aux |= (1 << (int(c -  A )));       vec[i] = aux;       val[i] = t;     }     memset(dp, -1, sizeof dp);     int ans = solve(0, 0);     cout << (ans == INF ? -1 : ans) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s1 =        What are you doing at the end of the world? Are you busy? Will you save          us? ;   string s2 =  What are you doing while sending   ;   string s3 =   ? Are you busy? Will you send   ;   string s4 =   ? ;   long long M[100005];   char f(long long n, long long k) {     if (n == 0) return s1[k];     if (k < s2.size()) return s2[k];     k = k - s2.size();     if (k < M[n - 1]) return f(n - 1, k);     k = k - M[n - 1];     if (k < s3.size()) return s3[k];     k = k - s3.size();     if (k < M[n - 1]) return f(n - 1, k);     k = k - M[n - 1];     if (k < s4.size()) return s4[k];     k = k - s4.size();     return  . ;   }   int main() {     ios_base::sync_with_stdio(0);     long long q;     cin >> q;     M[0] = s1.size();     for (long long i = 1; i <= 53; i++) {       if (M[i - 1])         M[i] = s2.size() + M[i - 1] + s3.size() + M[i - 1] + s4.size();     }     for (long long i = 54; i <= 100001; i++) M[i] = M[53];     for (long long i = 0; i < q; i++) {       long long n, k;       cin >> n >> k;       long long n2 = n;       if (n2 > 53) n2 = 53;       if (k > M[n2])         cout <<  . ;       else         cout << f(n, k - 1);     }   }
#include <bits/stdc++.h>   using namespace std;   int d[1503][1503][26], e[1503][26], z[1503][26];   int main() {     ios::sync_with_stdio(false);     int n, q, m;     char c;     string s;     cin >> n >> s >> q;     for (int i = 0; i < 26; i++)       for (int j = 1; j <= n; j++) e[j][i] = e[j - 1][i] + (s[j - 1] ==  a  + i);     for (int i = 1; i <= n; i++)       for (int j = i; j <= n; j++)         for (int k = 0; k < 26; k++) d[i][j][k] = e[j][k] - e[i - 1][k];     for (int i = 1; i <= n; i++)       for (int j = 0; j < 26; j++) {         int l = 1;         for (int k = 1; k <= n; k++) {           while (d[l][k][j] + i < k - l + 1) l++;           z[i][j] = max(z[i][j], k - l + 1);         }       }     while (q--) cin >> m >> c, cout << z[m][c -  a ] <<   n ;   }
#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 * (b / gcd(a, b)); }   int n, arr[109];   vector<int> odd, even;   int main() {     scanf( %d , &n);     for (int i = 0; i < (int)(n); i++) {       scanf( %d , &arr[i]);       if (arr[i] % 2 == 0)         even.push_back(i);       else         odd.push_back(i);     }     if (even.size() == n - 1)       printf( %d n , odd[0] + 1);     else       printf( %d n , even[0] + 1);   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int data;     long int sum;     struct node *left, *right;     struct node* next;   };   struct node* newNode(int data) {     struct node* Node = new (struct node);     Node->data = data;     Node->left = Node->right = Node->next = NULL;     return (Node);   }   long long ans[110][110][2];   void solveTestCase() {     int n1, n2, k1, k2;     scanf( %d , &n1);     ;     scanf( %d , &n2);     ;     scanf( %d , &k1);     ;     scanf( %d , &k2);     ;     k1 = min(k1, n1);     k2 = min(k2, n2);     ans[0][0][0] = 1;     ans[0][0][1] = 1;     for (int i = 0; i < n1 + 1; i++) {       for (int j = 0; j < n2 + 1; j++) {         if (i == 0 && j == 0) {           continue;         }         for (int k = 1; k < k1 + 1; k++) {           if (i - k < 0) {             break;           }           ans[i][j][0] = (ans[i][j][0] + ans[i - k][j][1]) % 100000000;         }         for (int k = 1; k < k2 + 1; k++) {           if (j - k < 0) {             break;           }           ans[i][j][1] = (ans[i][j][1] + ans[i][j - k][0]) % 100000000;         }       }     }     long long ans_final = (ans[n1][n2][0] + ans[n1][n2][1]) % 100000000;     printf( %I64d n , ans_final);     ;   }   int main() {     solveTestCase();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   long long a[100001];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     scanf( %d , &m);     int Left = 1;     for (; m--;) {       int x, y;       scanf( %d%d , &x, &y);       while (Left < x) {         a[Left + 1] = max(a[Left + 1], a[Left]);         ++Left;       }       printf( %I64d n , a[Left]);       a[Left] += y;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x * f;   }   long long n;   long long f[35][35][2];   inline long long power(long long x, long long y) {     if (y < 0) return 1;     long long ans = 1;     for (; y; y >>= 1) {       if (y & 1) ans = ans * x % mod;       x = x * x % mod;     }     return ans;   }   signed main() {     n = read();     f[31][0][1] = 1;     for (long long i = 30; i >= 0; i--) {       if (!((n >> i) & 1)) {         for (long long j = 0; j <= 30 - i; j++) {           f[i][j + 1][0] = (f[i][j + 1][0] + f[i + 1][j][0]) % mod;           f[i][j][0] = (f[i][j][0] + f[i + 1][j][0] * power(2, j) % mod) % mod;           f[i][j][1] =               (f[i][j][1] + f[i + 1][j][1] * power(2, j - 1) % mod) % mod;         }       } else {         for (long long j = 0; j <= 30 - i; j++) {           f[i][j + 1][0] = (f[i][j + 1][0] + f[i + 1][j][0]) % mod;           f[i][j][0] = (f[i][j][0] + f[i + 1][j][0] * power(2, j) % mod) % mod;           f[i][j + 1][1] = (f[i][j + 1][1] + f[i + 1][j][1]) % mod;           f[i][j][0] =               (f[i][j][0] + f[i + 1][j][1] * power(2, j - 1) % mod) % mod;           if (j)             f[i][j][1] =                 (f[i][j][1] + f[i + 1][j][1] * power(2, j - 1) % mod) % mod;         }       }     }     long long ans = 1;     for (long long i = 1; i <= 30; i++)       ans = (ans + f[0][i][0] + f[0][i][1]) % mod;     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, m, lim, p = 1, a[N], nex[N * 2], spot[N * 2], head[N], fa[N], f[N], g[N];   int maxcolor[N][2], c[N];   bool color[N], v[N];   void add(int x, int y) {     nex[++p] = head[x], head[x] = p, spot[p] = y;     nex[++p] = head[y], head[y] = p, spot[p] = x;   }   void dfs1(int x) {     int tp, y, cnt = 0, m1 = 0, m2 = 0;     v[x] = 1;     for (tp = head[x]; y = spot[tp], tp; tp = nex[tp])       if (a[y] >= lim && y != fa[x]) {         fa[y] = x;         dfs1(y);         if (!c[y])           cnt += f[y];         else {           c[x]++;           if (f[y] > m2) m2 = f[y];           if (m1 < m2) swap(m1, m2);         }       }     maxcolor[x][0] = m1;     maxcolor[x][1] = m2;     f[x] = cnt + m1 + 1;     c[x] += color[x];   }   void dfs2(int x, int &ret) {     if (fa[x]) {       if (c[fa[x]] - (c[x] != 0) > 0) {         if (c[x]) {           if (f[x] < maxcolor[fa[x]][0]) {             g[x] = f[x] - maxcolor[x][0] + max(maxcolor[x][0], g[fa[x]]);             if (g[fa[x]] > maxcolor[x][1]) maxcolor[x][1] = g[fa[x]];             if (maxcolor[x][0] < maxcolor[x][1])               swap(maxcolor[x][0], maxcolor[x][1]);           } else {             g[x] = f[x] - maxcolor[x][0] +                    max(maxcolor[x][0],                        g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1]);             if (g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1] >                 maxcolor[x][1])               maxcolor[x][1] = g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1];             if (maxcolor[x][0] < maxcolor[x][1])               swap(maxcolor[x][0], maxcolor[x][1]);           }         } else           g[x] = f[x] - maxcolor[x][0] + max(maxcolor[x][0], g[fa[x]] - f[x]);       } else {         if (c[x]) {           if (f[x] < maxcolor[fa[x]][0])             g[x] = f[x] + g[fa[x]];           else             g[x] = f[x] + g[fa[x]] - maxcolor[fa[x]][0] + maxcolor[fa[x]][1];         } else           g[x] = g[fa[x]];       }       c[x] += ((c[fa[x]] - (c[x] != 0)) != 0);     } else       g[x] = f[x];     ret = max(ret, g[x]);     for (int tp = head[x]; tp; tp = nex[tp])       if (a[spot[tp]] >= lim && spot[tp] != fa[x]) dfs2(spot[tp], ret);   }   int solve() {     memset(v, 0, sizeof(v));     memset(fa, 0, sizeof(fa));     memset(c, 0, sizeof(c));     int maxn = 0;     for (int i = 1; i <= n; i++)       if (a[i] >= lim && v[i] == 0) {         dfs1(i), dfs2(i, maxn);       }     return maxn >= m;   }   int main() {     int i, x, y, l, r, tp;     cin >> n >> m;     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     for (i = 1; i < n; i++) {       scanf( %d%d , &x, &y);       add(x, y);     }     for (l = 1, r = 1000000; l < r;) {       lim = l + r + 1 >> 1;       memset(color, 0, sizeof(color));       for (i = 1; i <= n; i++)         if (a[i] < lim)           for (tp = head[i]; tp; tp = nex[tp]) color[spot[tp]] = 1;       if (solve())         l = lim;       else         r = lim - 1;     }     cout << l;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, b[101][101];   bool isHorrizontal() {     int i, j;     for (i = 0; i < n / 3; i++) {       for (j = 0; j < m; j++) {         if (b[i][j] != 1) return false;       }     }     for (i = n / 3; i < (n - n / 3) / 2 + (n / 3); i++) {       for (j = 0; j < m; j++) {         if (b[i][j] != 2) return false;       }     }     for (i = (n - n / 3) / 2 + (n / 3); i < n; i++) {       for (j = 0; j < m; j++) {         if (b[i][j] != 3) return false;       }     }     return true;   }   bool isVertical() {     int i, j;     for (i = 0; i < n; i++) {       for (j = 0; j < m / 3; j++) {         if (b[i][j] != 1) return false;       }     }     for (i = 0; i < n; i++) {       for (j = m / 3; j < (m - m / 3) / 2 + (m / 3); j++) {         if (b[i][j] != 2) return false;       }     }     for (i = 0; i < n; i++) {       for (j = (m - m / 3) / 2 + (m / 3); j < m; j++) {         if (b[i][j] != 3) return false;       }     }     return true;   }   int main() {     int i, j, c = 0, tmp = 0;     scanf( %d %d , &n, &m);     char a;     map<char, int> mp;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         scanf(  %c , &a);         if (!mp.count(a)) {           tmp++;           mp[a] = tmp;         }         b[i][j] = mp[a];         if (b[i][j] == 3) c = 1;       }     }     if (c == 1) {       if (n % 3 == 0 || m % 3 == 0) {         if (n % 3 == 0 && isHorrizontal())           puts( YES );         else if (m % 3 == 0 && isVertical())           puts( YES );         else           puts( NO );       } else         puts( NO );     } else       puts( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   int main() {     ios_base::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       ll a, b, c;       cin >> a >> b >> c;       ll x, y;       if (a == c and b == 1) {         cout <<  -1 -1  <<   n ;         continue;       }       if (c <= a)         x = -1;       else         x = 1;       if (a * b <= c)         y = -1;       else         y = b;       cout << x <<     << y <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int grid[101][101];   int canDoRowOperation(int r, int c) {     int m = 1000;     for (int j = 0; j < c; j++) m = min(m, grid[r][j]);     return m;   }   int canDoClmOperation(int r, int c) {     int m = 1000;     for (int i = 0; i < r; i++) m = min(m, grid[i][c]);     return m;   }   int main() {     int r, c;     scanf( %d %d , &r, &c);     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         scanf( %d , &grid[i][j]);       }     }     vector<string> r_c;     vector<int> vec_num;     if (r <= c) {       for (int i = 0; i < r; i++) {         int m = canDoRowOperation(i, c);         if (m) {           for (int j = 0; j < c; j++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( row );           vec_num.push_back(i + 1);         }       }       for (int j = 0; j < c; j++) {         int m = canDoClmOperation(r, j);         if (m) {           for (int i = 0; i < r; i++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( col );           vec_num.push_back(j + 1);         }       }     } else {       for (int j = 0; j < c; j++) {         int m = canDoClmOperation(r, j);         if (m) {           for (int i = 0; i < r; i++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( col );           vec_num.push_back(j + 1);         }       }       for (int i = 0; i < r; i++) {         int m = canDoRowOperation(i, c);         if (m) {           for (int j = 0; j < c; j++) grid[i][j] -= m;         }         for (int k = 0; k < m; k++) {           r_c.push_back( row );           vec_num.push_back(i + 1);         }       }     }     bool isPossible = true;     for (int i = 0; i < r; i++)       for (int j = 0; j < c; j++)         if (grid[i][j]) isPossible = false;     if (isPossible == false) {       cout << -1 << endl;     } else {       int sz = r_c.size();       cout << sz << endl;       for (int i = 0; i < sz; i++) cout << r_c[i] <<     << vec_num[i] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 100010;   inline long long read() {     long long p = 0;     long long f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       p = p * 10 + ch -  0 ;       ch = getchar();     }     return p * f;   }   long long a[N];   long long R[N << 1], L[N << 1];   int main() {     long long n = read();     long long m = read();     if (n == 1) {       printf( 0 n );       return 0;     }     for (long long i = 1; i <= m; i++) a[i] = read();     long long ans = 0;     long long l = 1;     long long r = n;     for (long long i = 1; i <= n; i++) R[i] = i;     for (long long i = 1; i <= m; i++) {       long long x = a[i];       l++;       r++;       R[r] = R[r - 1];       R[l + x - 1] = R[l + x - 2];     }     l++;     r++;     R[r] = R[r - 1];     for (long long i = l; i <= r; i++) ans += R[i] + 1;     l = m + 2;     r = n + m + 1;     for (long long i = 1; i <= n; i++) L[i + m + 1] = i;     for (long long i = 1; i <= m; i++) {       long long x = a[i];       l--;       r--;       L[l] = L[l + 1];       L[l + x - 1] = L[l + x];     }     l--;     r--;     L[l] = L[l + 1];     for (long long i = l; i <= r; i++) ans -= L[i];     return printf( %lld n , ans), 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, s;     scanf( %d%d , &n, &s);     int a, max, sum = 0;     scanf( %d , &a);     max = a;     sum += a;     for (int i = 1; i < n; i++) {       scanf( %d , &a);       if (a > max) max = a;       sum += a;     }     sum -= max;     if (sum <= s)       printf( YES );     else       printf( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int pos[100005];   pair<int, long long> ai[4 * 100005];   void update_ai(int node, int l, int r, int pos, pair<int, long long> val) {     if (l == r) {       ai[node] = val;       return;     }     int mid = (l + r) >> 1;     if (pos <= mid)       update_ai(2 * node, l, mid, pos, val);     else       update_ai(2 * node + 1, mid + 1, r, pos, val);     ai[node].first = ai[2 * node].first + ai[2 * node + 1].first;     ai[node].second = ai[2 * node].second + ai[2 * node + 1].second;   }   void query_ai(int node, int l, int r, int lq, int rq,                 pair<int, long long> &ans) {     if (lq <= l && r <= rq) {       ans.first += ai[node].first;       ans.second += ai[node].second;       return;     }     int mid = (l + r) >> 1;     if (lq <= mid) query_ai(2 * node, l, mid, lq, rq, ans);     if (mid + 1 <= rq) query_ai(2 * node + 1, mid + 1, r, lq, rq, ans);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       int x;       scanf( %d , &x);       pos[x] = i;     }     double sol = 0;     for (int i = 1; i <= n; ++i) {       pair<int, long long> x = make_pair(0, 0);       query_ai(1, 1, n, 1, pos[i], x);       sol += ((double)(n - pos[i] + 1)) / n * (((double)x.second) / (n + 1));       x = make_pair(0, 0);       query_ai(1, 1, n, pos[i], n, x);       sol += x.first -              ((double)pos[i]) / n * (x.first - ((double)x.second) / (n + 1));       update_ai(1, 1, n, pos[i], make_pair(1, pos[i]));     }     printf( %.20f n , sol);     return 0;   }