#598 (Div. 3)C. Platforms Jumping

题目描述

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.
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.

Input

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.
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.

Output

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, 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.
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.

Examples

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 [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,左岸为0,右岸为n+1。你想从左岸走到右岸。
河上有m块木板,第i块木板的长度为c[i]。你可以随意改变木板放置的位置,但木板必须在1-n的范围内,且木板之间不能重合。
你的一步可以走[1,d]的距离。你希望过河,但不希望踩到水里,所以你只能踩着木板过河。问:你能否踩着木板成功过河,如果可以,木板应该怎么安排。

题目分析

再放第i块木板时,先判断剩余的空间减去这一步的距离(假设正常的每一步都是走d的距离)是否够我们把余下的木板都放入河中。
如果够,则下一步的距离为d,然后放下第i块木板。
如果不够,那么减小一步的距离,计算这一步为多少时才能把剩余的木板全部放入河中,然后再放下木板。
再放木板时可以记录木板的位置,以此构建a[]。
假设我们铺完所有的木板之后所到达的位置是k,那么如果k+d>=n+1,则可以成功过河,否则不能过河。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=1e3+5;
int c[N],a[N];
int main()
{
	int n,m,d;
	cin>>n>>m>>d;
	int sum=0;			//记录当前剩余木板的总长度
	for(int i=1;i<=m;i++)
	{		
		cin>>c[i];
		sum+=c[i];
	}
	int k=0;				//记录当前走到的位置
	for(int i=1;i<=m;i++)	//放m个木板
	{						//n-k-d+1   总距离n-当前位置k-这一步走过的距离d-1=当前剩余空间
		if(n+1-k-d>=sum)	//当前剩余空间减去d够放下剩余的木板
		{
			k+=d;			//这一步距离为d
			for(int j=0;j<c[i];j++)	//放木板
				a[k+j]=i;
			k+=c[i]-1;		//从该木板这左侧走到右侧
		}
		else
		{
			int t;			//记录这一步的距离
			for(int j=d-1;j>=1;j--)		//看这一步应该迈多大
				if(n+1-k-j>=sum)
				{
					t=j; break;
				}
			k+=t;
			for(int j=0;j<c[i];j++)		//放木板
				a[k+j]=i;
			k+=c[i]-1;
		}
		sum-=c[i];			//剩余木板减去当前放上的木板长度
	}
	if(k+d>=n+1)			//判断是否能过河
	{
		puts("YES");
		for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	}
	else puts("NO");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值