当前位置: 代码迷 >> 综合 >> 2018 Multi-University Training Contest 2 :Swaps and Inversions
  详细解决方案

2018 Multi-University Training Contest 2 :Swaps and Inversions

热度:5   发布时间:2023-12-01 21:52:55.0

一看题目就知道是逆序数问题,利用归并排序的方法求逆序数
再基于贪心思想,逆序数*min(x,y)就是答案

详细请看代码:

#include<cstdio>
#include<cstring>
using namespace std;typedef long long ll;
const int maxn=100000+100;int ans[maxn];
int tmp[maxn];
ll cnt;inline void Union(int l,int r,int mid){int tmp1_l=l;int tmp2_l=mid+1;int start=l;while(tmp1_l<=mid && tmp2_l<=r){if(ans[tmp1_l]<=ans[tmp2_l]){tmp[start++]=ans[tmp1_l++];}else{tmp[start++]=ans[tmp2_l++];cnt+=(ll)(mid-tmp1_l+1);}}while(tmp1_l<=mid) tmp[start++]=ans[tmp1_l++];while(tmp2_l<=r)   tmp[start++]=ans[tmp2_l++];for(int i=l;i<=r;i++) ans[i]=tmp[i];
}inline void kaven(int l,int r){if(l>=r) return ;int mid=(l+r)>>1;kaven(l,mid);kaven(mid+1,r);Union(l,r,mid);
}int main(){int n,x,y;while(scanf("%d%d%d",&n,&x,&y)==3){cnt=0;for(int i=1;i<=n;i++) scanf("%d",&ans[i]);kaven(1,n);ll money=x<y?x:y;printf("%lld\n",cnt*money);}
}
  相关解决方案