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 ci (so the i-th platform takes ci consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed n.c++
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.ide
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.spa
Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).code
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:orm
The first example: n=7.htm
The first line of the input contains three integers n, m and d (1≤n,m,d≤1000,m≤n) — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.three
The second line of the input contains m integers c1,c2,…,cm (1≤ci≤n,∑i=1mci≤n), where ci is the length of the i-th platform.ci
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).input
If the cell i does not belong to any platform, ai 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.it
Note that all ai equal to 1 should form a contiguous subsegment of the array a of length c1, all ai equal to 2 should form a contiguous subsegment of the array a of length c2, ..., all ai equal to m should form a contiguous subsegment of the array a of length cm. 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.
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
Consider the first example: the answer is [0,1,0,2,2,0,3]. The sequence of jumps you perform is 0→2→4→5→7→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→5→6→11.
如今有長度爲n的河,河上面你須要擺放m個木板,第i個木板的長度爲c[i],木板與木板之間的擺放不能交叉,如今問你是否存在一個方案,可以拜訪這些木板。
貪心,咱們假設全部木板都在最後面擺放。而後我開始跳躍,若是我腳下沒有木板,我就從後面拿一塊木板放在腳下便可。
dp,dp[i][j]表示我如今在i位置,是站在第j塊木板的開始,而後開始轉移便可。
#include<bits/stdc++.h> using namespace std; const int maxn = 1005; int n,m,d; int c[maxn]; int dp[maxn][maxn]; int from[maxn][maxn]; int plan[maxn]; void print_plan(){ //plan[n]=m; int now=n; int j=m; while(j>0){ for(int i=from[now][j];i<from[now][j]+c[j-1];i++){ plan[i]=j-1; } now=from[now][j]; j--; } for(int i=1;i<n;i++){ cout<<plan[i]<<" "; } cout<<endl; } int main(){ scanf("%d%d%d",&n,&m,&d); for(int i=1;i<=m;i++){ scanf("%d",&c[i]); } c[0]=1; n++;m++;c[m]=1; dp[0][0]=1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ for(int k=1;k<=d;k++){ if(i-k-c[j-1]+1>=0&&dp[i-k-c[j-1]+1][j-1]){ dp[i][j]=1; from[i][j]=i-k-c[j-1]+1; } } } } if(dp[n][m]==1){ puts("YES"); print_plan(); }else{ puts("NO"); } }