Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4568 Accepted Submission(s): 3171
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
3 1 2 3 4 4 3 2 1
06
AC代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> int tree[1001]; int lowbit(int i) { return i&(-i); } void update(int x,int val) { for(int i=x;i<1001;i+=lowbit(i)) tree[i]+=val; } int Sum(int x) { int sum=0; for(int i=x;i>0;i-=lowbit(i)) sum+=tree[i]; return sum; } int main() { int n; while(~scanf("%d",&n)) { int num; memset(tree,0,sizeof(tree)); int ans=0; for(int i=1;i<=n;i++) { scanf("%d",&num); update(num,1); ans+=i-Sum(num); } printf("%d\n",ans); } return 0; }