码迷,mamicode.com
首页 > 编程语言 > 详细

HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)

时间:2018-08-14 11:34:17      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:some   scanf   training   bec   直接   numbers   arch   limit   java   

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
 
题目意思:
给你一个长度为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 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)

标签:some   scanf   training   bec   直接   numbers   arch   limit   java   

原文地址:https://www.cnblogs.com/yinbiao/p/9471010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!