Test instructions: give you n unordered number, through the adjacent two number exchange, the value of the exchange is two number of sum, to make itincreasing the minimum cost of order and
Unlike poj3270, POJ can be exchanged for every number, and can be done with permutation groups.
#include <cstdio> #include <cstring> #include <vector> #include <iostream> #include < algorithm>using namespace std; #define LL __int64#define N 100000+10ll 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;}
Hdoj Cow Sorting