zoukankan      html  css  js  c++  java
  • 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
    ";
    		cout<<rv[1];
    		for(int i=2;i<=n;i++) cout<<' '<<rv[i]; 
    	}else cout<<"NO";
    	return 0;
    }
    
  • 相关阅读:
    Web_0012:FTP文件编辑器关联
    JS_0033:判断苹果安卓 微信浏览器,并在苹果微信浏览器下做特别处理
    JS_0032:匿名函数 function前加 ! 或 function用括号包裹起来 含义
    JS_0031:JS 常用方法
    spark为什么用scala语言开发
    spark开发常见问题
    java实现多继承
    数仓架构
    object类中的方法
    Spring的IOC原理
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308898.html
Copyright © 2011-2022 走看看