本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y)

实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大,所以使用树状数组时需要先离散化

离散化后排序有两种方式,一种是先按value排,再按id排,另一种是按value排后,把重复的统一化

这里采用第一种方式

Swaps and Inversions

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

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<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.
 
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
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int v, id;
  11. bool operator < (const node &a) const
  12. {
  13. if (v == a.v)
  14. return id < a.id;
  15. return v < a.v;
  16. }
  17. }t[];
  18.  
  19. int ch[];
  20. int ch1[];
  21. int n;
  22.  
  23. void update(int x)
  24. {
  25. int temp = x;
  26. while (temp <= n)
  27. {
  28. ch[temp]++;
  29. temp += temp & (-x);
  30. }
  31. }
  32.  
  33. int getsum(int x)
  34. {
  35. int sum = ;
  36. while (x)
  37. {
  38. sum += ch[x];
  39. x -= x & (-x);
  40. }
  41. return sum;
  42. }
  43.  
  44. int main()
  45. {
  46. ios::sync_with_stdio(false);
  47. int a, b;
  48. while (cin>> n >> a >> b)
  49. {
  50. memset(ch, , sizeof(ch));
  51. for (int i = ; i <= n; i++)
  52. {
  53. cin >> t[i].v;
  54. t[i].id = i;
  55. }
  56.  
  57. sort(t+,t+n+);
  58.  
  59. long long ans = ;
  60. for (int i = ; i <= n; i++)
  61. {
  62. update(t[i].id);
  63. ans += i - getsum(t[i].id);
  64. }
  65. ans = min(ans*a, ans*b);
  66. cout << ans << endl;
  67. }
  68. return ;
  69. }

HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组的更多相关文章

  1. HDU6333-2018ACM暑假多校联合训练1002-Harvest of Apples-莫队+费马小定理

    题意很简单啦,求S(n,m)的值 通过打表我们可以知道 S(n + 1, m) = S(n, m) * 2 - C(n, m); S(n - 1, m) = (S(n, m) + C(n - 1, m ...

  2. HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造

    Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  3. HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和

    题意是给了一种矩阵的生成方式 让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值) 没看标程之前硬撸写了160行 用了前缀和以后代码量缩短到原来的1/3 根据规律可以推导出这个矩阵是在不断重复 ...

  4. HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  5. HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube

    就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...

  6. HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence

    这个题的题意是给你n个字符串,认定()是一种平衡的串,两个以上连续的()()也是一种平衡的串,如果一对括号里面包含一个平衡的串,这个括号也被算在这个平衡的串之内, 如(()(()))是一个长度为8的平 ...

  7. HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple

    题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...

  8. HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

    题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...

  9. HDU6308-2018ACM暑假多校联合训练1011-Time Zone

    题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...

随机推荐

  1. Dev DateEdit控件格式设置

    设置日期显示格式: 设置三个属性(显示时.编辑时) dtPubDate.Properties.DisplayFormat.FormatString = "yyyy-MM-dd";  ...

  2. 取当前时间,格式为,yyyy-mm-dd hh:mm:ss

    function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month = now.getM ...

  3. leetcode144

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  4. 定时器Quartz ClassNotFound org.springframework.scheduling.quartz.SchedulerFactoryBean

    转自:https://blog.csdn.net/truong/article/details/37508003 没有添加spring-context-support-3.2.4.RELEASE.ja ...

  5. Window setInterval() 方法

    Window 对象 转自:https://www.w3cschool.cn/jsref/met-win-setinterval.html 定义和用法 setInterval() 方法可按照指定的周期( ...

  6. Entitlements

    [Entitlements] Entitlements confer specific capabilities or security permissions to your iOS or OS X ...

  7. c++ 流状态

    这里是对cin中函数的作用的补充.

  8. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

  9. 189. Rotate Array 从右边开始翻转数组

    [抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

  10. 数字图像处理实验(9):PROJECT 04-05,Correlation in the Frequency Domain 标签: 图像处理MATLAB 2017-05-25 10:14

    实验要求: Objective: To know how to implement correlation of 2 functions in the frequency domain and, us ...