HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838
Cow Sorting
Problem Description
Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y.
Please help Sherlock calculate the minimal time required to reorder the cows.
Input
Line 1: A single integer: N
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
Output
Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.
Sample Input
3
2
3
1
Sample Output
7
Hint
Input Details
Three cows are standing in line with respective grumpiness levels 2, 3, and 1.
Output Details
2 3 1 : Initial order.
2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4).
1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).
#include<stdio.h>
#include<string.h>
#define ll __int64
ll c[+],v[+];
int n;
int Lowbit(int k)
{
return (k&-k);
}
void update(int pos,int num,int val)
{
while(pos<=n)
{
c[pos]+=num;
v[pos]+=val;
pos+=Lowbit(pos);
}
}
ll sum_count(int pos)
{
ll s=; while(pos>)
{
s+=c[pos];
pos-=Lowbit(pos);
}
return s;
}
ll sum(int pos)
{
ll s=; while(pos>)
{
s+=v[pos];
pos-=Lowbit(pos);
}
return s;
}
int main()
{
int i,x;
while(scanf("%d",&n)!=-)
{
memset(c,,sizeof(c));
memset(v,,sizeof(v));
ll ans=,k2,k1;
for(i=;i<=n;i++)
{
scanf("%d",&x);
update(x,,x);
k1=i-sum_count(x);///到此为止 比x大的个数;
/*sum_count[x] 为输入i个数的时候 x之前有sum_count[x]个比x小的数 用i相减则为大于x的个数*/
if(k1!=)
{
k2=sum(n)-sum(x);///到此为止 比x大的数的和;
ans+=x*k1+k2;///到此为止 比x大的数与x交换之后的和;
}
}
printf("%I64d\n",ans);
}
return ;
}
/*坑爹的题 必须用 __int64 不能用long long 不能用int ╮(╯▽╰)╭*/
HDU Cow Sorting (树状数组)的更多相关文章
- hdu 2838 Cow Sorting (树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
\(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
随机推荐
- powershell例子
例子如下: $ErrorActionPreference="Stop" function getlist{ ls D:\tmp2|select name,extension,ful ...
- Debian/Ubuntu server上安装安全更新
原始链接:http://serverfault.com/questions/270260/how-do-you-use-apt-get-to-only-install-critical-securit ...
- 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)
这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...
- SQL排序问题
''按多个字段排序 Select * From Job order by job desc,id asc ''按首字符(非数字)排序 )) ) end ''按首字符分组 ) ''合并Order by排 ...
- 解决fedora25安装vmware12问题:
运行vmware需要几个工具.gcc 编译工具是必须要有的.dnf groupinstall “Development tools“rpm -qa |grep kernel-headersrpm -q ...
- cnetos7.0 安装mysql
CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载 [root@localhost ~]# wget http://dev.mysql.com/ge ...
- 安天AVL联合小米MIUI首擒顽固病毒“不死鸟”
不死鸟作为希腊神话中的一种怪物,拥有不断再生的能力,每当寿限将至时,它会在巢穴中自焚,并在三天后重新复活.就在近期,安天AVL移动安全团队和小米MIUI安全中心发现了病毒界的“不死鸟”,其顽固程度之深 ...
- Java研发岗位面试归类A(附答案)
题目来自http://www.codeceo.com/article/201-java-interview-qa.html,答案自己网上找的,如有疏漏,欢迎斧正.一起学习,共同进步. 一.Java基础 ...
- Python 创建本地服务器环境生成二维码
一. 需求 公司要做一个H5手机端适配页面,因技术问题所以H5是外包的,每次前端给我们源码,我们把源码传到服务器让其他人访问看是否存在bug,这个不是很麻烦吗?有人说,可以让前端在他们的服务器上先托管 ...
- Python 实现Windows开机运行某软件
开机运行:随系统启动的应用程序,当系统启动之后会自动加载的应用 在注册表中添加启动项便可实现开机启动. 代码如下: # -*- coding:utf-8 -*- import win32api imp ...