zoukankan      html  css  js  c++  java
  • Codeforces Round #598 (Div. 3) C. Platforms Jumping

    There is a river of width nn. The left bank of the river is cell 00 and the right bank is cell n+1n+1 (more formally, the river can be represented as a sequence of n+2n+2 cells numbered from 00 to n+1n+1). There are also mm wooden platforms on a river, the ii-th platform has length cici (so the ii-th platform takes cici consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed nn.

    You are standing at 00 and want to reach n+1n+1 somehow. If you are standing at the position xx, you can jump to any position in the range [x+1;x+d][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=1d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 00 and n+1n+1belong to wooden platforms.

    You want to know if it is possible to reach n+1n+1 from 00 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=7n=7, m=3m=3, d=2d=2 and c=[1,2,1]c=[1,2,1], then one of the ways to reach 88 from 00 is follow:

    The first example: n=7n=7.
    Input

    The first line of the input contains three integers nn, mm and dd (1n,m,d1000,mn1≤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 mm integers c1,c2,,cmc1,c2,…,cm (1cin,i=1mcin1≤ci≤n,∑i=1mci≤n), where cici is the length of the ii-th platform.

    Output

    If it is impossible to reach n+1n+1 from 00, print NO in the first line. Otherwise, print YES in the first line and the array aa of length nn in the second line — the sequence of river cells (excluding cell 00 and cell n+1n+1).

    If the cell ii does not belong to any platform, aiai should be 00. Otherwise, it should be equal to the index of the platform (11-indexed, platforms are numbered from 11 to mm in order of input) to which the cell ii belongs.

    Note that all aiai equal to 11 should form a contiguous subsegment of the array aa of length c1c1, all aiai equal to 22 should form a contiguous subsegment of the array aa of length c2c2, ..., all aiai equal to mm should form a contiguous subsegment of the array aa of length cmcm. The leftmost position of 22 in aa should be greater than the rightmost position of 11, the leftmost position of 33 in aa should be greater than the rightmost position of 22, ..., the leftmost position of mm in aa should be greater than the rightmost position of m1m−1.

    See example outputs for better understanding.

    Examples
    input
    Copy
    7 3 2
    1 2 1
    
    output
    Copy
    YES
    0 1 0 2 2 0 3 
    
    input
    Copy
    10 1 11
    1
    
    output
    Copy
    YES
    0 0 0 0 0 0 0 0 0 1 
    
    input
    Copy
    10 1 5
    2
    
    output
    Copy
    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][0,1,0,2,2,0,3]. The sequence of jumps you perform is 0245780→2→4→5→7→8.

    Consider the second example: it does not matter how to place the platform because you always can jump from 00 to 1111.

    Consider the third example: the answer is [0,0,0,0,1,1,0,0,0,0][0,0,0,0,1,1,0,0,0,0]. The sequence of jumps you perform is 056110→5→6→11.

    贪心,按最远的跳,再加上板子长度,判断是否能到河对岸。

    如果能,尽量把板子放的远一点。

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 11;
    int c[N],ans[N];
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int n,m,d;
        cin>>n>>m>>d;//n是宽度,m是板子数目,d是可以最多跳多远
        int pos=0;
        for (int i=1; i<=m; i++) {
            pos+=d;//每次跳最远
            int l;
            cin>>l;
            pos+=l-1;
            c[i]=l;//记录板子长度
        }
        pos+=d;//最后再跳一次
        if (pos<n+1) {//如果跳不过去
            cout<<"NO";//就输出NO
            return 0;
        }
        cout<<"YES
    ";
        pos=0;
        int sum=0;
        for (int i=1; i<=m; i++)
            sum+=c[i];//板子总长度
        for (int i=1; i<=m; i++) {
            if (pos+d+sum-1<=n) pos+=d;
            else pos=n-sum+1;
            for (int j=pos; j<=pos+c[i]-1; j++)
                ans[j]=i;
            pos+=c[i]-1;
            sum-=c[i];
        }
        for (int i=1; i<=n; i++)
            cout<<ans[i]<<" ";
    }
  • 相关阅读:
    由wifi吞吐量问题联想到的分治思维
    总结----调试问题套路(经验)
    常用指令备忘录----持续更新
    【mark】OS是否使用svc方式分开系统空间和用户空间的优劣
    转载----五种开源协议(GPL,LGPL,BSD,MIT,Apache)
    rt-thread 动态装载实现、优化
    转:嵌入式: jffs2,yaffs2,logfs,ubifs文件系统性能分析
    gcc ld 链接器相关知识,调试指令(程序员的自我修养----链接、装载与库)
    HTML5与CSS3经典代码
    jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11802854.html
Copyright © 2011-2022 走看看