HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组
本题题意是,给你一个长度为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
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.
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.
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int v, id;
bool operator < (const node &a) const
{
if (v == a.v)
return id < a.id;
return v < a.v;
}
}t[]; int ch[];
int ch1[];
int n; void update(int x)
{
int temp = x;
while (temp <= n)
{
ch[temp]++;
temp += temp & (-x);
}
} int getsum(int x)
{
int sum = ;
while (x)
{
sum += ch[x];
x -= x & (-x);
}
return sum;
} int main()
{
ios::sync_with_stdio(false);
int a, b;
while (cin>> n >> a >> b)
{
memset(ch, , sizeof(ch));
for (int i = ; i <= n; i++)
{
cin >> t[i].v;
t[i].id = i;
} sort(t+,t+n+); long long ans = ;
for (int i = ; i <= n; i++)
{
update(t[i].id);
ans += i - getsum(t[i].id);
}
ans = min(ans*a, ans*b);
cout << ans << endl;
}
return ;
}
HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组的更多相关文章
- 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 ...
 - HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造
		
Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
 - HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和
		
题意是给了一种矩阵的生成方式 让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值) 没看标程之前硬撸写了160行 用了前缀和以后代码量缩短到原来的1/3 根据规律可以推导出这个矩阵是在不断重复 ...
 - 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 ...
 - HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube
		
就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...
 - HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence
		
这个题的题意是给你n个字符串,认定()是一种平衡的串,两个以上连续的()()也是一种平衡的串,如果一对括号里面包含一个平衡的串,这个括号也被算在这个平衡的串之内, 如(()(()))是一个长度为8的平 ...
 - HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple
		
题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...
 - HDU6301-2018ACM暑假多校联合训练1004-Distinct Values
		
题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...
 - HDU6308-2018ACM暑假多校联合训练1011-Time Zone
		
题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...
 
随机推荐
- 2014.8.25 VS新建项目模板消失解决方法
			
Vs2005 新建项目时windows应用程序模板消失问题解决方法: 1:进入C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\ ...
 - 201671010127   2016—2017-2  通过一个小程序对Java的再认识。
			
学习了将近四周的Java语言,对于Java语言,我也有了更进一步的理解,出于对Java语言的喜爱,我总是喜欢没事的时候,自己敲一些很简单的代码,一边学习Java语言,一边对比C语言,往往可以帮助我们更 ...
 - get-task-allow有什么用
			
[failed to get the task for process问题] A: Why am I getting "Error launching remote program: fai ...
 - codeforces:Roads in the Kingdom分析和实现
			
题目大意:国家有n个城市,还有n条道路,每条道路连通两个不同的城市,n条道路使得所有n个城市相互连通.现在国家经费不足,要关闭一条道路.国家的不便度定义为国家中任意两个不同的城市之间的距离的最大值,那 ...
 - 788. Rotated Digits 旋转数字
			
[抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...
 - 11-内涵段子-爬虫(python+正则)
			
爬取内涵段子,使用正则进行简单处理: #_*_ coding: utf-8 _*_ ''' Created on 2018年7月14日 @author: sss function:爬去内涵段子(静态网 ...
 - Django--form保存用户输入内容
			
需求 用户提交form时,如果报错,页面中的用户信息还在(除了密码),没有被刷新掉,不用用户再次输入. 速查 views.py 1 2 3 def login(request): obj = ...
 - Palindromeness CodeChef - PALPROB
			
传送门 分析 有中文题面所以就不写题目大意了 我们先建出回文树 然后根据fail信息建出一棵树 从根向下dfs,中间记录每一个len出现在哪个节点即可 代码 #include<iostream& ...
 - php连接redis
			
$redis = new Redis(); $redis->connect();
 - js日期 操作
			
//重写toString方法,将时间转换为Y-m-d H:i:s格式 Date.prototype.toString = function(){ ) + "-" + this.ge ...