codeforces 1256C (贪心+构造)

时间:2022-07-28
本文章向大家介绍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).

给定m个木板,每次只能走木板,最大距离为d,询问能否铺完m个木板,始得从1走到n

思路

很明显是道贪心的问题,如果走d个距离,并且当前位置加上剩余木板的长度不超过n,那么就可以走d,否则就应该铺成一个长木板

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1010;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int a[N],ans[N];
void solve(){
    int n,m,d;cin>>n>>m>>d;
    rep(i,1,m+1) cin>>a[i];
    int sum=0;
    rep(i,1,m+1) sum+=a[i];
    if(sum+(m+1)*(d-1)<n){
        cout<<"NO"<<endl;
        return;
    }
    int pos=0;
    rep(i,1,m+1){
        if(pos+sum+d-1<n+1) pos+=d;
        else pos=n+1-sum;
        rep(j,pos,pos+a[i]) ans[j]=i;
        pos+=a[i]-1;
        sum-=a[i];
    }
    cout<<"YES"<<endl;
    rep(i,1,n+1) cout<<ans[i]<<' ';
    cout<<endl;
}
int main(){
    IOS;
    //int t;cin>>t;
    //while(t--){
        solve();
    //}
    return 0;
}