题意:给你n个无序的数,通过相邻的两个数交换,交换的价值是两个数之和,求使之increasing order的最小代价和
与poj3270不同的是poj可以每个数任意交换,可以用置换群来做
#include<cstdio> #include<cstring> #include<vector> #include<iostream> #include<algorithm> using namespace std; #define ll __int64 #define N 100000+10 ll a[N],c[N],s[N],Sum[N]; int n; ll lowbit(ll x) { return x&(-x); } void updata(ll t,ll value) { for(int i=t;i<=n;i+=lowbit(i)) { c[i]+=value; s[i]+=t; } } int getSum(ll x,ll &y) { y=0; ll temp=0; for(int i=x;i>=1;i-=lowbit(i)) { temp+=c[i]; y+=s[i]; } return temp; } int main() { while(scanf("%d",&n)!=EOF) { ll sum=0; Sum[0]=0; for(ll i=1;i<=n;i++) { scanf("%I64d",&a[i]); Sum[i]=Sum[i-1]; Sum[i]+=a[i]; } for(ll i=1;i<=n;i++) { updata(a[i],1); ll y; ll temp=i-getSum(a[i],y); sum+=temp*a[i]+Sum[i]-y; } printf("%I64d\n",sum); } return 0; }
时间: 2024-11-07 14:30:25