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

A - Total Shopping Amount

【题目来源】

AtCoder:A - Total Shopping Amount

【题目描述】

Takahashi is shopping at a supermarket.
高桥正在超市购物。

Takahashi has placed \(K\) items in his shopping basket. Each item has a price tag, and the price of the \(i\)-th item \((1 \leq i \leq K)\) is \(L_i\) yen.
高桥已在购物篮中放置了 \(K\) 件商品。每件商品都有一个价格标签,第 \(i\) 件商品(\(1 ≤ i ≤ K\))的价格为 \(L_i\) 日元。

This supermarket is running a campaign where, at checkout, customers receive points equal to the remainder when the total amount is divided by a positive integer \(M\).
这家超市正在开展一项促销活动:在结账时,顾客获得的积分等于总金额除以一个正整数 \(M\) 所得的余数。

That is, if the total sum of all item prices is \(S = L_1 + L_2 + \cdots + L_K\), the points Takahashi receives are the remainder when \(S\) is divided by \(M\). Note that if \(S\) is a multiple of \(M\), the points received are \(0\).
也就是说,如果所有商品价格的总和为 \(S = L_1 + L_2 + ⋯ + L_K\),高桥获得的积分是 \(S\) 除以 \(M\) 的余数。注意,如果 \(S\)\(M\) 的倍数,则获得的积分为 \(0\)

Find the number of points Takahashi will receive.
求高桥将获得的积分数量。

【输入】

\(K\) \(M\)
\(L_1\) \(L_2\) \(\cdots\) \(L_K\)

  • The first line contains an integer \(K\) representing the number of items in the shopping basket, and a positive integer \(M\) used for the point calculation, separated by a space.
  • The second line contains \(K\) integers \(L_1, L_2, \ldots, L_K\) representing the prices of each item, separated by spaces.

【输出】

Print the number of points Takahashi will receive as an integer on a single line.

【输入样例】

3 100
150 80 45

【输出样例】

75

【解题思路】

image

【代码详解】

#include <bits/stdc++.h>
using namespace std;
int k, m, ans;  // k: 数字个数,m: 模数,ans: 总和

int main()
{
    cin >> k >> m;  // 读入数字个数k和模数m
    
    for (int i = 1; i <= k; i++)  // 循环读取k个数字
    {
        int x;
        cin >> x;  // 读入第i个数字
        ans += x;  // 累加到总和
    }
    
    cout << ans % m << endl;  // 输出总和除以m的余数
    return 0;
}

【运行结果】

3 100
150 80 45
75

B - Dungeon Exploration

【题目来源】

AtCoder:B - Dungeon Exploration

【题目描述】

Takahashi challenges himself to explore a dungeon.
高桥挑战自己去探索一个地下城。

The dungeon has \(N\) rooms arranged in a row. The \(i\)-th room from the left contains \(1\) monster. The \(i\)-th monster has strength \(H_i\), and upon being defeated, it drops an item that restores \(P_i\) health points.
地下城有 \(N\) 个房间排成一行。从左数第 \(i\) 个房间内有 \(1\) 只怪物。第 \(i\) 只怪物的力量为 \(H_i\),被击败后会掉落一个恢复 \(P_i\) 点生命值的物品。

Takahashi visits all rooms exactly once, from room \(1\) to room \(N\) in order of increasing room number. Takahashi's initial health is \(S\). There is no upper limit on health.
高桥按房间编号递增的顺序访问所有房间各一次,从房间 \(1\) 到房间 \(N\)。高桥的初始生命值为 \(S\)。生命值没有上限。

When visiting each room \(i\), the following rules are automatically applied. Takahashi has no choice in the matter.
当访问每个房间 \(i\) 时,将自动应用以下规则。高桥对此没有选择权。

  • If his current health is greater than or equal to \(H_i\), Takahashi defeats the monster. First, his health decreases by \(H_i\), and immediately after, it recovers by \(P_i\). That is, the net change in health is \(-H_i + P_i\). Since his health was at least \(H_i\) just before the battle, it is guaranteed that his health is non-negative immediately after the \(H_i\) decrease.
    如果他的当前生命值大于或等于 \(H_i\),高桥会击败怪物。首先,他的生命值减少 \(H_i\),随后立即恢复 \(P_i\)。也就是说,生命值的净变化为 \(-H_i + P_i\)。由于战斗前他的生命值至少为 \(H_i\),保证在减少 \(H_i\) 后其生命值非负。
  • If his current health is less than \(H_i\), Takahashi cannot defeat the monster and passes through the room. In this case, his health does not change.
    如果他的当前生命值小于 \(H_i\),高桥无法击败怪物并会穿过房间。这种情况下,他的生命值不变。

For each room where a monster was not defeated, Takahashi must pay a penalty of \(C\).
对于每个未击败怪物的房间,高桥必须支付 \(C\) 的惩罚。

After visiting all \(N\) rooms, determine the total penalty Takahashi must pay. That is, if the number of rooms where the monster was not defeated is \(k\), output \(k \times C\).
访问完所有 \(N\) 个房间后,确定高桥必须支付的总惩罚。也就是说,如果未击败怪物的房间数为 \(k\),则输出 \(k × C\)

【输入】

\(N\) \(S\) \(C\)
\(H_1\) \(P_1\)
\(H_2\) \(P_2\)
\(\vdots\)
\(H_N\) \(P_N\)

  • The first line contains \(N\) representing the number of rooms, \(S\) representing Takahashi's initial health, and \(C\) representing the penalty per room, separated by spaces.
  • From the 2nd line to the \((N + 1)\)-th line, information about each room is given over \(N\) lines.
  • The \((1 + i)\)-th line contains the strength \(H_i\) of the \(i\)-th monster and the health recovery amount \(P_i\) upon defeating that monster, separated by spaces.

【输出】

Output the total penalty Takahashi must pay, on a single line.

【输入样例】

3 10 5
3 1
12 5
5 3

【输出样例】

5

【解题思路】

image

【代码详解】

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s, c, ans;  // n: 物品数量,s: 初始容量/力量,c: 惩罚代价,ans: 总代价

signed main()
{
    cin >> n >> s >> c;  // 读入物品数量、初始容量/力量和惩罚代价
    
    for (int i = 1; i <= n; i++)  // 处理每个物品
    {
        int h, p;  // h: 需要消耗的容量/力量,p: 击败后获得的收益
        cin >> h >> p;  // 读入当前物品的消耗和收益
        
        if (s < h)  // 如果当前容量/力量不足以应对消耗
        {
            ans += c;  // 增加惩罚代价
        }
        else
        {
            s = s - h + p;  // 消耗h,获得p,更新当前容量/力量
        }
    }
    
    cout << ans << endl;  // 输出总代价
    return 0;
}

【运行结果】

3 10 5
3 1
12 5
5 3
5

C - Apple Harvest

【题目来源】

AtCoder:C - Apple Harvest

【题目描述】

Takahashi is working a part-time job harvesting apples at an orchard. The orchard has \(N\) apples, each located at a different height on the tree.
高桥正在一家果园做采摘苹果的兼职工作。果园有 \(N\) 个苹果,每个位于树上的不同高度。

The \(i\)-th apple is at a height of \(H_i\) centimeters from the ground. Takahashi's height is \(T\) centimeters, but by stretching on his toes, he can reach up to a maximum height of \(T + K\) centimeters.
\(i\) 个苹果距离地面的高度为 \(H_i\) 厘米。高桥的身高是 \(T\) 厘米,但通过踮起脚尖,他能够到的最大高度为 \(T + K\) 厘米。

To harvest an apple, the apple's height must be within his reach (at most \(T + K\) centimeters). For apples he cannot reach, he would need to use a stepladder.
要收获一个苹果,该苹果的高度必须在他的可触及范围内(最多 \(T + K\) 厘米)。对于他无法够到的苹果,他需要使用梯凳。

Since Takahashi finds using a stepladder troublesome, he wants to maximize the number of apples he can harvest by stretching alone. Fortunately, the trees in this orchard are planted in special mobile planters, and he can dig a hole of depth \(D\) centimeters in the ground to sink an entire planter (\(D\) is any non-negative integer). When a tree is sunk, the heights of all its apples are uniformly reduced by \(D\).
由于高桥觉得使用梯凳很麻烦,他希望最大化仅通过踮脚就能收获的苹果数量。幸运的是,这个果园的树都种在特殊的可移动种植盆中,他可以在地面上挖一个深度为 \(D\) 厘米的坑来降低整个种植盆(\(D\) 可以是任意非负整数)。当一棵树被降低时,其所有苹果的高度都会统一减少 \(D\)

However, there is a constraint that the height of the lowest apple must not become less than \(1\) centimeter. In other words, for all \(i\), the condition \(H_i - D \geq 1\) must be satisfied.
然而,有一个约束条件:最低苹果的高度不得低于 \(1\) 厘米。换言之,对于所有 \(i\),必须满足条件 \(H_i - D ≥ 1\)

Find the maximum number of apples Takahashi can harvest by stretching alone.
求高桥仅通过踮脚就能收获的最大苹果数量。

【输入】

\(N\) \(T\) \(K\)
\(H_1\) \(H_2\) \(\ldots\) \(H_N\)

  • The first line contains the number of apples \(N\), Takahashi's height \(T\), and the additional height \(K\) he can reach by stretching, separated by spaces.
  • The second line contains the heights of each apple \(H_1, H_2, \ldots, H_N\), separated by spaces.

【输出】

Print the maximum number of apples Takahashi can harvest by stretching alone, in a single line.

【输入样例】

5 150 30
100 160 180 200 250

【输出样例】

5

【解题思路】

image

【代码详解】

#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, t, k, minn = 1e9;  // n: 数量,t: 基础值,k: 附加值,minn: 最小值
int h[N], ans;  // h: 高度数组,ans: 符合条件的数量

int main()
{
    cin >> n >> t >> k;  // 读入数量、基础值和附加值
    for (int i = 1; i <= n; i++)
    {
        cin >> h[i];  // 读入每个高度
        minn = min(minn, h[i]);  // 更新最小值
    }
    
    int D = minn - 1;  // 计算偏移量D = 最小值-1
    
    for (int i = 1; i <= n; i++)
    {
        // 判断条件:t+k ≥ (h[i]-D)
        if (t + k >= h[i] - D)
        {
            ans++;  // 计数加1
        }
    }
    
    cout << ans << endl;  // 输出结果
    return 0;
}

【运行结果】

5 150 30
100 160 180 200 250
5
posted @ 2026-03-03 11:28  团爸讲算法  阅读(2)  评论(0)    收藏  举报