其他分享
首页 > 其他分享> > 贪心(跳板)

贪心(跳板)

作者:互联网

CodeForces - 1256C

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.

 题意:问从0能到n+1,这之间有m块跳板,每次最多跳距离d。问能否到n+1,如果能输出1到n放哪块板子的下标,不放就为0;

解法:贪心先判断能否到n+1,如何根据空隙贪心放板子。

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 1e7 + 5 ;
int a[1009];
int vis[1009];

int main()
{
    int n , m , d ;
    scanf("%d%d%d" , &n , &m , &d);
    int p = 0 , cnt = 0 ;
    memset(vis , 0 , sizeof(vis));
    for(int i = 1 ; i <= m  ; i++)
    {
        scanf("%d" , &a[i]);
        cnt += a[i];
    }
    int l = 0 ;
    for(int i = 1 ; i <= m ; i++)
    {
        l += d + a[i] - 1 ;
    }
    l += d ;
    if(l >= n + 1)
    {
        printf("YES\n");
        int r = n - cnt ;//空隙
        int s = 0 ;
        for(int i = 1 ; i <= m ; i++)
        {
            int t = min(r , d - 1);//可行空隙
            r -= t ;//剩余空隙
            s += t + 1 ;//放木板的位置
            vis[s] = i ;//标记该位置放index板
            s += a[i] - 1 ;//加上木板长度
        }
        for(int i = 1 ; i <= n ;)
        {
            if(vis[i])
            {
                int j ;
                for(j = i ; j < i + a[vis[i]] ; j++)
                {
                    vis[j] = vis[i] ;
                }
                i += a[vis[i]] ;
            }
            else
            {
                i++ ;
            }
        }
        for(int i = 1 ; i < n ; i++)
        {
            cout << vis[i] << " " ;
        }
        cout << vis[n] << endl ;

    }
    else
    {
        printf("NO\n");
    }
    return 0 ;
}

 

标签:跳板,platforms,int,should,platform,position,include,贪心
来源: https://www.cnblogs.com/nonames/p/11823472.html