hdu 6318
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<j≤n 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.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.
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.
1≤n,x,y≤100000, 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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 const int N=1e5+9; 7 typedef long long ll; 8 /* 9 例如: 10 4 2 3 1 有5个逆序对 11 可以先交换两次相邻位置 2 4 3 1 2 3 4 1 逆序对变成了5-2==3 12 可以先交换一次相邻位置 2 4 3 1 逆序对变成了5-1==4 13 分析:4 2 3 1 可以任意选两个相邻的数交换,如4 2 ,那么在交换4 2 14 时,1,3所对应的逆序对不会变(1,3,左边比他们大的数的数目不变) 15 但4 2 变成了2 4 因此这个逆序对没了。肯定把逆序对变成非逆序对。 16 y 对应一个逆序对 ,x 对应一个逆序对 17 因此 最小花费=逆序对*min(x,y) . 18 */ 19 // 注意到逆序对=交换相邻需要交换的次数 20 int a[N],b[N]; 21 ll ans; 22 int n,x,y; 23 void gsort(int l,int r) 24 { if(l==r) return ; 25 int mid=(r+l)>>1; 26 gsort(l,mid);gsort(mid+1,r); 27 int k=l; 28 int i=l,j=mid+1; 29 while(i<=mid&&j<=r){ 30 if(a[i]<=a[j]){ 31 b[k++]=a[i++]; 32 } 33 else{ 34 b[k++]=a[j++]; 35 ans+=mid-i+1; 36 } 37 } 38 while(i<=mid) b[k++]=a[i++]; 39 while(j<=r) b[k++]=a[j++]; 40 for(int i=l;i<=r;i++) a[i]=b[i]; 41 } 42 void solve() 43 { 44 ans=0; 45 gsort(1,n); 46 printf("%lld\n",ans*min(x,y)); 47 } 48 int main() 49 { 50 while(~scanf("%d%d%d",&n,&x,&y)){ 51 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 52 solve(); 53 } 54 return 0; 55 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 using namespace std; 7 #define ll long long 8 #define N 100009 9 #define gep(i,a,b) for(ll i=a;i<=b;i++) 10 #define mem(a,b) memset(a,b,sizeof(a)) 11 #define lowbit(x) x&(-x) 12 ll c[N],a[N],n; 13 ll x,y; 14 struct Node{ 15 ll id,val; 16 }nod[N]; 17 void update(ll i,ll num) 18 { 19 while(i<=n){ 20 c[i]+=num; 21 i+=lowbit(i); 22 } 23 } 24 ll getsum(ll n) 25 { 26 ll sum=0; 27 while(n>0){ 28 sum+=c[n]; 29 n-=lowbit(n); 30 } 31 return sum; 32 } 33 bool cmp(Node a,Node b) 34 { 35 return a.val<b.val; 36 } 37 int main() 38 { 39 while(~scanf("%lld%lld%lld",&n,&x,&y)){ 40 mem(a,0); 41 mem(c,0); 42 gep(i,1,n){ 43 scanf("%lld",&nod[i].val); 44 nod[i].id=i; 45 } 46 sort(nod+1,nod+1+n,cmp);//要先排序 47 a[nod[1].id]=1;//先让最小的为1 48 gep(i,2,n) 49 { 50 if(nod[i].val!=nod[i-1].val){//离散化 51 a[nod[i].id]=i; 52 } 53 else{ 54 a[nod[i].id]=a[nod[i-1].id]; 55 } 56 } 57 ll ans=0; 58 gep(i,1,n){ 59 update(a[i],1); 60 ans+=getsum(n)-getsum(a[i]);//左边比我大的数的数目 61 } 62 printf("%lld\n",ans*min(x,y)); 63 } 64 return 0; 65 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现