首页 > 代码库 > HDU1394(Minimum Inversion Number)

HDU1394(Minimum Inversion Number)

题目地址:Minimum Inversion Number

 

题目大意:

    求逆序对数,求循环移位后逆序数的最小值,意思一次将第一位移到最后一位,然后计算逆序对数,求出最小的那个。

 

解题思路:

           数组数组。

           线段树代码。

代码:

  

 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <string> 8 #include <bitset> 9 #include <vector>10 #include <queue>11 #include <stack>12 #include <cmath>13 #include <list>14 //#include <map>15 #include <set>16 using namespace std;17 /***************************************/18 #define ll long long19 #define int64 __int6420 #define PI 3.141592721 /***************************************/22 const int INF = 0x7f7f7f7f;23 const double eps = 1e-8;24 const double PIE=acos(-1.0);25 const int d1x[]= {0,-1,0,1};26 const int d1y[]= {-1,0,1,0};27 const int d2x[]= {0,-1,0,1};28 const int d2y[]= {1,0,-1,0};29 const int fx[]= {-1,-1,-1,0,0,1,1,1};30 const int fy[]= {-1,0,1,-1,1,-1,0,1};31 const int dirx[]= {-1,1,-2,2,-2,2,-1,1};32 const int diry[]= {-2,-2,-1,-1,1,1,2,2};33 /*vector <int>map[N];map[a].push_back(b);int len=map[v].size();*/34 /***************************************/35 void openfile()36 {37     freopen("data.in","rb",stdin);38     freopen("data.out","wb",stdout);39 }40 priority_queue<int> qi1;41 priority_queue<int, vector<int>, greater<int> >qi2;42 /**********************华丽丽的分割线,以上为模板部分*****************/43 const int M=5010;44 int c[M];45 int n;46 int lowbit(int x)47 {48     return x&(-x);49 }50 int sum(int x)51 {52     int ret=0;53     while(x>0)54     {55         ret+=c[x];56         x-=lowbit(x);57     }58     return ret;59 }60 void add(int x,int val)61 {62     while(x<=n)63     {64         c[x]+=val;65         x+=lowbit(x);66     }67 }68 int main()69 {70     int a[M];71     while(scanf("%d",&n)!=EOF)72     {73         int i,j;74         int cnt=0;75         memset(c,0,sizeof(c));76         memset(a,0,sizeof(a));77         for(i=0;i<n;i++)78         {79             scanf("%d",&a[i]);80             a[i]++;81             cnt+=sum(n)-sum(a[i]);82             add(a[i],1);83         }84         int min=cnt;85         for(int i=0;i<n;i++)86         {87             cnt+=n-a[i]-(a[i]-1);88             if(cnt<min)89                 min=cnt;90         }91         printf("%d\n",min);92     }93     return 0;94 }
View Code

 

HDU1394(Minimum Inversion Number)