Sicily 13983. Milk Scheduling

13983. Milk Scheduling

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes only one unit of time to milk.

Being impatient animals, some cows will refuse to be milked if Farmer John waits too long to milk them. More specifically, cow i produces g_i gallons of milk (1 <= g_i <= 1000), but only if she is milked before a deadline at time d_i (1 <= d_i <= 10,000). Time starts at t=0, so at most x total cows can be milked prior to a deadline at time t=x.

Please help Farmer John determine the maximum amount of milk that he can obtain if he milks the cows optimally.

Input

* Line 1: The value of N.

* Lines 2..1+N: Line i+1 contains the integers g_i and d_i.

Output

* Line 1: The maximum number of gallons of milk Farmer John can obtain.

Sample Input

4
10 3
7 5
8 1
2 1

Sample Output

25

Hint

In the sample, there are 4 cows. The first produces 10 gallons of milk if milked by time 3, and so on. Farmer John milks cow 3 first, giving up on cow 4 since she cannot be milked by her deadline due to the conflict with cow 3. Farmer John then milks cows 1 and 2.

Problem Source

2015年每周一赛第六场

按照g大优先、d小优先排序,对于一个奶牛,优先放到尽量靠近d的时间点,这样尽可能的延后,给了其他的奶牛充分的时间……

#include <iostream>
#include <algorithm>
using namespace std;

struct cow {
	int g, d;
	cow(int g = 0, int d = 0) {
		this->g = g;
		this->d = d;
	}
};

bool cmp(const cow & c1, const cow & c2) {
	if (c1.g == c2.g) return c1.d < c2.d;
	else return c1.g > c2.g;
}

cow c[10005];
bool used[10005];

int haveTime(int d) {
	for (int i = d; i > 0; i--) if (!used[i]) return i;
	return 0;
}

int main() {
	std::ios::sync_with_stdio(false);

	int n;
	cin >> n;
	for (int i = 0; i < n; i++) cin >> c[i].g >> c[i].d;
	sort(c, c + n, cmp);
	int t = 4, ans = 0;
	for (int i = 0; i < n; i++) {
		int feedDay = haveTime(c[i].d);
		if (feedDay) {
			used[feedDay] = true;
			ans += c[i].g;
		}
	}
	cout << ans << endl;

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值