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

Input
1 485 7
Output
NO
Input
2 50 13 5
Output
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;}
0 0
原创粉丝点击