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;
    }
    
  • 相关阅读:
    kubernetes中跨namespace的服务调用 & 外部服务调用 & host配置
    pm2 ——带有负载均衡功能的Node应用的进程管理器
    Linux——docker启用配置:dockerfile——ubuntu
    .net core 单元测试——sonar
    Linux——工具参考篇(3)——ps 进程查看器
    开发框架
    Django框架 + Djiango安装 + First Djiango + 常用命令
    计算机网络知识点随机整理
    Ubuntu tricks
    Python 图片Resize.py
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308898.html
Copyright © 2011-2022 走看看