AtCoder Weekday Contest 0022 Beta题解(AWC 0022 Beta A-E)

A - Battery Level and Charger Arrival

【题目来源】

AtCoder:A - Battery Level and Charger Arrival (atcoder.jp)

【题目描述】

Takahashi manages \(N\) smartphones. The current battery level of the \(i\)-th smartphone is \(W_i\) percent.
高桥管理着 \(N\) 部智能手机。第 \(i\) 部手机的当前电量为 \(W_i\) 百分比。

The battery of a smartphone decreases by \(D\) percentage points per day if not charged.
如果不充电,手机的电池电量每天下降 \(D\) 个百分点。

Takahashi is considering ordering from an online charger store. If he places an order, the charger will arrive in exactly \(K\) days. Since Takahashi currently has no charger, he cannot charge any smartphone during the \(K\) days until it arrives.
高桥正在考虑从一家在线充电器商店下单。如果他下单,充电器将在恰好 \(K\) 天后送达。由于高桥目前没有充电器,在充电器送达前的 \(K\) 天内,他无法为任何手机充电。

After \(K\) days, when the charger arrives, the battery level of the \(i\)-th smartphone is considered to be \(W_i - D \times K\) percent. A smartphone with this value being \(1\) or more is still usable, while a smartphone with this value less than \(1\) has powered off and become unusable.
\(K\) 天后,当充电器送达时,第 \(i\) 部手机的电量被视为 \(W_i - D \times K\) 百分比。此值大于等于 \(1\) 的手机仍可使用,而此值小于 \(1\) 的手机将关机并无法使用。

Determine the number of smartphones among the \(N\) that are still usable after \(K\) days.
确定 \(K\) 天后仍可使用的手机数量。

【输入】

\(N\) \(D\) \(K\)
\(W_1\) \(W_2\) \(\ldots\) \(W_N\)

  • The first line contains the integer \(N\) representing the number of smartphones, the integer \(D\) representing the daily battery decrease, and the integer \(K\) representing the number of days until the charger arrives, separated by spaces.
  • The second line contains the integers \(W_1, W_2, \ldots, W_N\) representing the current battery levels of each smartphone, separated by spaces.

【输出】

Output in one line the number of smartphones that are still usable after \(K\) days.

【输入样例】

3 10 5
100 50 30

【输出样例】

1

【解题思路】

image

【代码详解】

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, d, k, ans;  // n: 总数量,d: 每天消耗量,k: 天数,ans: 结果计数

signed main()
{
    cin >> n >> d >> k;  // 读入n, d, k
  
    for (int i = 1; i <= n; i++)  // 遍历每个物品
    {
        int w; 
        cin >> w;  // 读入当前物品的初始重量
        w = w - d * k;  // 计算经过k天后的剩余重量
      
        if (w >= 1)  // 如果剩余重量大于等于1
        {
            ans++;  // 计数加1
        }
    }
  
    cout << ans << endl;  // 输出满足条件的物品数量
    return 0;
}

【运行结果】

3 10 5
100 50 30
1

B - Target Score for the Test

【题目来源】

AtCoder:B - Target Score for the Test

【题目描述】

Takahashi took final exams in \(N\) subjects. In each subject \(i\) (\(1 \leq i \leq N\)), Takahashi scored \(A_i\) points.
高桥参加了 \(N\) 门科目的期末考试。在每门科目 \(i\)\(1 \leq i \leq N\))中,高桥获得了 \(A_i\) 分。

At Takahashi's school, if there is even one subject where the score is below the target score of \(T\) points, the student must attend supplementary lessons.
在高桥的学校,如果有任何一门科目的分数低于目标分数 \(T\) 分,该学生就必须参加补习课。

Fortunately, this school has an "extra assignment system." For each extra assignment submitted, Takahashi can choose any one subject and increase that subject's score by 1 point. The same subject may be chosen multiple times. There is no upper limit on the score for each subject. However, the total number of extra assignments that can be submitted is at most \(M\). It is also allowed to submit no extra assignments at all.
幸运的是,这所学校有一个"额外作业系统"。每提交一次额外作业,高桥可以选择任意一门科目,并将该科目的分数提高 1 分。同一门科目可以被多次选择。每门科目的分数没有上限。但是,可以提交的额外作业总数最多为 \(M\)。也允许完全不提交额外作业。

Takahashi wants to avoid supplementary lessons by strategically submitting extra assignments so that all subjects' scores become at least \(T\) points.
高桥希望通过策略性地提交额外作业,使所有科目的分数都至少达到 \(T\) 分,从而避免参加补习课。

Determine whether it is possible to achieve a score of at least \(T\) in all subjects by submitting a total of at most \(M\) extra assignments. If it is possible, output the minimum number of extra assignments needed. If all subjects already have scores of at least \(T\), output \(0\). If it is impossible, output -1.
判断通过提交总计不超过 \(M\) 份额外作业,是否能使所有科目的分数至少达到 \(T\) 分。如果可能,输出所需的最少额外作业数量。如果所有科目的分数已经至少达到 \(T\) 分,输出 \(0\)。如果不可能,输出 -1

【输入】

\(N\) \(M\) \(T\)
\(A_1\) \(A_2\) \(\ldots\) \(A_N\)

  • The first line contains three space-separated integers: \(N\) representing the number of subjects, \(M\) representing the maximum number of extra assignments that can be submitted, and \(T\) representing the target score.
  • The second line contains space-separated integers \(A_1, A_2, \ldots, A_N\) representing the scores Takahashi obtained in each subject.

【输出】

If it is possible to achieve a score of at least \(T\) in all subjects, output the minimum number of extra assignments needed on a single line. If it is impossible, output -1 on a single line.

【输入样例】

3 5 60
55 70 58

【输出样例】

-1

【解题思路】

image

【代码详解】

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, t, tot;  // n: 数量,m: 预算,t: 目标值,tot: 总需要量

signed main()
{
    cin >> n >> m >> t;  // 读入数量、预算、目标值
    
    for (int i = 1; i <= n; i++)  // 遍历每个物品
    {
        int x; 
        cin >> x;  // 读入当前物品的值
        
        if (x < t)  // 如果当前物品的值小于目标值
        {
            tot += (t - x);  // 计算需要增加的量,并累加到总需要量
        }
    }
    
    if (tot > m)  // 如果总需要量超过预算
    {
        cout << -1 << endl;  // 输出-1表示无法满足
    }
    else  // 如果预算足够
    {
        cout << tot << endl;  // 输出总需要量
    }
    
    return 0;
}

【运行结果】

3 5 60
55 70 58
-1
posted @ 2026-03-15 12:36  团爸讲算法  阅读(2)  评论(0)    收藏  举报