YEZAELP

SMMR-075: Bubble

Jun 13th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using pii=pair<int,int>;
  4. vector <vector<int>> mp(2001,vector<int>(2001));
  5. int main(){
  6.     int n,t;
  7.     scanf("%d%d",&n,&t);
  8.     int ar[n+1],srt[n+1];
  9.  
  10.     for(int i=1;i<=n;i++) {
  11.         scanf("%d",&ar[i]);
  12.         mp[0][ar[i]] = i;
  13.         srt[i]=ar[i];
  14.     }
  15.     sort(srt+1,srt+n+1);
  16.     int stop;
  17.     for(int i=1;i<n;i++){
  18.         bool check=true;
  19.         for(int j=1;j<n;j++){
  20.             if(ar[j]>ar[j+1]){
  21.                 swap(ar[j],ar[j+1]);
  22.                 if(ar[j]!=srt[j]) check=false;
  23.             }
  24.             mp[i][ar[j]]=j;
  25.         }
  26.         mp[i][ar[n]]=n;
  27.  
  28.         if(check) {
  29.             stop=i;
  30.             break;
  31.         }
  32.     }
  33.     while(t--){
  34.         int k,x;
  35.         scanf("%d%d",&k,&x);
  36.         if(k>stop) printf("%d\n",mp[stop][x]);
  37.         else printf("%d\n",mp[k][x]);
  38.     }
  39.  
  40.     return 0;
  41. }
Add Comment
Please, Sign In to add comment