A12989 | Platforms Jumping
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a river of width $n$ . The left bank of the river is cell $0$ and the right bank is cell $n + 1$ (more formally, the river can be represented as a sequence of $n + 2$ cells numbered from $0$ to $n + 1$ ). There are also $m$ wooden platforms on a river, the $i$ -th platform has length $c_i$ (so the $i$ -th platform takes $c_i$ consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed $n$ .
You are standing at $0$ and want to reach $n+1$ somehow. If you are standing at the position $x$ , you can jump to any position in the range $[x + 1; x + d]$ . However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if $d=1$ , you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells $0$ and $n+1$ belong to wooden platforms.
You want to know if it is possible to reach $n+1$ from $0$ if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if $n=7$ , $m=3$ , $d=2$ and $c = [1, 2, 1]$ , then one of the ways to reach $8$ from $0$ is follow:
The first example: $n=7$ .
You are standing at $0$ and want to reach $n+1$ somehow. If you are standing at the position $x$ , you can jump to any position in the range $[x + 1; x + d]$ . However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if $d=1$ , you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells $0$ and $n+1$ belong to wooden platforms.
You want to know if it is possible to reach $n+1$ from $0$ if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).
For example, if $n=7$ , $m=3$ , $d=2$ and $c = [1, 2, 1]$ , then one of the ways to reach $8$ from $0$ is follow:
The first example: $n=7$ .
输入格式
The first line of the input contains three integers $n$ , $m$ and $d$ ( $1 \le n, m, d \le 1000, m \le n$ ) — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains $m$ integers $c_1, c_2, \dots, c_m$ ( $1 \le c_i \le n, \sum\limits_{i=1}^{m} c_i \le n$ ), where $c_i$ is the length of the $i$ -th platform.
The second line of the input contains $m$ integers $c_1, c_2, \dots, c_m$ ( $1 \le c_i \le n, \sum\limits_{i=1}^{m} c_i \le n$ ), where $c_i$ is the length of the $i$ -th platform.
输出格式
If it is impossible to reach $n+1$ from $0$ , print NO in the first line. Otherwise, print YES in the first line and the array $a$ of length $n$ in the second line — the sequence of river cells (excluding cell $0$ and cell $n + 1$ ).
If the cell $i$ does not belong to any platform, $a_i$ should be $0$ . Otherwise, it should be equal to the index of the platform ( $1$ -indexed, platforms are numbered from $1$ to $m$ in order of input) to which the cell $i$ belongs.
Note that all $a_i$ equal to $1$ should form a contiguous subsegment of the array $a$ of length $c_1$ , all $a_i$ equal to $2$ should form a contiguous subsegment of the array $a$ of length $c_2$ , ..., all $a_i$ equal to $m$ should form a contiguous subsegment of the array $a$ of length $c_m$ . The leftmost position of $2$ in $a$ should be greater than the rightmost position of $1$ , the leftmost position of $3$ in $a$ should be greater than the rightmost position of $2$ , ..., the leftmost position of $m$ in $a$ should be greater than the rightmost position of $m-1$ .
See example outputs for better understanding.
If the cell $i$ does not belong to any platform, $a_i$ should be $0$ . Otherwise, it should be equal to the index of the platform ( $1$ -indexed, platforms are numbered from $1$ to $m$ in order of input) to which the cell $i$ belongs.
Note that all $a_i$ equal to $1$ should form a contiguous subsegment of the array $a$ of length $c_1$ , all $a_i$ equal to $2$ should form a contiguous subsegment of the array $a$ of length $c_2$ , ..., all $a_i$ equal to $m$ should form a contiguous subsegment of the array $a$ of length $c_m$ . The leftmost position of $2$ in $a$ should be greater than the rightmost position of $1$ , the leftmost position of $3$ in $a$ should be greater than the rightmost position of $2$ , ..., the leftmost position of $m$ in $a$ should be greater than the rightmost position of $m-1$ .
See example outputs for better understanding.
输入输出样例
输入 #1
7 3 2 1 2 1
输出 #1
YES 0 1 0 2 2 0 3
输入 #2
10 1 11 1
输出 #2
YES 0 0 0 0 0 0 0 0 0 1
输入 #3
10 1 5 2
输出 #3
YES 0 0 0 0 1 1 0 0 0 0
Consider the first example: the answer is $[0, 1, 0, 2, 2, 0, 3]$ . The sequence of jumps you perform is $0 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 7 \rightarrow 8$ .
Consider the second example: it does not matter how to place the platform because you always can jump from $0$ to $11$ .
Consider the third example: the answer is $[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]$ . The sequence of jumps you perform is $0 \rightarrow 5 \rightarrow 6 \rightarrow 11$ .
Consider the second example: it does not matter how to place the platform because you always can jump from $0$ to $11$ .
Consider the third example: the answer is $[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]$ . The sequence of jumps you perform is $0 \rightarrow 5 \rightarrow 6 \rightarrow 11$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted