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;
}

你可能感兴趣的:(poj 3045 Cow Acrobats(灵活题,推论))