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 ...
随机推荐
- python之路二十
一, $.ajax,这个是JQuery对ajax封装的最基础步,通过使用这个函数可以完成异步通讯的所有功能.也就是说什么情况下我们都可以通过此方法进行异步刷新的操作.但是它的参数较多,有的时候可能会麻 ...
- asp.net 文件下载(txt,rar,pdf,word,excel,ppt)
aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况 ...
- VMWARE修改CPUID
在cmd shell下执行wmic cpu get ProcessorId命令,可是查看机器的cpuID,但这个命令显示的只是2组ID,实际CPUID,有4组 通过更改虚拟机配置文件.vmx可是实现任 ...
- Python 实现简单的 Web
简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...
- TFS工作区(Workspaces )命令
Workspaces 命令 tf workspaces [/owner:ownername] [/computer:computername] [/server:servername] [/forma ...
- Centos挂载第二块硬盘
作为一个初创小公司的架构师,工作内容纷繁复杂,涉及了系统管理员.数据库管理员.架构师.高级软件工程师.项目经理的部分. 今天的任务是安装公司的服务器,使用centos6.7.安装过程就不用细讲了. ...
- JavaScript跨域提交数据
1.通过jsonp跨域 场景:假设前台有JS方法"crossJS", 1.1发送请求http://www.xxx.com/?callback=crossJS.(创建一个scr ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- 安装opensuse时遇到的一些问题
1.硬盘安装suse的时候提示找不到源,因为是2块硬盘所以需要mount一下硬盘. 2.安装N卡驱动的时候,推荐一键安装,不然需要把所有GCC和make安装好,并且禁用系统的nouneau.
- IDEA快捷键+使用小技巧
一 常用快捷键 Alt+回车 导入包,自动修正,当引入的类需要异常捕获的时候 Ctrl+Shift+Space 自动补全代码,"new"字符,还可以引入强制转换的 Ctrl-Alt ...