/*
translation:
n头牛要叠罗汉,每头牛有两个属性,力量和重量。每头牛要支撑其上面所有牛的风险是上面所有牛的重量减去这头牛
本身的力量。求一种排列顺序使得最大的风险最少
solution:
贪心即可。重量越大力量越大的牛应该在最低下。
note:
# 一开始以为二分,其实用不着二分
*:对于涉及到需要预先对物品进行排序的算法(特别是贪心之类),经常遇到同一个物品拥有两个属性的情况。常用到的方法有:
1.优先按照某一属性排列,适用于优先考虑某一种属性的情况。
2.按照两个属性之比排列,适用于两个属性随着排列一个递增一个递减的情况
3.按照两个元素之和排列,使用于两个属性随着排列都递增的情况。
date:
2016.11.3
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 50000 + 10;
const int INF = 500000000 + 10;
struct Cow
{
int weight, strength;
} c[maxn];
int n, total_w;
bool cmp(const Cow& a, const Cow& b)
{
return a.weight + a.strength > b.weight + b.strength;
}
int main()
{
while(~scanf("%d", &n)){
total_w = 0;
for(int i = 0; i < n; i++)
scanf("%d%d", &c[i].weight, &c[i].strength), total_w += c[i].weight;
sort(c, c + n, cmp);
int ans = -INF;
for(int i = 0; i < n; i++){ //排在前面的牛处在底层
total_w -= c[i].weight;
ans = max(ans, total_w - c[i].strength);
}
printf("%d\n", ans);
}
return 0;
}
poj3045(简单贪心,根据多个权重排序类型的总结)
最新推荐文章于 2021-06-13 21:00:04 发布