B. Before an Exam
来源:互联网 发布:java和c语言的区别 编辑:程序博客网 时间:2024/04/27 21:06
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 thanminTimei 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 timetablesс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 scheduleishould 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 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.
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 485 7
NO
2 50 13 5
YES1 4
解题说明:题目意思翻译过来就是有n组min max值以及一个sum值,现在要求n个介于min和max之间的数,让其和为sum。此题可以先从sum中减去所有的min,然后对剩下的值进行处理,对其分配时不能超过每一对max-min的值,如果能全部分完,则满足要求。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include<set>#include <algorithm>using namespace std;int main() {int i,j,k,l,sum,d;int a[33],b[33];scanf("%d %d",&d,&sum);for(i=0;i<d;i++) {scanf("%d %d",&a[i],&b[i]);sum -= a[i];}if(sum < 0) { printf("NO\n"); }else{for(i=0;i<d;i++) {if(sum>0) {j = b[i] - a[i];if(j>sum){k=sum;}else{k=j;}sum -= k;a[i] += k;}}if(sum > 0) {printf("NO\n");}else{printf("YES\n");for(i=0;i<d;i++){printf("%d ",a[i]);}printf("\n");}}return 0;}
- B. Before an Exam
- B - Before an Exam
- CF 4B Before an Exam
- Before an Exam(比赛3.B)
- codeforces Round4 B. Before an Exam
- Codeforces 4B Before an Exam
- codeforces 4B Before an Exam
- Codeforces 4B. Before an Exam
- CodeForces 4B - Before an Exam
- CodeForces #4B. Before an Exam
- CodeForces 4B Before an Exam
- 4B. Before an Exam【水】
- Codeforces 4B Before an Exam
- A - Before an Exam
- CodeForces 4B Before an Exam(数学+模拟)
- Codeforces 4B-Before an Exam(构造)
- CodeForces 4B Before an Exam(贪心算法)
- CodeForces 4B Before an Exam(DP)
- C语言 打印*号金字塔~~~
- 关于iOS App的切图 .
- dom4j解析xml字符串实例
- 通过手机远程关机
- POJ 3177 Redundant Paths + POJ 3352 Road Construction
- B. Before an Exam
- onbeforeunload与a标签在IE中的冲突bug
- Android 通讯录分组导航和挤压动画
- 音频编码汇总
- VS2010 MFC工程中 错误提示: IntelliSense: 无法打开源文件 "defines.h"
- 浅谈new和delete
- 关于操作符“>>”和“<<”的重载
- memcpy与memmove
- js控制按钮n秒后可用