poj 3045 Cow Acrobats(灵活题,推论)


Input

* Line 1: A single line with the integer N. 

* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i. 

Output

* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.

Sample Input

3
10 3
2 5
3 3

Sample Output

2
http://poj.org/problem?id=3045

我们很容易就可以得到一个式子:当前危险值=sum(w[1]..w[i-1])-s[i];
那么下一个危险值=当前危险值+w[i]+s[i]-s[i+1];

多出了什么?

w[i]+s[i]就新增的,这个值在前面越小,危险值就会越平均,最大的危险值就越小。(这里还有点疑问,应该用dp思想考虑?)

于是这道题的解法就显而易见了,按w[i]+s[i]从小到大排序,计算最大危险值 (转载)

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
int n;
pair<int,int> x[800001]; 
int cmp(pair<int,int>a,pair<int,int>b){
	return a.first+a.second<b.first+b.second;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		scanf("%d %d",&x[i].first,&x[i].second);//重量和力气 
	}
	sort(x,x+n,cmp); 
	int s=0,max=-1000000000;
	for(int i=0;i<n;++i){
		if(s-x[i].second>max)
			max=s-x[i].second;
		s+=x[i].first;
	}
	printf("%d",max);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值