Advertisement
tepyotin2

P51 Swapity Swapity Swap #1014

Nov 9th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int a[100001];
  6. bool visited[100001];
  7.  
  8. int main(){
  9.     freopen("swap.in", "r", stdin);
  10.    
  11.     int n, m, k;
  12.     cin >> n >> m >> k;
  13.    
  14.     for (int i = 1; i <= n; i++)
  15.     {
  16.         a[i] = i;
  17.     }
  18.    
  19.     for (int i = 0; i < m; i++)
  20.     {
  21.         int l, r;
  22.         cin >> l >> r;
  23.         for (int j = 0; j < (r-l+1)/2; j++)
  24.         {
  25.             int temp = a[l+j];
  26.             a[l+j] = a[r-j];
  27.             a[r-j] = temp;
  28.         }
  29.     }
  30.    
  31.     for (int i = 1; i <= n; i++)
  32.     {
  33.         if (!visited[i])
  34.         {
  35.             visited[i] = true;
  36.             int start = a[i];
  37.             vector<int> cycle;
  38.             cycle.push_back(i);
  39.             while (start != i)
  40.             {
  41.                 visited[start] = true;
  42.                 cycle.push_back(start);
  43.                 start = a[start];
  44.             }
  45.             int mod = k % cycle.size();
  46.             for (int j = 0; j < cycle.size(); j++)
  47.             {
  48.                 a[cycle[j]] = cycle[(j + mod) % cycle.size()];
  49.             }
  50.         }
  51.     }
  52.    
  53.     freopen("swap.out", "w", stdout);
  54.    
  55.     for (int i = 1; i <= n; i++)
  56.     {
  57.         cout << a[i] << endl;
  58.     }
  59.    
  60.    
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement