AtCoder Weekday Contest 0008 Beta题解(AWC 0008 Beta A-E)
A - Planting the Flower Bed
【题目来源】
AtCoder:A - Planting the Flower Bed
【题目描述】
Takahashi has decided to make a flower bed in his garden. The flower bed is long and narrow, and he plans to plant \(N\) flowers in a row.
高桥决定在自家花园里建一个花坛。这个花坛狭长,他计划在一排中种植 \(N\) 株花。
The width of the flower bed is \(W\) centimeters, with the left end at position \(0\) cm and the right end at position \(W\) cm. Each flower can be considered as a point (its size is negligible). The \(1\)st flower is planted at the left end (position \(0\) cm), and the \(N\)th flower is planted at the right end (position \(W\) cm). The \(N\) flowers are placed at equal intervals so that the spacing between all adjacent flowers is the same. Specifically, the \(i\)th flower \((1 \leq i \leq N)\) is planted at position \(\frac{(i-1) \times W}{N-1}\) cm, and the spacing between any two adjacent flowers is \(\frac{W}{N-1}\) centimeters.
花坛的宽度为 \(W\) 厘米,左端位于位置 \(0\) 厘米,右端位于位置 \(W\) 厘米。每株花可视为一个点(其大小可忽略)。第 \(1\) 株花种植在左端(位置 \(0\) 厘米),第 \(N\) 株花种植在右端(位置 \(W\) 厘米)。\(N\) 株花以等间距布置,使得所有相邻花之间的间距相同。具体而言,第 \(i\) 株花(\(1 ≤ i ≤ N\))种植在位置 \((i-1) × W / (N-1)\) 厘米处,且任意两株相邻花之间的间距为 \(W / (N-1)\) 厘米。
For the flowers to grow healthily, the spacing between adjacent flowers must be sufficiently large. Specifically, the spacing between adjacent flowers must be at least \(K\) centimeters.
为了让花健康生长,相邻花之间的间距必须足够大。具体而言,相邻花之间的间距必须至少为 \(K\) 厘米。
Takahashi wants to check whether this equal-interval arrangement satisfies the condition. If the condition is satisfied, output Yes; otherwise, output No.
高桥想检查这种等间距布置是否满足条件。如果条件满足,输出 Yes;否则,输出 No。
【输入】
\(N\) \(W\) \(K\)
The first line contains three space-separated integers: \(N\) representing the number of flowers, \(W\) representing the width of the flower bed (in centimeters), and \(K\) representing the required minimum spacing (in centimeters).
【输出】
If the spacing between adjacent flowers in the equal-interval arrangement is at least \(K\) centimeters, output Yes; otherwise, output No on a single line.
【输入样例】
5 100 20
【输出样例】
Yes
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
int n, w, k; // n: 花的总数,w: 水的总量,k: 每朵花需要的最小水量
int main()
{
cin >> n >> w >> k; // 读入花的总数、水的总量和每朵花需要的最小水量
// 判断条件:将水均匀分配给除了第一朵花以外的其他花
// w/(n-1) 表示其他每朵花能得到的水量
// 如果这个水量 >= 每朵花需要的最小水量k,则满足条件
if (w / (n - 1) >= k)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}
【运行结果】
5 100 20
Yes
B - Buildings Visible from the Sea
【题目来源】
AtCoder:B - Buildings Visible from the Sea
【题目描述】
Takahashi is visiting a town along the coast.
高桥正在游览一个沿海小镇。
There are \(N\) buildings lined up in a straight line from the sea toward the land. The buildings are numbered \(1, 2, \ldots, N\) in order from the closest to the sea, where building \(1\) is closest to the sea and building \(N\) is farthest from the sea. The height of building \(i\) is \(H_i\). All buildings have the same width and are lined up in a straight line with no gaps between them.
有 \(N\) 栋建筑物从海边向内陆沿直线排列。建筑物按从海边最近的顺序编号为 \(1, 2, …, N\),其中建筑 \(1\) 离海最近,建筑 \(N\) 离海最远。建筑 \(i\) 的高度为 \(H_i\)。所有建筑具有相同的宽度,并沿直线紧密排列,彼此之间没有间隙。
Takahashi is viewing this row of buildings head-on from a sufficiently distant point on the sea. A building that can be seen without being blocked by buildings in front of it is called a building "visible from the sea," defined as follows:
高桥从海面上一个足够远的点正视这排建筑物。一栋不被其前方的建筑物遮挡而能看到的建筑被称为"从海上可见的建筑",定义如下:
- Building \(1\) is always visible from the sea.
建筑 \(1\) 总是从海上可见的。 - For \(i \geq 2\), let \(M\) be the maximum height among buildings \(1, 2, \ldots, i-1\). If \(H_i > M\), then building \(i\) is visible from the sea. If \(H_i \leq M\), then building \(i\) is not visible from the sea.
对于 \(i ≥ 2\),令 M 为建筑 \(1, 2, …, i-1\) 中的最大高度。如果 \(H_i > M\),则建筑 \(i\) 从海上可见。如果 \(H_i ≤ M\),则建筑 \(i\) 从海上不可见。
In other words, building \(i\) is visible from the sea if and only if it is strictly taller than all buildings in front of it (on the sea side).
换言之,建筑 \(i\) 从海上可见当且仅当其严格高于其前方(靠海一侧)的所有建筑。
Find the number of buildings visible from the sea.
求从海上可见的建筑数量。
【输入】
\(N\)
\(H_1\) \(H_2\) \(\ldots\) \(H_N\)
- The first line gives the number of buildings \(N\).
- The second line gives the heights of each building \(H_1, H_2, \ldots, H_N\), separated by spaces.
【输出】
Print the number of buildings visible from the sea in one line.
【输入样例】
5
3 1 4 1 5
【输出样例】
3
【解题思路】

【代码详解】
#include <bits/stdc++.h>
using namespace std;
int n, h, ans = 1; // n: 数字个数,h: 当前最高值,ans: 计数(初始为1表示第一个数字)
int main()
{
cin >> n >> h; // 读入数字个数和第一个数字(作为初始最高值)
for (int i = 2; i <= n; i++) // 从第二个数字开始遍历
{
int x;
cin >> x; // 读入当前数字
if (x > h) // 如果当前数字大于当前最高值
{
ans++; // 计数器加1
h = x; // 更新当前最高值为当前数字
}
}
cout << ans << endl; // 输出计数结果
return 0;
}
【运行结果】
5
3 1 4 1 5
3
浙公网安备 33010602011771号