Advertisement
libobil

Untitled

Sep 12th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. const int maxn = 1e3+10;
  5. int a[maxn], n, s;
  6.  
  7. void fast_io() {
  8.  
  9.     ios_base :: sync_with_stdio(0);
  10.     cin.tie(nullptr);
  11.     cout.tie(nullptr);
  12.  
  13. }
  14.  
  15. void read() {
  16.  
  17.     cin >> n >> s;
  18.     for (int i = 1 ; i <= n ; ++i)
  19.         cin >> a[i];
  20.    
  21. }
  22.  
  23. vector < pair < int, int > > v;
  24.  
  25. void operation(int x, int y) {
  26.  
  27.     if (x < 1 || x > n || y < 1 || y > n) return;
  28.  
  29.     v.push_back({x, y});
  30.     a[x] ^= a[y];
  31.  
  32. }
  33.  
  34. void solve() {
  35.  
  36.     int beg = 0;
  37.  
  38.     for (int log = 19 ; log >= 0 ; --log) {
  39.  
  40.         for (int i = 1 ; i <= n-beg ; ++i) {
  41.  
  42.             if (a[i] & (1 << log)) {
  43.                
  44.                 if (i < n && (!(a[i+1] & (1 << log)))) operation(i+1, i);
  45.                 if (a[i-1] & (1 << log)) operation(i-1, i);
  46.  
  47.             }
  48.  
  49.         }
  50.  
  51.         beg += ((a[n-beg] & (1 << log)) > 0);
  52.    
  53.     }
  54.  
  55.     cout << v.size() << '\n';
  56.     for (auto [x, y] : v)
  57.         cout << x << ' ' << y << '\n';
  58.  
  59. }
  60.  
  61. void solve25() {
  62.  
  63.     int last_max = 0, max_ix;
  64.  
  65.     for (int iteration = 0 ; iteration < n-1 ; ++iteration) {
  66.  
  67.         max_ix = 1;
  68.         for (int i = 2 ; i <= n-iteration ; ++i) {
  69.  
  70.             if ((a[i] ^ last_max) > (a[max_ix] ^ last_max)) max_ix = i;
  71.  
  72.         }
  73.  
  74.         for (int i = 1 ; i <= n-iteration - (iteration == 0) ; ++i) {
  75.  
  76.             operation(i, i+1);
  77.  
  78.         }
  79.  
  80.         for (int i = max_ix+1 ; i <= n-iteration ; ++i)
  81.             operation(i, i-1);
  82.        
  83.         for (int i = max_ix-2 ; i >= 1 ; --i)
  84.             operation(i, i+1);
  85.  
  86.         last_max = a[n-iteration];
  87.  
  88.     }
  89.  
  90.     operation(1, 2);
  91.  
  92.     // cout << "a: ";
  93.     // for (int i = 1 ; i <= n ; ++i)
  94.     //     cout << a[i] << ' ';
  95.     // cout << '\n';
  96.  
  97.     // return;
  98.  
  99.     cout << v.size() << '\n';
  100.     for (auto [x, y] : v)
  101.         cout << x << ' ' << y << '\n';
  102.  
  103. }
  104.  
  105. signed main () {
  106.  
  107.     fast_io();
  108.     read();
  109.     if (s == 1) solve25();
  110.     else solve();
  111.  
  112.     return 0;
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement