Codeforces 1256C Platforms Jumping

思路:

cf自带的题解,思路很清晰:

This problem has a very easy idea but requires terrible implementation. Firstly, let’s place all platforms as rightmost as we can. Thus, we will have the array, in which the first n−∑i=1mci elements are zeros and other elements are 1, 2, …, m.
Now, let’s start the algorithm. Firstly, we need to jump to the position d or less. If we could jump to the position d then we don’t need to jump to some position to the left from d. But if we cannot do it, let’s take the leftmost platform to the right from the position d and move it in such a way that its left border will be at the position d. Now we can jump to the position d and then jump by 1 right to reach the position d+c1−1. Let’s repeat the same algorithm and continue jumping.
If after some move we can jump to the position at least n+1then we are done.
Time complexity: O(n2)but I’m sure it can be implemented in O(nlogn) or O(n).

整体思路就是贪心。

代码:

#include<bits/stdc++.h>
using namespace std;
#define rp(i,n) for(int i=0;i<n;i++)
#define rpn(i,n) for(int i=1;i<=n;i++)
const int MAX_N=1005;
int c[MAX_N];
int rv[MAX_N];
int main(){
	int n,m,d,suml;
	cin>>n>>m>>d;
	rpn(i,m) cin>>c[i];
	for(int i=m,pos=n;i>=1;i--){
		for(int j=0;j<c[i];j++){
			rv[pos-j]=i;
		}
		pos-=c[i];
	}
	int pos=0,flag=false;
	for(int i=1;i<=m;i++){
		pos+=d;//往前跳d 
		if(pos>n||rv[pos]){flag=true;break;}
		int plat=pos+1;
		while(!rv[plat]) plat++;
		int len=c[rv[plat]];
		for(int j=0;j<len;j++){
			swap(rv[pos+j],rv[plat+j]);
		}
		pos+=len-1;
	}
	if(pos+d>n||flag){
		cout<<"YES\n";
		cout<<rv[1];
		for(int i=2;i<=n;i++) cout<<' '<<rv[i]; 
	}else cout<<"NO";
	return 0;
}
posted @ 2019-11-05 22:55  YuhanのBlog  阅读(120)  评论(0编辑  收藏  举报