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. 思路: 因为数据量比较多,所以需 ...
随机推荐
- 免插件打造wordpress投稿页面
一.新建投稿页面模板 把主题的 page.php 另存为 tougao.php,并且在第一行的 <?php 之后添加模板的标识注释: /* Template Name: tougao */ 紧接 ...
- JQuery - 去除所有空格
$('#submit').click(function () { //去除所有空格 String.prototype.NoSpace = function () { return this.repla ...
- asp.net下用js实现弹出子窗口选定值并返回
对应上一篇博客代码: 父页面: <head runat="server"> <meta http-equiv="X-UA-Compatible" ...
- Delphi中JSon SuperObject 使用:数据集与JSON对象互转
在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包 ...
- Swift - 跑酷游戏开发(SpriteKit游戏开发)
一,下面演示了如何开发一个跑酷游戏,实现的功能如下: 1,平台工厂会不断地生成平台,并且向左移动.当平台移出游戏场景时就可将其移除. 2,生成的平台宽度随机,高度随机.同时短平台踩踏的时候会下落. 3 ...
- SuperSocket应用之FTP源码解析
一 简述 命令行协议是一种使用比较多的协议,其优点在于使用简单易于扩展性,同时也利于解析和使用.FTP,POP,SMTP等均采用命令行协议,其中FTP在早起互联网时期成为网络资源共享的主要方式,可见F ...
- js / ajax 成功提交后怎么跳转到另外一个页面?
把success那段改成 success : function (r) { if ( r.status == 'error' ){ alert(msg[r.msgno]); } else if (r. ...
- TApplication.Initialize的前世今生
---------------------------------------------------------------------------------------------------- ...
- 网络数据(socket)传输总结
环境限定:TCP/IP下的socket网络传输:C/C++开发语言,32/64位机. 目前有两种方式对数据进行传输:1)字符流形式,即将数据用字符串表示:2)结构型方式,即将数据按类型直接传输. 1) ...
- Test oracle db iops
Today, i need to test one database's iops and do something for oracle db's io test. How to test the ...