我是靠谱客的博主 幸福小鸽子,最近开发中收集的这篇文章主要介绍HDU 6318 2018多校第二场1010 Swaps and InversionsSwaps and Inversions,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Swaps and Inversions

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0

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

3 233 666
1 2 3
3 1 666
3 2 1

Sample Output

0
3

就是求把给出的序列变成升序的最小代价,只能交换相邻两个元素,说白了就是求逆序数*每步花费代价。

用线段树和归并排序都可以写,值得注意的是可能会有重复元素。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e5+5;

int tree[maxn<<2],number[maxn],all[maxn];
void pushup(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void update(int id,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt]++;
        return;
    }
    int m=(l+r)>>1;
    if(id<=m) update(id,l,m,rt<<1);
    else update(id,m+1,r,rt<<1|1);
    pushup(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r) return tree[rt];
    int m=(l+r)>>1;
    long long ans=0;
    if(L<=m) ans+=query(L,R,l,m,rt<<1);
    if(R> m) ans+=query(L,R,m+1,r,rt<<1|1);
    return ans;
}
int main()
{
#ifdef DEBUG
    freopen("in.txt","r",stdin);
#endif // DEBUG
    int n,x,y;
    while(~scanf("%d%d%d",&n,&x,&y))
    {
        memset(tree,0,sizeof(tree));
        for(int i=1; i<=n; i++) scanf("%d",&number[i]),all[i]=number[i];
        sort(number+1,number+n+1);
        int m=unique(number+1,number+n+1)-number-1;

        long long ans=0;
        for(int i=1; i<=n; i++)
        {
            int id=lower_bound(number+1,number+n+1,all[i])-number;
            update(id,1,n,1);
            ans+=query(id+1,n,1,n,1);
        }
        cout<<ans*min(x,y)<<endl;
    }
    return 0;
}

最后

以上就是幸福小鸽子为你收集整理的HDU 6318 2018多校第二场1010 Swaps and InversionsSwaps and Inversions的全部内容,希望文章能够帮你解决HDU 6318 2018多校第二场1010 Swaps and InversionsSwaps and Inversions所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部