CodeForces - 1256C (+ thinking greedy)

The meaning of problems

https://vjudge.net/problem/CodeForces-1256C

It has a width of n river. River's left bank are numbered 0, on the right bank number is the n- + 1. On rivers and m a wooden platform, the i length of the platforms of C i (i-th so that the platform occupies rivers C i successive positions). The sum of the length of the platform to ensure that not more than n.

You are standing 0 (left bank), and the like to reach the right bank i.e. n- + 1 position. If you stand location x, you can skip [ X + . 1, X + D ] at any position within the range. However, you can only jump on the wooden platform (  ie, not water  ). For example, if D = . 1, only then jumps to the next position (if there is a wooden platform in this position). You can assume cells 0 and n- + . 1 belongs to the wooden platform.

You can use any platform to the left or right to move any number of times (can not move), so long as they do not overlap each other (but the two platforms side by side). That means you can not change the relative order of the platform.

Please note that you should first mobile platform and then jump (once you start, you can no longer mobile platform).

For example, if n- = . 7, m = . 3, D = 2 and C = [ . 1 , 2 , . 1 one], it jumps to the right bank from the left bank of the method:

Thinking

I began to read the title wrong, father of the pit. Note that each board's order can not be changed, and each board has to spend. Because our primary goal is to reach the n + 1, so greedy heartbeat Step d, but we have to consider leaving enough space to put the board, so if the current position of the board length + d + not discharge and -1 <= n, then we jump to the current position + d; otherwise not Skip n- discharge board length and +1, each jump to put the board in position, the final determination whether to jump to n + 1.

Code

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=2005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int main()
{
    std::ios::sync_with_stdio(false);
    int n,m,d;
    while(cin>>n>>m>>d)
    {
        int w[N],sum=0;
        for(int i=1; i<=m; i++)
        {
            cin>>w[i];
            sum+=w[i];
        }
        int s=0,t=1;
        int ans[N];
        memset(ans,0,sizeof(ans));
        int flag=0;
        while(s<=n)
        {
            //      cout<<"s:"<<s<<endl;

            if(s+d+sum-1<=n)
                s+=d;
            else
            {
                s=n-sum+1;
            }
            if(s>=n+1)
            {
                break;
            }
            //    cout<<s<<endl;
            if(t<=m)
            {
                for(int i=s; i<s+w[t]; i++)
                {
                    ans[i]=t;
                }
                s=s+w[t]-1;
                sum-=w[t];
                t++;
            }
            else
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            cout<<"NO"<<endl;
        }
        else
        {
            cout<<"YES"<<endl;
            for(int i=1; i<=n; i++)
            {
                cout<<ans[i]<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/11848317.html