Swaps and Inversions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2787 Accepted Submission(s): 1071
Problem Description
Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.
Input
There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.
Output
For every test case, a single integer representing minimum money to pay.
Sample Input
3 233 666
1 2 3
3 1 666
3 2 1
Sample Output
0
3
题目大意:
求给定序列的逆序数。
维护一个和给定序列一样大的初始全为0的数组。从大到小依次取数列中的数,将其对应的位置置为1,并查询前面有多少个1,则逆序数加上这个数。
用线段树可以方便地查询区间1的个数。
注意从大到小取数的时候,如果两个数一样大,则先取位置靠后的。这样逆序数就不会多算了。在排序中实现这个构思。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<stack> using namespace std; const int maxn=100000; struct tnode { int num;//值 int seq;//序号 bool operator<(const tnode& y) const { if(num==y.num) return seq<y.seq; return num<y.num; } }; tnode node[maxn+10]; struct ttree { int l,r; int sum; }; ttree tree[maxn*4+10]; void pushup(int x) { if(tree[x].l==tree[x].r) return; tree[x].sum=tree[x*2].sum+tree[x*2+1].sum; } void build(int x,int l,int r) { tree[x].l=l; tree[x].r=r; tree[x].sum=0; if(l<r) { int mid=(l+r)/2; build(x*2,l,mid); build(x*2+1,mid+1,r); } } void modify(int x,int pos) { if(tree[x].l==tree[x].r) tree[x].sum=1; else { int mid=(tree[x].l+tree[x].r)/2; if(pos<=mid) modify(x*2,pos); else modify(x*2+1,pos); pushup(x); } } int query(int x,int l,int r) { if(l<=tree[x].l&&tree[x].r<=r) return tree[x].sum; int ret=0; int mid=(tree[x].l+tree[x].r)/2; if(l<=mid) ret+=query(x*2,l,r); if(r>mid) ret+=query(x*2+1,l,r); return ret; } int main() { int n,x,y; while(scanf("%d%d%d",&n,&x,&y)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",&node[i].num); node[i].seq=i; } sort(node+1,node+n+1); long long ans=0; build(1,1,n); for(int i=n;i>=1;i--) { ans+=1LL*query(1,1,node[i].seq); modify(1,node[i].seq); } printf("%lld ",ans*min(x,y)); } return 0; }