malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m, a[100010];   map<string, int> mp;   int tot, cnt[100010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     char f[110];     string s;     for (int i = 1; i <= m; i++) {       scanf( %s , f);       s = f;       if (!mp[s]) mp[s] = ++tot;       ++cnt[mp[s]];     }     sort(cnt + 1, cnt + tot + 1);     int minans = 0, maxans = 0;     for (int i = tot, j = 1; i >= 1; i--, j++) {       minans += cnt[i] * a[j];       maxans += cnt[i] * a[n - j + 1];     }     printf( %d %d n , minans, maxans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   const int C = 26;   void add(int &x, int y) {     x += y;     while (x >= MOD) x -= MOD;     while (x < 0) x += MOD;   }   int fix(int x) {     while (x >= MOD) x -= MOD;     while (x < 0) x += MOD;     return x;   }   int pw(int a, int b) {     int ret = 1;     while (b) {       if (b & 1) ret = 1ll * ret * a % MOD;       b >>= 1;       a = 1ll * a * a % MOD;     }     return ret;   }   int inv(int a) { return pw(a, MOD - 2); }   int mul(int a, int b) { return (long long)a * b % MOD; }   const int MAXN = 2e5 + 10;   const int LOG = 20;   int n, q, p[MAXN];   int tab[MAXN][LOG];   int get(int l, int r) {     int ans = 1;     for (int i = LOG - 1; ~i; --i)       if (l + (1 << i) <= r) ans = mul(ans, tab[l][i]), l += 1 << i;     return ans;   }   int st[MAXN << 2], lz[MAXN << 2];   int pr[MAXN];   void plant(int v, int b, int e) {     if (e - b == 1) {       st[v] = pr[b];       return;     }     int m = (b + e) >> 1;     plant(v << 1, b, m);     plant(v << 1 ^ 1, m, e);     st[v] = fix(st[v << 1] + st[v << 1 ^ 1]);   }   void weed(int v) {     for (int u : {v << 1, v << 1 ^ 1}) {       st[u] = mul(st[u], lz[v]);       lz[u] = mul(lz[u], lz[v]);     }     lz[v] = 1;   }   void water(int v, int b, int e, int l, int r, int x) {     if (l >= r) return;     if (b == l && e == r) {       st[v] = mul(st[v], x);       lz[v] = mul(lz[v], x);       return;     }     weed(v);     int m = (b + e) >> 1;     water(v << 1, b, m, l, min(r, m), x);     water(v << 1 ^ 1, m, e, max(l, m), r, x);     st[v] = fix(st[v << 1] + st[v << 1 ^ 1]);   }   int smoke(int v, int b, int e, int l, int r) {     if (l >= r) return 0;     if (b == l && e == r) return st[v];     weed(v);     int m = (b + e) >> 1;     return fix(smoke(v << 1, b, m, l, min(r, m)) +                smoke(v << 1 ^ 1, m, e, max(l, m), r));   }   void solve() {     cin >> n >> q;     for (int i = 0; i < n; i++) cin >> p[i];     for (int i = 0; i < n; i++) p[i] = mul(p[i], inv(100));     for (int i = 0; i < n; i++) tab[i][0] = p[i];     for (int j = 1; j < LOG; j++)       for (int i = 0; i < n; i++)         if (i + (1 << j) <= n)           tab[i][j] = mul(tab[i][j - 1], tab[i + (1 << (j - 1))][j - 1]);         else           tab[i][j] = tab[i][j - 1];     fill(lz, lz + (MAXN << 2), 1);     pr[n - 1] = inv(p[n - 1]);     for (int i = n - 2; ~i; --i) pr[i] = mul(pr[i + 1], inv(p[i]));     plant(1, 0, n);     set<int> pts;     pts.insert(0);     pts.insert(n);     while (q--) {       int u;       cin >> u;       --u;       if (pts.count(u)) {         auto prv = --pts.lower_bound(u);         water(1, 0, n, *prv, u, smoke(1, 0, n, u, u + 1));         pts.erase(u);       } else {         auto prv = --pts.lower_bound(u);         water(1, 0, n, *prv, u, inv(smoke(1, 0, n, u, u + 1)));         pts.insert(u);       }       cout << st[1] <<   n ;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int te = 1;     for (int w = 1; w <= te; w++) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   void solve() {     int n, m, k, x, y;     cin >> n >> m >> k;     for (int i = 0; i < 2 * k; i++) cin >> x >> y;     string ans =   ;     ans += (string(n - 1,  U ) + string(m - 1,  L ));     for (int i = 0; i < n; i++) {       if (i & 1)         ans += string(m - 1,  L );       else         ans += string(m - 1,  R );       if (i != n - 1) ans +=  D ;     }     cout << ans.size() <<   n ;     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   int w[505];   int book[505];   int v[1005];   int used[505];   int main(void) {     int n, m;     int k;     int r = 0;     scanf(  %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       used[i] = 0;       scanf(  %d , &w[i]);     }     for (int i = 1; i <= m; i++) {       scanf(  %d , &v[i]);     }     k = 1;     for (int i = 1; i <= m; i++) {       if (!used[v[i]]) {         used[v[i]] = 1;         book[k++] = v[i];       }     }     for (int i = 1; i <= m; i++) {       for (int j = 1; j < k; j++) {         if (book[j] == v[i]) {           for (int p = j - 1; p >= 1; p--) {             r += w[book[p]];             book[p + 1] = book[p];           }           book[1] = v[i];           break;         }       }     }     printf( %d n , r);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char b[1000050], a[1000050];   int n, m, p, P;   bool c[2050], ans[2050], Ans;   bool Calc(int x, int y, int z, int o, int u) {     for (; o >= z; o--) {       bool Flag = false;       while (y >= x) {         bool flag = true;         for (int j = y, k = o; j <= n && k <= m; j += p, k += u)           if (a[j] != b[k] || ((j + p > n) ^ (k + u > m))) {             flag = false;             break;           }         if (flag) {           c[y] = 1;           y--;           Flag = true;           break;         }         y--;       }       if (!Flag) return false;     }     return true;   }   void Put() {     for (int i = 1; i <= P; i++) putchar(ans[i] ?  1  :  0 );     puts(  );   }   void PUUUUUT() {     if (Ans)       for (int i = 1; i <= p; i++)         if (ans[i] && !c[i])           break;         else if (!ans[i] && c[i])           return;     for (int i = 1; i <= p; i++) ans[i] = c[i];     Ans = true;   }   void Solve() {     int k = n / p, l = n % p;     for (int i = 0; i <= l; i++)       if ((m - i) % k == 0 && (m - i) / k - i <= p - l) {         if (Calc(1, l, 1, i, (m - i) / k) &&             Calc(l + 1, p, i + 1, (m - i) / k, (m - i) / k))           PUUUUUT();         memset(c, 0, sizeof(c));       }     return;   }   int main() {     gets(a + 1);     gets(b + 1);     scanf( %d , &P);     n = strlen(a + 1);     m = strlen(b + 1);     p = min(P, n);     Solve();     if (!Ans)       puts( 0 );     else       Put();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     int n, k;     cin >> n >> k;     for (int i = 1; i <= k; ++i) cout << 2 * i <<     << 2 * i - 1 <<    ;     for (int i = 2 * k + 1; i <= 2 * n; ++i) cout << i <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const int INF = 0x3f3f3f3f;   inline void cmin(int &x, int y) {     if (y < x) x = y;   }   inline void cmax(int &x, int y) {     if (y > x) x = y;   }   bool isvowel(char a) {     if (a ==  a  || a ==  e  || a ==  i  || a ==  o  || a ==  u ) return true;     return false;   }   int main() {     string s, t;     cin >> s >> t;     if (s.size() != t.size()) return !printf( No );     for (int i = 0; i < s.size(); i++) {       if (isvowel(s[i]) == isvowel(t[i]))         continue;       else         return !printf( No );     }     printf( Yes );     return 0;   }
#include <bits/stdc++.h>   using namespace ::std;   const long long maxn = 1e5 + 500;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 500;   const long long rad = 330;   long long a[maxn];   long long t[maxn];   bool cmp(pair<long long, long long> a, pair<long long, long long> b) {     if (a.first / rad == b.first / rad) {       return (a.second < b.second);     }     return (a.first < b.first);   }   long long ml[maxn];   pair<long long, long long> sque[maxn];   map<pair<long long, long long>, long long> anss;   long long lastid[maxn];   long long nextid[maxn];   long long id[maxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, k;     cin >> n >> k;     for (long long i = 1; i <= n; i++) {       cin >> t[i];     }     for (long long i = 1; i <= n; i++) {       cin >> a[i];       if (t[i] == 2) {         a[i] *= -1;       }     }     for (long long i = 1; i <= n; i++) {       a[i] += a[i - 1];     }     vector<long long> vec;     for (long long i = 0; i <= n; i++) {       vec.push_back(a[i]);     }     {       sort(vec.begin(), vec.end());       auto it = unique(vec.begin(), vec.end());       vec.resize(distance(vec.begin(), it));     }     vector<long long>::iterator it;     for (long long i = 0; i <= n; i++) {       id[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();       it = lower_bound(vec.begin(), vec.end(), a[i] + k);       if (it != vec.end() && (*it) == a[i] + k) {         nextid[i] = it - vec.begin();       } else {         nextid[i] = maxn - 1;       }       it = lower_bound(vec.begin(), vec.end(), a[i] - k);       if (it != vec.end() && (*it) == a[i] - k) {         lastid[i] = it - vec.begin();       } else {         lastid[i] = maxn - 1;       }     }     long long q;     cin >> q;     vector<pair<long long, long long> > que;     for (long long i = 0; i < q; i++) {       long long l, r;       cin >> l >> r;       l--;       sque[i] = make_pair(l, r);       que.push_back(make_pair(l, r));     }     sort(que.begin(), que.end(), cmp);     long long res = 0;     long long l = 0;     long long r = 0;     for (auto b : que) {       long long L = b.first;       long long R = b.second;       while (r <= R) {         res += ml[lastid[r]];         ml[id[r]]++;         r++;       }       while (L < l) {         l--;         res += ml[nextid[l]];         ml[id[l]]++;       }       while (R + 1 < r) {         r--;         ml[id[r]]--;         res -= ml[lastid[r]];       }       while (l < L) {         ml[id[l]]--;         res -= ml[nextid[l]];         l++;       }       anss[make_pair(L, R)] = res;     }     for (long long i = 0; i < q; i++) {       cout << anss[sque[i]] << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int N;   int Move[100005], cnt, P[100005];   vector<int> G[100005];   int TT[100005], Next[100005];   int DP[100005], Stack[100005], top;   void Read() {     scanf( %d , &N);     for (int i = 1; i < N; i++) {       int father;       scanf( %d , &father);       ++father;       G[father].push_back(i + 1);       TT[i + 1] = father;     }   }   void DFS(int node) {     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       DFS(neighb);       DP[node] = max(DP[node], DP[neighb] + 1);     }   }   void precalcPath() {     int node = 1;     while (G[node].size() != 0) {       Stack[++top] = node;       for (int i = 0; i < G[node].size(); i++) {         int neighb = G[node][i];         if (DP[neighb] == DP[node] - 1) {           swap(G[node][i], G[node][G[node].size() - 1]);           G[node].pop_back();           node = neighb;           break;         }       }     }     Stack[++top] = node;   }   void Solve(int node, int next) {     int last = next;     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       TT[last] = neighb;       Move[++cnt] = last;       last = neighb;     }     for (int i = G[node].size() - 1; i >= 1; i--)       Solve(G[node][i], G[node][i - 1]);     if (G[node].size() > 0) Solve(G[node][0], next);   }   void printSol() {     int node = 1;     for (int i = 1; i <= N; i++) Next[TT[i]] = i;     for (int i = 1; i <= N; i++) {       printf( %d  , node - 1);       node = Next[node];     }     printf(  n%d n , cnt);     for (int i = cnt; i >= 1; i--) printf( %d  , Move[i] - 1);     printf(  n );   }   int main() {     Read();     DFS(1);     precalcPath();     for (int i = 1; i < top; i++) Solve(Stack[i], Stack[i + 1]);     printSol();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   const int mod = 1e9 + 7;   struct point {     int x, y;     inline friend long long operator*(point a, point b) {       return 1ll * a.x * b.y - 1ll * a.y * b.x;     }   } p[N];   long long sx[N], sy[N], s[N], ans, sum, area;   int n;   inline long long getS(int l, int r) { return s[r] - s[l] + (p[r] * p[l]); }   inline int chk(int l, int r) {     long long L = getS(l, r), R = getS(r, l + n);     if (L < R) return 1;     if (L == R && r <= n) return 1;     return 0;   }   inline long long cro(int k, int l, int r) {     long long xy = (sx[r] - sx[l - 1]) * p[k].y;     long long yx = (sy[r] - sy[l - 1]) * p[k].x;     return (xy % mod - yx % mod + mod) % mod;   }   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n); i++) scanf( %d%d , &p[i].x, &p[i].y);     for (int i = (1); i <= (n / 2); i++) swap(p[i], p[n + 1 - i]);     for (int i = (1); i <= (n); i++) p[i + n] = p[i];     p[0] = p[n];     for (int i = (0); i < (n); i++) area = (area + (p[i] * p[i + 1])) % mod;     sx[0] = p[0].x, sy[0] = p[0].y;     for (int i = (1); i <= (2 * n); i++) {       sx[i] = (sx[i - 1] + p[i].x) % mod;       sy[i] = (sy[i - 1] + p[i].y) % mod;       s[i] = s[i - 1] + (p[i - 1] * p[i]);     }     int r = 0;     for (int l = (1); l <= (n); l++) {       if (r <= l + 1) sum = 0, r = l + 1;       while (chk(l, r + 1)) sum = (sum + s[++r] - s[l] + mod) % mod;       ans = (ans + sum + cro(l, l + 2, r)) % mod;       sum = (sum - (p[l] * p[l + 1]) % mod * (r - l - 1) % mod -              (p[l + 1] * p[l + 2]) % mod + 2ll * mod) %             mod;     }     ans = (1ll * n * (n - 3) / 2) % mod * area - 2 * ans;     ans = (ans % mod + mod) % mod;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int r, p, s;       cin >> r >> p >> s;       string q;       cin >> q;       int lastn = ceil(n / 2.0);       int R = 0, P = 0, S = 0;       for (long long i = 0; i < q.size(); i++) {         if (q[i] ==  R ) {           R++;         } else if (q[i] ==  S ) {           S++;         } else {           P++;         }       }       int total = min(R, p) + min(P, s) + min(S, r);       if (total < lastn) {         cout <<  NO  << endl;         ;         continue;       } else {         char ans[q.size()];         memset(ans,  0 , sizeof(ans));         for (int i = 0; i < q.size(); i++) {           if (q[i] ==  S ) {             if (r > 0) {               ans[i] =  R ;               r--;             }           } else if (q[i] ==  R ) {             if (p > 0) {               ans[i] =  P ;               p--;             }           } else {             if (s > 0) {               ans[i] =  S ;               s--;             }           }         }         for (int i = 0; i < q.size(); i++) {           if (ans[i] ==  0 ) {             if (r > 0) {               ans[i] =  R ;               r--;             } else if (p > 0) {               ans[i] =  P ;               p--;             } else if (s > 0) {               ans[i] =  S ;               s--;             }           }         }         cout <<  YES  << endl;         ;         for (int i = 0; i < q.size(); i++) {           cout << ans[i];         }         cout << endl;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();   }
#include <bits/stdc++.h>   int main() {     char s[20];     bool jizz = false;     int value1[61] = {0}, value2[61] = {0}, k, max = 0;     int m, n, a, b;     scanf( %s , &s);     for (m = 0; m < strlen(s); m++) {       if (s[m] ==  : ) break;       for (a = 2; a < 61; a++) {         if (s[m] <=  Z  && s[m] >=  A )           k = s[m] -  A  + 10;         else           k = s[m] -  0 ;         if (k > max) max = k;         value1[a] = value1[a] * a + k;       }     }     for (n = m + 1; n < strlen(s); n++) {       for (a = 2; a < 61; a++) {         if (s[n] <=  Z  && s[n] >=  A )           k = s[n] -  A  + 10;         else           k = s[n] -  0 ;         if (k > max) max = k;         value2[a] = value2[a] * a + k;       }     }     if (value1[60] <= 23 && value2[60] <= 59)       printf( -1 n );     else {       for (a = max + 1; a < 60; a++) {         if (value1[a] <= 23 && value2[a] <= 59) {           jizz = true;           break;         }       }       if (jizz == false) printf( 0 n );       for (a = max + 1; a < 60; a++) {         if (value1[a] <= 23 && value2[a] <= 59) {           printf( %d  , a);         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   char s1[6], s2[6], ch;   int cnt = 0, ans, ok1, ok2;   bool func1(int sumA, int sumB, char s[]) {     int dif = sumB - sumA;     for (int i = 0; i < 3; i++) {       bool fl = 0;       ch = s[i];       while (ch <  9  && dif > 0) ch++, dif--, fl = 1;       if (fl) cnt++;     }     return dif == 0;   }   bool func2(int sumA, int sumB, char s[]) {     int dif = sumB - sumA;     for (int i = 2; i >= 0; i--) {       bool fl = 0;       ch = s[i];       while (ch >  0  && dif > 0) ch--, dif--, fl = 1;       if (fl) cnt++;     }     return dif == 0;   }   int main() {     while (scanf( %3s%3s , s1, s2) == 2) {       cerr << s1 <<     << s2 <<   n ;       int sum = 0, sum1 = 0, sum2 = 0, ans = 9;       for (int i = 0; i < 3; i++) sum1 += s1[i] -  0 ;       for (int i = 0; i < 3; i++) sum2 += s2[i] -  0 ;       sort(s1, s1 + 3), sort(s2, s2 + 3);       sum = max(sum1, sum2);       while (sum) {         cnt = ok1 = ok2 = 0;         if (sum1 <= sum)           ok1 = func1(sum1, sum, s1);         else           ok1 = func2(sum, sum1, s1);         if (sum2 <= sum)           ok2 = func1(sum2, sum, s2);         else           ok2 = func2(sum, sum2, s2);         if (ok1 && ok2) {           cerr <<  cnt=   << cnt <<     << sum <<   n ;           ans = min(ans, cnt);         }         sum--;       }       if (ans == 9) ans = 0;       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   const int MAX_LEN = 1010;   using namespace std;   template <typename U, typename V>   string to_string(pair<U, V>);   string to_string(const string& e_) { return     + e_ +    ; }   string to_string(char e_) { return      + string(1, e_) +     ; }   string to_string(bool e_) { return e_ ?  true  :  false ; }   template <typename T>   string to_string(T e_) {     string s_ =  [  ;     for (const auto& x_ : e_) s_ += to_string(x_) +    ;     return s_ +  ] ;   }   template <typename U, typename V>   string to_string(pair<U, V> e_) {     return  (  + to_string(e_.first) +  ,   + to_string(e_.second) +  ) ;   }   void dbg_str() { ; }   template <typename U, typename... V>   void dbg_str(U u, V... v) {     ;     dbg_str(v...);   }   long long int n, m;   long long int ans, mn;   long long int lx, hx, wx;   struct rd {     long long int l;     long long int w;     long long int h;   };   vector<rd> rooms;   vector<rd> rolls;   int main() {     scanf( %lld , &n);     for (int i = 0; i < n; i++) {       scanf( %lld %lld %lld , &lx, &wx, &hx);       rooms.push_back({lx, wx, hx});     }     scanf( %lld , &m);     for (int i = 0; i < m; i++) {       scanf( %lld %lld %lld , &lx, &wx, &hx);       rolls.push_back({lx, wx, hx});     }     ans = 0;     for (int i = 0; i < n; i++) {       mn = 1e14;       for (int j = 0; j < m; j++) {         long long int totw = (rolls[j].l / rooms[i].h) * rolls[j].w;         if (!totw) continue;         long long int nr =             (long long int)ceil(((double)2 * (rooms[i].l + rooms[i].w)) / totw);         mn = min(mn, nr * rolls[j].h);       }       ans += mn;     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   long long int M = 998244353, q, n, p[300001], d[300001], fans, products[300001],                 invproducts[300001];   long long int power(long long int x, long long int n) {     if (n == 0)       return (long long)1;     else if (n % 2 == 0)       return power((x * x) % M, n / 2);     else       return (x * power((x * x) % M, (n - 1) / 2)) % M;   }   long long int cans(long long int p, long long int q) {     return ((p * power(q, M - 2)) % M + M) % M;   }   void build() {     products[0] = invproducts[0] = 1;     for (long long int i = 1; i <= n; i++) {       products[i] = (products[i - 1] * p[i]) % M;       invproducts[i] = cans(1, products[i]);     }     return;   }   long long int querry(long long int l, long long int r) {     return (products[r] * invproducts[l - 1]) % M;   }   long long int qvalue(long long int i, long long int j) {     long long int prod = querry(i, j);     long long int ans = (d[i - 1] * prod) % M;     ans = ((d[j] - ans) % M + M) % M;     return ans;   }   set<long long int> checkpoints;   void addcheckpoint(long long int id) {     checkpoints.insert(id);     auto it = checkpoints.find(id);     auto it1 = prev(it);     auto it2 = next(it);     fans = ((fans - qvalue(*it1, *it2 - 1)) % M + M) % M;     fans = (fans + qvalue(*it1, id - 1)) % M;     fans = (fans + qvalue(id, *it2 - 1)) % M;   }   void delcheckpoint(long long int id) {     auto it = checkpoints.find(id);     auto it1 = prev(it);     auto it2 = next(it);     fans = ((fans + qvalue(*it1, *it2 - 1)) % M + M) % M;     fans = ((fans - qvalue(*it1, id - 1)) % M + M) % M;     fans = ((fans - qvalue(id, *it2 - 1)) % M + M) % M;     checkpoints.erase(id);   }   int main() {     cin >> n >> q;     for (int i = 1; i <= n; i++) {       long long int pr;       cin >> pr;       p[i] = cans(100, pr);       d[i] = ((1 + d[i - 1]) * p[i]) % M;     }     build();     checkpoints.insert(1);     checkpoints.insert(n + 1);     fans = d[n];     while (q--) {       long long int quer;       cin >> quer;       if (checkpoints.find(quer) == checkpoints.end())         addcheckpoint(quer);       else         delcheckpoint(quer);       cout << fans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, one, two[2], tmp, num, a[N];   bool flag, f[N];   int read() {     int 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;   }   int main() {     n = read();     for (int i = 1; i <= n; i++) a[i] = read();     for (int i = 1; i <= n; i++) {       if (f[i]) continue;       f[i] = true;       if (a[i] == i) {         one = i;         continue;       }       if (a[a[i]] == i) {         two[0] = i;         two[1] = a[i];         f[a[i]] = true;         continue;       }       tmp = a[i];       num = 1;       while (!f[tmp]) {         f[tmp] = true;         num++;         tmp = a[tmp];       }       if (num & 1) flag = true;     }     if (one) {       printf( YES n );       for (int i = 1; i <= n; i++)         if (i != one) printf( %d %d n , one, i);       return 0;     }     if (flag || !two[0]) {       printf( NO );       return 0;     }     printf( YES n%d %d n , two[0], two[1]);     memset(f, 0, sizeof(f));     for (int i = 1; i <= n; i++) {       if (f[i] || i == two[0] || i == two[1]) continue;       tmp = i;       num = 0;       while (!f[tmp]) {         printf( %d %d n , two[num & 1], a[tmp]);         num++;         f[tmp] = true;         tmp = a[tmp];       }     }     return 0;   }