Advertisement
YEZAELP

CUBE-079: The Exam Sort

Aug 8th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6.     int n;
  7.     scanf("%d",&n);
  8.     vector <int> num{0};
  9.     set <int> keep;
  10.  
  11.     for(int i=1;i<=n;i++){
  12.         int x;
  13.         scanf("%d",&x);
  14.         num.push_back(x);
  15.     }
  16.  
  17.     int s = 1, e = n, cnt = 0;
  18.     for(int j=1;j<=n;j++){
  19.         int x;
  20.         if(j%2 == 1) {
  21.             x = s;
  22.             s++;
  23.         }
  24.         else {
  25.             x = e;
  26.             e--;
  27.         }
  28.         if(num[x] == x) {
  29.             cnt++;
  30.             continue;
  31.         }
  32.         for(int j=1;j<=n;j++){
  33.             if(num[j] == x){
  34.                 num.erase(num.begin()+j);
  35.                 num.insert(num.begin()+x,x);
  36.                 break;
  37.             }
  38.         }
  39.     }
  40.  
  41.     printf("%d",cnt);
  42.  
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement