Codeforces 4B多组解中找到一个符合题意的解



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 48
5 7
Output
NO
Input
2 5
0 1
3 5
Output
YES
1 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以分享一种有效的训练计划,以达到codeforces橙名。首先,建立一个长期的训练计划,每周至少完成3-4个题目,把每个题目的题过程都认真的思考一遍,确保理每一步的思路。建议每隔一段时间,做一次综合练习,以更好地检验自己的知识水平。此外,参加竞赛也可以加深对算法的理,从而提升题能力。 ### 回答2: 想要在Codeforces达到橙名的水平,需要进行系统性的训练,并掌握一定的算法和编程技巧。以下是一个可能的训练计划: 1. 学习基础知识:首先,建议你系统地学习计算机科学的基础知识,包括数据结构和算法。这可以帮助你理不同问题的决方案,并优化代码的执行效率。 2. 决题目:开始刷题是锻炼算法和编码能力的关键。选择一些简单的题目,如Codeforces的Div2 A、B级题目,按难度逐渐增加。通过不断决题目,你将熟悉各种算法,并提高编写清晰、高效代码的能力。 3. 学习算法:学习和掌握一些常用算法,如贪心、动态规划、图论等。理算法原理,分析其时间和空间复杂度,并通过练习将其应用于具体的问题。 4. 参加比赛:参加Codeforces的比赛是衡量自己水平的好方法。开始参加Div2级别的比赛,并争取在中等难度的题目上取得好成绩。逐渐挑战3级、4级比赛,尽量与更强的选手竞争,以提高自己的水平。 5. 反思和学习:每次比赛后,对自己的题过程进行反思和总结。分析错的原因,学习其他参赛选手的思考方式和技巧。通过学习他人的优秀代码和题思路,不断提升自己的编码能力。 6. 练习编码技巧:除了算法,熟练掌握编码技巧也非常重要。学习并应用一些常用的数据结构和STL库,如数组、链表、栈、队列等。多写一些小项目或练习题,锻炼自己的编码能力。 7. 时间管理:合理安排学习和练习的时间。每天保持一定时间的刷题和学习,坚持养成好的学习习惯。同时,在比赛中也要控制好时间,并尽量在限定时间内完成题目。 总之,达到Codeforces橙名的水平需要长期的训练和不断的学习。通过刷题、学习算法和编码技巧、参加比赛以及反思总结,你可以逐渐提升自己的水平,并取得理想的成绩。记住,坚持和持续学习是达到目标的关键。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值