Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.
So, today is the day when Peter's parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with dnumbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.
The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbersminTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.
In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them.
题目大意是 小皮一共要学习n天共sum小时,但是有约束条件:
第i天最少学习Li小时,最多学习Hi小时。即Li <= Di <= Hi。
问,如何把总的sum小时分配给n天。
刚开始感觉像是个背包,但是后来一想,就是先让第i天学习Li小时,然后把剩下的小时一次分配就好了。
也就是一个贪心的过程了。
另一方面,感觉也很像差分约束系统,以后有机会在好好学习一下差分约束系统怎么破吧。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
using namespace std;
pair<int, int> days[35];
int ans[35];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int d,sum;
scanf("%d %d", &d, &sum);
for (int i = 0; i < d; i++) scanf("%d %d", &(days[i].first), &(days[i].second));
for (int i = 0; i < d; i++) {
ans[i] = days[i].first;
sum -= ans[i];
}
if (sum < 0) {
printf("NO\n");
return 0;
}
for (int i = 0; i < d; i++) {
if (sum == 0) break;
int dlt = days[i].second - days[i].first;
ans[i] += (dlt < sum ? dlt : sum);
sum -= (dlt < sum ? dlt : sum);
}
if (sum > 0) {
printf("NO\n");
return 0;
}
printf("YES\n%d", ans[0]);
for (int i = 1; i < d; i++) printf(" %d", ans[i]);
printf("\n");
return 0;
}