USACO2005 Cow Acrobats /// 前缀和 oj23402
题目大意:
N (1 ≤ N ≤ 50,000)头牛叠罗汉 找到最优排序使所有牛的风险值总和最小
每头牛重量 (1 ≤ Wi ≤ 10,000) 力量 (1 ≤ Si ≤ 1,000,000,000)
自上往下编号1~n,第i头牛的风险值 = i~n的体重总和 - i的力量.
* Line 1: A single line with the integer N.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, Wi and Si.
* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
3
10 3
2 5
3 3
2
OUTPUT DETAILS:
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.
观察规律:
假设一开始为最优排序
W = W(1) + W(2) + ... + W(i-1) 则
牛(i)风险 = W - S(i)
牛(i+1)风险 = W + W(i)- S(i+1)
若交换位置
牛(i+1)风险 = W - S(i+1)
牛(i)风险 = W + W(i+1)- S(i)
因为一开始为最优排序
所以 W + W(i)- S(i+1)< W + W(i+1)- S(i)
则存在 W(i)+ S(i)< S(i+1)+ W(i+1)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,flag[];
long long m;
struct dlh
{
long long w,s,sum;
}d[];
bool cmp(struct dlh q,struct dlh p)
{
return q.sum>p.sum;
}
int main()
{
scanf("%d",&n);
long long cnt=,ans=-INF;
for(int i=;i<=n;i++)
{
scanf("%d%lld",&d[i].w,&d[i].s);
cnt+=d[i].w;
d[i].sum=d[i].w+d[i].s;
}
sort(d+,d++n,cmp);
for(int i=;i<=n;i++)
{
cnt-=d[i].w;
ans=max(ans,cnt-d[i].s);
}
printf("%d\n",ans); return ;
}
USACO2005 Cow Acrobats /// 前缀和 oj23402的更多相关文章
- BZOJ1629: [Usaco2007 Demo]Cow Acrobats
1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 601 Solved: 305[Su ...
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- Cow Acrobats(贪心)
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3686 Accepted: 1428 Desc ...
- POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏
Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4998 Accepted: 1892 Desc ...
- POJ3045 Cow Acrobats —— 思维证明
题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- poj3045 Cow Acrobats (思维,贪心)
题目: poj3045 Cow Acrobats 解析: 贪心题,类似于国王游戏 考虑两个相邻的牛\(i\),\(j\) 设他们上面的牛的重量一共为\(sum\) 把\(i\)放在上面,危险值分别为\ ...
- 【POJ - 3045】Cow Acrobats (贪心)
Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...
- BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...
- [USACO2005][POJ3045]Cow Acrobats(贪心)
题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风 ...
随机推荐
- unittest框架学习笔记三之testsuite
# coding=utf-8 '''created :2018/3/29 author:star project: testsuite'''import unittest,time,osfrom ba ...
- Ubuntu管理员密码设置
最近学习嵌入式编程,首先准备搭建一个嵌入式开发环境. 由于想省钱,就准备搭建一个虚拟的arm系统用于测试学习. 虚拟系统搭建与linux系统上,暂定使用Ubuntu+qemu进行环境搭建. 在进行Ub ...
- 无法将 Ethernet0 连接到虚拟网络”VMnet0″ 详细信息可以在 vmware.log 文件中找到未能连接虚拟机Ethernet0
在 vmware“编辑->虚拟网络设置”里面,点“恢复默认”可解决.
- 前端(二)—— CSS的引入方式、长度与颜色单位、常用样式、选择器
CSS的引入方式.长度与颜色单位.常用样式.选择器 一.CSS的三种引入方式 1.行间式 <!doctype html> <html> <head> <met ...
- 卷积神经网络学习笔记(CNN)
看了很多关于卷积神经网络的论文和资料 可是我发现一个问题,就是pooling会出现一个问题 我能找到的代码里面都是用的均值子采样,如果改成最大或最小,或P范数都会在学习训练的时候遇到不知道怎么处理的问 ...
- http over git server
编译安装git 参考 <CentOS7编译安装git> 安装httpd yum install httpd -y 安装gitweb yum install gitweb -y 创建项目根目 ...
- 安装jdk 并放在 /usr/java/default 目录下
安装步骤 . mkdir /usr/java 2. tar -xvf jdk*.tar.gz -C /usr/java/ 记得将* 换成版本即可3. ln -s /usr/java/jdk* /usr ...
- MySQL 8.0及以上版本设置密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
- android中的属性资源
属性资源可以很好的控制自定义View组件的外观行为. 属性资源放置在/res/values目录下,属性资源文件的根目录元素是<resources.../>,该元素包含如下两个子元素: at ...
- redis主从复制和哨兵
摘自:https://www.cnblogs.com/leeSmall/p/8398401.html 一.Redis主从复制 主从复制:主节点负责写数据,从节点负责读数据,主节点定期把数据同步到从节点 ...