poj_3045 贪心
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7408 | Accepted: 2770 |
Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts.
The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.
Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.Input
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
Output
Sample Input
3
10 3
2 5
3 3
Sample Output
2
Hint
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.
#include <iostream>
#include <stdio.h>
#include <algorithm> using namespace std; struct dat
{
int W;
int S;
} data[]; bool cmp(dat a, dat b)
{
return a.W+a.S > b.W+b.S;
} int main()
{
int n;
while(scanf("%d",&n)!=-)
{
for(int i=; i<n; i++)
{
scanf("%d%d",&data[i].W,&data[i].S);
} sort(data, data+n, cmp); long long max=-0x3f3f3f3f, temp;
long long sc=;
for(int i=n-; i>=; i--)
{
temp = sc-data[i].S;
if(temp>max)
max=temp;
sc+=data[i].W;
} printf("%lld\n", max);
}
return ;
}
poj_3045 贪心的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- Ubuntu 15.10 下Tachyon安装
1 系统环境 Ubuntu 15.10, Java 1.7, Hadoop 2.6.0 HA, Spark-1.4.0 三台机器 spark-1423-0001: Master, Worker spa ...
- rocketmq(三 java操作rocket API, rocketmq 幂等性)
JAVA操作rocketmq: 1.导入rocketmq所需要的依赖: <dependency> <groupId>com.alibaba.rocketmq</group ...
- 学习笔记《Java多线程编程实战指南》一
1.1什么是多线程编程 多线程编程就是以线程为基本抽象单位的一种编程范式,和面向对象编程是可以相容的,事实上Java平台中的一个线程就是一个对象.多线程编程不是线程越多越好,就像“和尚挑水”的故事一样 ...
- html网页中如何给文字加入下划线
网页中一些重要的文字或者特殊性高的文字,例如:链接,标注等我们需要加上下划线,那么这里我们就需要使用到<U>标签了,写法如下 字体下划线: <u>这里添加内容</u> ...
- Centos7之系统优化
优化条目: 修改ip地址.网关.主机名.DNS等 关闭selinux,清空iptables 添加普通用户并进行sudo授权管理 更新yum源及必要软件安装 定时自动更新服务器时间 精简开机自启动服务 ...
- 使用sqlplus创建Oracle表空间
登录 dos窗口输入以下命令:sqlplus 回车 提示输入用户名——即创建数据库实例时的用户名 ,用户名输入:sys as sysdba 密码.........回车登录 查看数据文件位置 接下来, ...
- golang初识3 - func
1. 功能块(function block) 格式: func function_name( [parameter list] ) [return_types] { //body } 与delphi的 ...
- 解决pre-commit hook failed (add --no-verify to bypass)的问题
报错图 问题原因: pre-commit钩子惹的祸 当你在终端输入git commit -m"XXX",提交代码的时候, pre-commit(客户端)钩子,它会在Git键入提交信 ...
- debian apache2 修改默认路径
1.修改 /etc/apache2/sites-enable/000-default.conf 将DocumentRoot改成你的路径 2.修改 /etc/apache2/apache2.conf & ...
- 显示开机信息-dmesg
显示开机信息-dmesg kernel会将开机信息存储在ring buffer中.您若是开机时来不及查看信息,可利用dmesg来查看.开机信息亦保存在/var/log目录中,名称为dmesg的文件里. ...