POJ 3045 贪心

题意

传送门 POJ 3045

考虑任意 2 头牛的情况,要使 W i S j < W j S i W_{i}-S_{j}<W_{j}-S_{i} 的牛 j j 位于牛 i i 下方,此时 S i + W i < S j + W j S_{i}+W_{i}<S_{j}+W_{j} 。对牛排序,若任意 2 头牛交换位置,其 m a x { W i S i } max\{\sum W_{i}-S_{i}\} 增大。故此时是最优排序,顺序搜索答案即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define min(a,b)    (((a) < (b)) ? (a) : (b))
#define max(a,b)    (((a) > (b)) ? (a) : (b))
#define abs(x)    ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
#define eps 1e-3
#define PI 3.14159265358979323846
#define MAX_N 50005
using namespace std;
typedef pair<int, int> P;
int N;
P cow[MAX_N];

bool cmp(const P& a, const P& b){
	return a.first + a.second < b.first + b.second;
}

int main(){
	while(~scanf("%d", &N)){
		for(int i = 0; i < N; i++) scanf("%d%d", &cow[i].first, &cow[i].second);
		sort(cow, cow + N, cmp);
		int w = 0, res = -INF;
		for(int i = 0; i < N; i++){
			res = max(res, w - cow[i].second);
			w += cow[i].first;
		}
		printf("%d\n", res);
	}
	return 0;
}
发布了91 篇原创文章 · 获赞 1 · 访问量 1611

猜你喜欢

转载自blog.csdn.net/neweryyy/article/details/105169443