Advertisement
Ryuuk

sliding-inversion

Aug 6th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <map>
  7. using namespace std;
  8. typedef pair<int, int> ii;
  9.  
  10. long long nbRt[100001];
  11. int main()
  12. {
  13.     ios_base::sync_with_stdio(false);
  14.     //freopen("input.txt","r",stdin);
  15.     int n,m ;
  16.     long long count =0,cont=0;
  17.     cin>>n>>m;
  18.     long long tab[n];
  19.  
  20.     for(int i=0; i<n; i++) //O(n)
  21.         cin>>tab[i];
  22.  
  23.     for(int j=0; j<m; j++) //O(m^2)
  24.         for(int l=j+1; l<m; l++)
  25.             if(tab[j]>tab[l])
  26.             {
  27.                 nbRt[j]++;
  28.                 count++;
  29.                 cont++;
  30.             }
  31.     for(int i=1; i<=n-m; i++)//O(n*m)
  32.     {
  33.         cont-=nbRt[i-1];
  34.         count+=cont;
  35.         for(int j=i; j<i+m-1; j++)
  36.             if((tab[j]>tab[i+m-1]))
  37.             {
  38.                 count++;
  39.                 cont++;
  40.                 nbRt[j]++;
  41.             }
  42.     }
  43.     cout<<count<<endl;
  44.  
  45.  
  46.     return 0 ;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement