zoukankan      html  css  js  c++  java
  • 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 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
     
     
     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
    ",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
    ",ans*min(x,y));
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    APP移动端开发html模板
    javascript使用百度地图api和html5特性获取浏览器位置
    javascript与jQuery的each,map回调函数参数顺序问题
    fiddler操作改到本地
    做54活动总结
    移动端文字垂直居中问题兼容
    ADO。Net(二)——防止SQL注入攻击
    ADO.Net(一)——增、删、改、查
    面向对象(六)——设计模式和委托
    面向对象(五)——类库和五大原则
  • 原文地址:https://www.cnblogs.com/tingtin/p/9370249.html
Copyright © 2011-2022 走看看