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.
1 48 5 7
NO
2 5 0 1 3 5
YES 1 4
题目大意:
要求在n天内学习t小时,给出每一天可以学习的最小时间和最大时间。问能不能实现,若可以输出时间表。
解题思路:
保证在区间范围内的情况下直接贪心。
#include <cstdio>
using namespace std;
struct node
{
int min1, max1;
}da[35];
int main()
{
int d, t, uplim = 0, downlim = 0;
scanf("%d%d", &d, &t);
for (int i = 0; i < d; i++){
scanf("%d%d", &da[i].min1, &da[i].max1);
downlim += da[i].min1;
uplim += da[i].max1;
}
if (uplim < t || downlim > t)
printf("NO\n");
else{
printf("YES\n");
t -= downlim;
for (int i = 0; i < d; i++){
if (t >= da[i].max1 - da[i].min1){
printf("%d ", da[i].max1);
t -= da[i].max1 - da[i].min1;
}
else if (t == 0){
printf("%d ", da[i].min1);
}
else if (t < da[i].max1 - da[i].min1){
printf("%d ", t + da[i].min1);
t = 0;
}
}
}
return 0;
}