1. 程式人生 > >HDU 6318 Swaps and Inversions 思路很巧妙!!!(轉換為樹狀數組或者歸並求解逆序數)

HDU 6318 Swaps and Inversions 思路很巧妙!!!(轉換為樹狀數組或者歸並求解逆序數)

eve because owb else 離散化 try 自己 title esp

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 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 1i<jn 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.
1n,x,y100000, 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

Source 2018 Multi-University Training Contest 2 題目意思:
給你一個長度為n的數列
開始檢查,如果某兩個數不是從小大大順序的,有一個就罰x元,
或者也可以直接在檢查之前對不符合要求的調換位置(相鄰的)
花費y元
然後要你求最小的花費
所以就是直接求這個序列的逆序數然後乘以x和y花費中較小的一個
逆序數可以歸並或者數狀數組求解 方法1歸並:
#include<stdio.h>
#include<memory>
#include<algorithm>
#define max_v 100005
using namespace std;
typedef long long LL;
LL a[max_v];
LL temp[max_v];
LL ans;
void mer(int s,int m,int t)
{
    int i=s;
    int j=m+1;
    int k=s;
    while(i<=m&&j<=t)
    {
        if(a[i]<=a[j])
        {
            temp[k++]=a[i++];
        }else
        {
            ans+=j-k;//求逆序數
            temp[k++]=a[j++];
        }
    }
    while(i<=m)
    {
        temp[k++]=a[i++];
    }
    while(j<=t)
    {
        temp[k++]=a[j++];
    }
}
void cop(int s,int t)
{
    for(int i=s;i<=t;i++)
        a[i]=temp[i];
}
void megsort(int s,int t)
{
    if(s<t)
    {
        int m=(s+t)/2;
        megsort(s,m);
        megsort(m+1,t);
        mer(s,m,t);
        cop(s,t);
    }
}
int main()
{
    int n;
    LL c1,c2;
    while(~scanf("%d %lld %lld",&n,&c1,&c2))
    {
        if(n==0)
            break;
        ans=0;
        for(int i=0;i<n;i++)
            scanf("%lld",&a[i]);
        megsort(0,n-1);
        printf("%lld\n",ans*min(c1,c2));
    }
    return 0;
}
/*
題目意思:
給你一個長度為n的數列
開始檢查,如果某兩個數不是從小大大順序的,有一個就罰x元,
或者也可以直接在檢查之前對不符合要求的調換位置(相鄰的)
花費y元
然後要你求最小的花費
所以就是直接求這個序列的逆序數然後乘以x和y花費中較小的一個
逆序數可以歸並或者數狀數組求解
*/

方法二:

直接樹狀樹組加離散化

離散化其實就是數據範圍壓縮!!!,註意理解

#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define max_v 500005
int n;
struct node
{
    int v;
    int pos;
} p[max_v];
int c[max_v];
int re[max_v];
int maxx;
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int d)
{
    while(x<max_v)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
}
int getsum(int x)//返回1到x中小與等於x的數量
{
    int res=0;
    while(x>0)
    {
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
bool cmp(node a,node b)
{
   if(a.v!=b.v)
     return a.v<b.v;
   else
    return a.pos<b.pos;
}
int main()
{
    long long c1,c2;
    while(~scanf("%d %lld %lld",&n,&c1,&c2))
    {
        if(n==0)
            break;
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i].v);
            p[i].pos=i;
        }

        sort(p+1,p+1+n,cmp);

        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=(i-getsum(p[i].pos)-1);//先找再更新,避免getsum的時候算上自己
            update(p[i].pos,1);
        }
        printf("%lld\n",ans*min(c1,c2));
    }
    return 0;
}
[ Copy to Clipboard ]    [ Save to File]

HDU 6318 Swaps and Inversions 思路很巧妙!!!(轉換為樹狀數組或者歸並求解逆序數)