HDU 6318 Swaps and inversions idea is very ingenious!!! (Convert to tree array or merge to solve reverse order number)

Source: Internet
Author: User

Swaps and inversions

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2315 Accepted Submission (s): 882


Problem Descriptionlong long ago, there is an integer sequence a.
Tonyfang think this sequence are messy, so he'll count the number of inversions in this sequence. Because He is angry and you'll have a 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 swaps any of the adjacent elements.
What's the minimum amount of money are you need to spend?
The definition of inversion in this problem is pair(i,J) which1≤i<j≤n and ai>aJ.

Inputthere is multiple test cases, please read till the end of input file.
For each test, 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 is in [−9,9]. There ' re ten test cases.

Outputfor every test case, a single integer representing minimum.

Sample INPUT3 233 6661 2 33 1 6663 2 1

Sample Output03

Source 2018 Multi-university Training Contest 2topic Meaning:
give you a sequence of length n
start checking if a certain two number is not from small to large order, there is a penalty x yuan,
Alternatively, the non-conforming replacement position (adjacent) can also be directly
Spend y dollars
and ask you for the minimum cost.
so the reverse number of this sequence is directly calculated and multiplied by the smaller of the x and y costs.
Inverse number can be merged or a number array solved Method 1 Merge:
#include <stdio.h>#include<memory>#include<algorithm>#defineMax_v 100005using namespaceStd;typedefLong LongLL; LL A[max_v]; LL Temp[max_v]; LL ans;voidMerintSintMintt) {    intI=s; intj=m+1; intk=s;  while(i<=m&&j<=t) {if(a[i]<=A[j]) {Temp[k++]=a[i++]; }Else{ans+=j-k;//find the number of reverse ordertemp[k++]=a[j++]; }    }     while(i<=m) {temp[k++]=a[i++]; }     while(j<=t) {temp[k++]=a[j++]; }}voidCopintSintt) {     for(inti=s;i<=t;i++) A[i]=temp[i];}voidMegsort (intSintt) {    if(s<t) {intM= (s+t)/2;        Megsort (S,M); Megsort (M+1, T);        Mer (S,M,T);    Cop (S,T); }}intMain () {intN;    LL c1,c2;  while(~SCANF ("%d%lld%lld",&n,&c1,&C2)) {        if(n==0)             Break; Ans=0;  for(intI=0; i<n;i++) scanf ("%lld",&A[i]); Megsort (0, N-1); printf ("%lld\n", ans*min (c1,c2)); }    return 0;}/*title: Give you a series of length n to start the check, if a certain two number is not small and large order, there is a penalty x yuan, Alternatively, you can spend Y-dollars on non-compliant swap positions (adjacent) directly before the check, and then ask for the minimum cost, so you can simply ask for the inverse number of the sequence and multiply it by X and Y. The smaller number of reverse-order numbers may be merged or a number array solved*/

Method Two:

Direct Tree-Tree Group plus discretization

Discretization is actually data range compression!!! , pay attention to understanding

#include <queue>#include<Set>#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;#defineMax_v 500005intN;structnode{intv; intPOS;} P[MAX_V];intC[max_v];intRe[max_v];intMaxx;intLowbit (intx) {    returnx& (-x);}voidUpdateintXintd) {     while(x<Max_v) {C[x]+=D; X+=lowbit (x); }}intGetsum (intX//returns the number of small and equal to X 1 to x{    intres=0;  while(x>0) {res+=C[x]; X-=lowbit (x); }    returnRes;}BOOLCMP (node A,node b) {if(a.v!=B.V)returna.v<B.V; Else    returna.pos<B.pos;}intMain () {Long Longc1,c2;  while(~SCANF ("%d%lld%lld",&n,&c1,&C2)) {        if(n==0)             Break; Memset (c,0,sizeof(c));  for(intI=1; i<=n;i++) {scanf ("%d",&p[i].v); P[i].pos=i; } sort (P+1, p+1+n,cmp); Long Longans=0;  for(intI=1; i<=n;i++) {ans+ = (i-getsum (p[i].pos)-1);//Look for updates first, avoid getsum when you count on yourselfUpdate (P[i].pos,1); } printf ("%lld\n", ans*min (c1,c2)); }    return 0;}    [Copy to Clipboard] [Save to File]

HDU 6318 Swaps and inversions idea is very ingenious!!! (Convert to tree array or merge to solve reverse order number)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.