C. Platforms Jumping(Codeforces Round #598 (Div. 3))(模拟)
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Description
There is a river of width n. The left bank of the river is cell and the right bank is cell (more formally, the river can be represented as a sequence of cells numbered from to ). There are also wooden platforms on a river, the -th platform has length (so the -th platform takes consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed .
You are standing at and want to reach somehow. If you are standing at the position , you can jump to any position in the range . 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 , you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells and belong to wooden platforms.
You want to know if it is possible to reach from 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
and
, then one of the ways to reach
from
is follow:
Input
The first line of the input contains three integers , and — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains integers , where is the length of the -th platform.
Output
If it is impossible to reach
from
, print NO
in the first line. Otherwise, print YES
in the first line and the array a of length
in the second line — the sequence of river cells (excluding cell
and cell
).
If the cell does not belong to any platform, should be . Otherwise, it should be equal to the index of the platform ( -indexed, platforms are numbered from to in order of input) to which the cell belongs.
Note that all equal to should form a contiguous subsegment of the array of length , all equal to should form a contiguous subsegment of the array of length , …, all equal to should form a contiguous subsegment of the array of length . The leftmost position of in should be greater than the rightmost position of , the leftmost position of in should be greater than the rightmost position of , …, the leftmost position of in should be greater than the rightmost position of .
See example outputs for better understanding.
input
7 3 2
1 2 1
output
YES
0 1 0 2 2 0 3
input
10 1 11
1
output
YES
0 0 0 0 0 0 0 0 0 1
input
10 1 5
2
output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is . The sequence of jumps you perform is .
Consider the second example: it does not matter how to place the platform because you always can jump from to .
Consider the third example: the answer is . The sequence of jumps you perform is .
题解
大模拟
如果模板稀缺的话就贪心地把模板往后放;若木板不稀缺,就考虑保证所有木板都有空间放的情况下尽量往后放;若木板数量和剩余的空间数一样的话就直接挨着铺。
代码
#include <iostream>
#include <algorithm>
#include <vector>
#define _for(i, a) for(int i = 0; i < (a); i++)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const LL MAXN = 110;
const LL inf = 0x3f3f3f3f;
LL n, m, d;
LL sum = 0; //木板长度总数
LL b[1005];
vector<LL> c;
void init() {
mem(b, 0);
c.clear();
}
void sol() {
LL _sum = n; //剩余的空间
LL pos = -1;
LL t = 0; //木板序号
while (_sum && t < m) {
LL tem = min(_sum - sum + 1, d); //本回合能移动的最大距离
pos += tem; //移动人物的位置到木板最左边
_for(i, c[t]) b[pos + i] = t + 1; //记录木板序号
_sum = _sum - tem - c[t] + 1; //改变剩余的空间数量
sum -= c[t]; //改变剩余的木板长度
pos += c[t] - 1; //再改变任务的位置到木板最右边
t++;
}
if (pos + d < n) {
cout << "NO\n";
return;
}
cout << "YES\n";
_for(i, n) {
cout << b[i] << (i == n - 1 ? "\n" : " ");
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//freopen("in.txt", "r", stdin);
while (cin >> n >> m >> d) {
init();
_for(i, m) {
LL x;
cin >> x;
sum += x;
c.push_back(x);
}
sol();
}
return 0;
}