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;
}
- Mysql优化系列(0)--总结性梳理
- 将淘宝数据包导入自己的商城系统
- 大话Git
- AS3中的序列化与反序列化
- 在Entity Framework 中执行T-sql语句
- 按分类统计商品总数的性能优化思考
- 机器学习之Python基础(一)
- 中国造机器人突破新高,将大量挤压就业空间,明天在哪里打工?
- 嵌入式数据库Perst
- USB 设备的PID-Product ID,VID-Vendor ID
- Asp.Net4.0/VS2010新变化(3):webform中也可以直接url路由
- IIS7上部署Asp.Net4.0时UrlRouting的若干问题
- As3.0中的反射
- 域名yg.cc以10.2万元的价格结拍,可搭建“摇滚”音乐平台
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 逐行阅读Spring5.X源码(七)扫描和注册神器 ConfigurationClassPostProcessor ,学此类者,胜过学九阳神功!胆小勿入!
- 「Mysql索引原理(三)」Mysql中的Hash索引原理
- RNN、lstm、gru详解
- 「Mysql索引原理(四)」单列索引
- 「Mysql索引原理(五)」多列索引
- 「Mysql索引原理(六)」聚簇索引
- 「Mysql索引原理(七)」覆盖索引
- 「Mysql索引原理(八)」使用索引扫描做排序
- 「Mysql索引原理(九)」前缀压缩索引
- 「Mysql索引原理(十)」冗余和重复索引
- 「Mysql索引原理(十一)」索引和锁
- 「Mysql索引原理(十二)」索引案例1-支持多种过滤条件
- 「Mysql索引原理(十三)」索引案例2-避免多个范围条件
- 「Mysql索引原理(十四)」索引案例3-优化排序
- 字符仿真