加载中...

CF题解|Before an Exam


Codeforces题解|4B - Before an Exam


题目信息 📚

【题目描述】

Tomorrow Peter has a Biology exam. He does not like this subject much, but $d$ days ago he learned 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 $\text{minTime}_i$ and not more than $\text{maxTime}_i$ 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 $\text{sumTime}$ spent on preparation, and now he wants to know if he can show his parents a timetable $\text{schedule}$ with $d$ numbers, where each number $\text{schedule}_i$ 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 $\text{schedule}_i$ should equal to $\text{sumTime}$.

【输入】

The first input line contains two integer numbers $d, \text{sumTime}$ $(1 \leq d \leq 30, 0 \leq \text{sumTime} \leq 240)$ — the number 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 $\text{minTime}_i, \text{maxTime}_i$ $(0 \leq \text{minTime}_i \leq \text{maxTime}_i \leq 8)$, separated by a space — the minimum and maximum amount of hours that Peter could spend on 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 — the 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】

1 48
5 7

【输出样例1】

NO

【输入样例2】

2 5
0 1
3 5

【输出样例2】

YES
1 4 

【题目来源】

https://codeforces.com/problemset/problem/4/B


题目解析 🍉

【题目分析】

思维 + 构造。

【C++代码】✅

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 50;
int d, sum, s_min, s_max, l[N], r[N];

int main() {
    ios::sync_with_stdio(false);  //cin读入优化
    cin.tie(0);

    // 读入数据,计算最小复习时间s_min和最大复习时间s_max
    cin >> d >> sum;
    for (int i = 1; i <= d; i++) {
        cin >> l[i] >> r[i];
        s_min += l[i];
        s_max += r[i];
    }

    // 符合范围
    if (sum >= s_min && sum <= s_max) {
        cout << "YES" << endl;
        // 将差值进行分配
        int dis = sum - s_min;
        for (int i = 1; i <= d; i++) {
            int t = min(dis + l[i], r[i]);
            dis = dis - (t - l[i]);
            cout << t << " ";
        }
        cout << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}

文章作者: Rickyの水果摊
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Rickyの水果摊 !
  目录