CodeFORCES 4B多组解中找到一个符合题意的解
来源:互联网 发布:手机降温软件有用吗 编辑:程序博客网 时间:2024/05/21 18:37
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 thanminTimei and not more thanmaxTimei hours per eachi-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 hourssumTime spent him on preparation, and now he wants to know if he can show his parents a timetablesсhedule with d numbers, where each numbersсhedulei stands for the time in hours spent by Peter eachi-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of allschedulei should equal tosumTime.
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 numbersminTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in thei-th day.
Output
In the first line print YES, and in the second line printd 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 printNO in the unique line. If there are many solutions, print any of them.
Sample Input
1 485 7
NO
2 50 13 5
YES1 4
题意:
题目很简单,就是判断可不可以在给定的n个区间里且在每个区间找到一个数,使其和等于给定的数,有输出YES,并输出一组解,没有就直接输出NO。
难点:
如何在给定的n个区间里找到解。
注意问题:
1.输出空格的限制
2.求解的过程。思想是让给定的sum和最小和比较,然后从第一个区间一次向上加,达到区间最大值使输出最大值,并改变最小和,若没有达到区间最大值就直接输出该数,并把最小值和赋值为sum即可。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;
int main(){ int n,sum,a[1005][2]; int k,i,tag; int maxsum,minsum; while(scanf("%d%d",&n,&sum)!=EOF){ maxsum=0,minsum=0; for(i=0;i<n;i++){ scanf("%d%d",&a[i][0],&a[i][1]); maxsum+=a[i][1]; minsum+=a[i][0]; } if(sum>=minsum&&sum<=maxsum){//判断给定的sum在最大和与最小和之间 printf("YES\n"); tag=0; for(i=0;i<n;++i){ for(k=a[i][0];k<=a[i][1];k++){ if((minsum-a[i][0]+k)==sum){break;} } if(tag!=0){ printf(" ");//空格的输出 } if(k>a[i][1]){printf("%d",a[i][1]);minsum=a[i][1]+minsum-a[i][0];} else{ printf("%d",k); minsum=sum; } tag=1; } printf("\n"); } else{ printf("NO\n"); } }
return 0;}
- CodeFORCES 4B多组解中找到一个符合题意的解
- Codeforces 4A-Watermelon(题意)
- 找到符合条件的整数
- 找到符合条件的整数
- 查找某个数据,找到后把符合条件数据的一行复制到另外一个地方
- 【codeforces 676C】 【尺取法】【从一个ab序列中最多改变k个字符找到最长的连续a子串或者b子串长度】
- 在序列中找到符合某个条件的子序列
- 二分查找,找到第一个符合条件的数字
- 编程之美-找到符合条件的整数
- 编程之美之找到符合条件的数
- [刷题笔记] Codeforces 115B ,一个清楚的大脑
- 在一个整数数组中寻找符合A+B=C的组合,使C为最大
- 实现一个比较高级的字符匹配算法,即一串很长的字符,要求找到符合要求字符的字符串
- 实现一个比较高级的字符匹配算法,即一串很长的字符,要求找到符合要求字符的字符串
- 编写一个符合国情的日期插件
- 编写一个符合国情的日期插件
- 编写一个符合国情的日期插件
- 编写一个符合国情的日期插件
- 九度OJ 题目1131:合唱队形
- 基于cocos2dx的游戏开发环境搭建,以及android调试
- Apache反向代理
- 宏定义中#号的作用
- spring resourceLoader 读取配置文件
- CodeFORCES 4B多组解中找到一个符合题意的解
- 微信摇一摇公众号开发
- [转]xfermode讲解
- 集団的自衛権―存立危機とは何なのか
- Struts(18)标签
- redis事务中的WATCH命令和基于CAS的乐观锁
- 图像分割阈值选取技术综述
- mysql 批量更新
- 子类重写父类的方法应注意的问题