hdu 2838 Cow Sorting(树状数组)
Cow Sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2239 Accepted Submission(s): 711
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.
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
3
2
3
1
7HintInput 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).
题意:给出一组数从1到N打乱,要求把数组又一次有序(从小到大),仅仅能交换相邻的两个数字。代价为相邻两个数字和。求最小代价?
思路:对于每一个数字x,我们仅仅须要把它和前面比它大的数字交换。求出交换代价,反复运行就能得出答案。
这个代价就是,比它大的数字个数t*x+前面比它大的数字和。
<pre name="code" class="cpp">#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<functional>
#include<iostream>
using namespace std;
#define N 100005
#define ll __int64
int a[N],cnt[N],n; //记录数字个数
ll sum[N]; //记录数字和
int lowbit(int x)
{
return x&(-x);
}
void add(int x)
{
int d=x;
while(x<=n)
{
cnt[x]++;
sum[x]+=d;
x+=lowbit(x);
}
}
int sum1(int x) //求比x小的数字已经出现几个(包含x)
{
int s=0;
while(x)
{
s+=cnt[x];
x-=lowbit(x);
}
return s;
}
ll sum2(int x) //求当前出现的比x大的数字和
{
ll s=0;
while(x)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int i,k,t;
while(scanf("%d",&n)!=-1)
{
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
ll ans=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
add(a[i]);
t=sum1(a[i]);
k=i-t; //前面有几个比x大的数
if(k!=0)
{
ans+=(ll)a[i]*k; //注意数字会超出int
ans+=sum2(n)-sum2(a[i]); //求当前位置出现的比x大的数字和
}
}
printf("%I64d\n",ans);
}
return 0;
}
hdu 2838 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 ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
\(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
- hdu 2838 Cow Sorting (树状数组+逆序对)
题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
随机推荐
- 推荐国内外优秀+免费CDN加速站点及公共cdn加速库
-----------------------------------------------------------------免费CDN加速站点 1.CloudFlare CloudFlare可能 ...
- CF 8D Two Friends (三分+二分)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意 :有三个点,p0,p1,p2.有两个人ali ...
- W英语: 紧急, 非紧急
take your time 慢慢来 It is not urgent. Take it easy please. 不急,慢慢来.
- Java多线程实现生产者消费者延伸问题
在操作系统中有一类问题被称为生产者消费者问题:意为,有数个生产者生产产品,有数个消费者消费产品,他们共享一定数量的缓存. 这里用java多线程编程,实现生产者消费者问题的一种延伸,橘子苹果问题. 题目 ...
- DDD领域驱动设计的理解
DDD领域驱动设计的理解 从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来解决. 比如,我是一家企业,然后我觉得我现在线下销售自己的产品还不够,我希望能 ...
- 正确理解Python文件读写模式字w+、a+和r+
w+ 和 r+的差别不难理解.还有a+ +同一时候读写,就可以读又可写,边写边读.边读边写,不用flush,用seek 和 tell可測得. fp = open("a.txt", ...
- Swift - 九宫格图片缩放总结样例
1,图片左中右三宫格缩放形式 //左右14像素不变形,中间缩放 let imgTrackRight = UIImage(named:"slider_max") let imgRig ...
- 导航条——flash导航条
1.概述 在一些个性网站中,网站导航的首选就是flash导航条,flash导航条可以给浏览者带来更好的视觉效果,是网站个性的主要体现之一. 2.技术要点 主要应用Flash动作脚本中的Button类的 ...
- 14.6.3 Grouping DML Operations with Transactions 组DML操作
14.6.3 Grouping DML Operations with Transactions 组DML操作 默认情况下,连接到MySQL server 开始是以启动自动提交模式, 会自动提交每条S ...
- 用yum查询想安装的软件
1.使用YUM查找软件包 命令:yum search~ 2.列出所有可安装的软件包 命令:yum list 3.列出所有可更新的软件包 命令:yum list updates 4.列出所有 ...