题解:直接找出来没个数交换的次数就好了。。。。没个数交换的次数是这个数左边比它大的+右边比它小的,注意是LL 啊 !!!错了半天;还有重复值的处理;
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; const int INF=0x3f3f3f3f; #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) #define SD(x,y) scanf("%lf%lf",&x,&y) #define P_ printf(" ") typedef long long LL; const int MAXN=1000010; int tree[MAXN]; int b[MAXN],a[MAXN]; LL dp[MAXN]; int lowbit(int x){return x&(-x);} void insert(int x){ while(x<MAXN){ tree[x]++; x+=lowbit(x); } } int Sum(int x){ int sum=0; while(x>0){ sum+=tree[x]; x-=lowbit(x); } return sum; } int main(){ int n; SI(n); LL ans=0; dp[0]=0; for(int i=1;i<=100000;i++)dp[i]=dp[i-1]+i; mem(b,0);mem(tree,0); for(int i=0;i<n;i++){ SI(a[i]); b[i]=i-Sum(a[i]+1); insert(a[i]+1); } // for(int i=0;i<n;i++)PI(b[i]);puts(""); mem(tree,0); for(int i=n-1;i>=0;i--){ insert(a[i]+1); b[i]+=Sum(a[i]); } // for(int i=0;i<n;i++)PI(b[i]);puts(""); ans=0; for(int i=0;i<n;i++)ans+=dp[b[i]]; printf("%lld\n",ans); return 0; }
原文:http://www.cnblogs.com/handsomecui/p/5122776.html