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;
    }
    
  • 相关阅读:
    c语言printf实现同一位置打印输出
    https加解密过程
    矩形重叠判断
    cocos creator Touch事件应用(触控选择多个子节点)
    js动态创建类对象
    HTTP ERROR 400 Bad Request
    JavaScript(JS)计算某年某月的天数(月末)
    spring hibernate实现动态替换表名(分表)
    Zookeeper实现分布式锁
    Spring FactoryBean和BeanFactory 区别
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308898.html
Copyright © 2011-2022 走看看