HDU 6318 2018多校第二场1010 Swaps 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值