1.题意:给定每一天学习的最短时间和最长时间,现在给定在d天内用的总时间,求是否可以每天都按规定来学习,是则打印出每天学习用的时间,否则打印出NO。
题目:
B - Before an Exam
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
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 d numbers, 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.
Input
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 numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.
Output
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.
Sample Input
Input
1 48
5 7
Output
NO
Input
2 5
0 1
3 5
Output
YES
1 4
2.样例:
2 5 0 1 3 5
分别代表
2天,总时间为5;
第一天的最小时间,第二天的最长时间
第一天的最小时间,第二天的最长时间
3.思路:用min记录最少用的时间,然后用总时间值与最少时间值的差,分配到各天去
4.代码:
struct node
{
int min;
int max;
int sub;
int ans;
} a[35];
int main()
{
int d,sum,m,summax=0,summin=0,j,flag=0,anss;
summin=0;
scanf("%d%d",&d,&m);
for(int i=0; i<d; i++)
{
scanf("%d%d",&a[i].min,&a[i].max);
a[i].sub=a[i].max-a[i].min;
summax+=a[i].max;
summin+=a[i].min;
a[i].ans=a[i].min;
}
if(m>summax||m<summin)
{
flag=1;
}
else
{
j=0;
int mm=m-summin;
while(mm>0)
{
a[j].ans+=a[j].sub;
mm-=a[j].sub;
anss=mm;
if(mm>=0)
j++;
}
a[j].ans=a[j].ans+mm;
}
if(flag==0)
{
printf("YES\n");
for(int i=0; i<d; i++)
printf("%d ",a[i].ans);
printf("\n");
}
if(flag==1)
printf("NO\n");
return 0;
}
本文介绍了一个算法,用于解决学生在有限时间内如何合理安排每天的学习时间,以满足家长规定的每日学习时长范围,并确保总学习时间符合预期。通过计算最小和最大可能的学习时间,算法能够判断是否有可行的学习计划。

348

被折叠的 条评论
为什么被折叠?



