Codeforces Round #598 (Div. 3) C. Platforms Jumping
There is a river of width nn. The left bank of the river is cell 00 and the right bank is cell n+1n+1 (more formally, the river can be represented as a sequence of n+2n+2 cells numbered from 00 to n+1n+1). There are also mm wooden platforms on a river, the ii-th platform has length cici (so the ii-th platform takes cici consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed nn.
You are standing at 00 and want to reach n+1n+1 somehow. If you are standing at the position xx, you can jump to any position in the range [x+1;x+d][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=1d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 00 and n+1n+1belong to wooden platforms.
You want to know if it is possible to reach n+1n+1 from 00 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).
For example, if n=7n=7, m=3m=3, d=2d=2 and c=[1,2,1]c=[1,2,1], then one of the ways to reach 88 from 00 is follow:
The first example: n=7n=7.
The first line of the input contains three integers nn, mm and dd (1≤n,m,d≤1000,m≤n1≤n,m,d≤1000,m≤n) — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.
The second line of the input contains mm integers c1,c2,…,cmc1,c2,…,cm (1≤ci≤n,∑i=1mci≤n1≤ci≤n,∑i=1mci≤n), where cici is the length of the ii-th platform.
If it is impossible to reach n+1n+1 from 00, print NO in the first line. Otherwise, print YES in the first line and the array aa of length nn in the second line — the sequence of river cells (excluding cell 00 and cell n+1n+1).
If the cell ii does not belong to any platform, aiai should be 00. Otherwise, it should be equal to the index of the platform (11-indexed, platforms are numbered from 11 to mm in order of input) to which the cell ii belongs.
Note that all aiai equal to 11 should form a contiguous subsegment of the array aa of length c1c1, all aiai equal to 22 should form a contiguous subsegment of the array aa of length c2c2, ..., all aiai equal to mm should form a contiguous subsegment of the array aa of length cmcm. The leftmost position of 22 in aa should be greater than the rightmost position of 11, the leftmost position of 33 in aa should be greater than the rightmost position of 22, ..., the leftmost position of mm in aa should be greater than the rightmost position of m−1m−1.
See example outputs for better understanding.
7 3 2
1 2 1
YES
0 1 0 2 2 0 3
10 1 11
1
YES
0 0 0 0 0 0 0 0 0 1
10 1 5
2
YES
0 0 0 0 1 1 0 0 0 0
Consider the first example: the answer is [0,1,0,2,2,0,3][0,1,0,2,2,0,3]. The sequence of jumps you perform is 0→2→4→5→7→80→2→4→5→7→8.
Consider the second example: it does not matter how to place the platform because you always can jump from 00 to 1111.
Consider the third example: the answer is [0,0,0,0,1,1,0,0,0,0][0,0,0,0,1,1,0,0,0,0]. The sequence of jumps you perform is 0→5→6→110→5→6→11.
贪心,按最远的跳,再加上板子长度,判断是否能到河对岸。
如果能,尽量把板子放的远一点。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + ;
int c[N],ans[N];
int main() {
ios_base::sync_with_stdio();
cin.tie();
cout.tie();
int n,m,d;
cin>>n>>m>>d;//n是宽度,m是板子数目,d是可以最多跳多远
int pos=;
for (int i=; i<=m; i++) {
pos+=d;//每次跳最远
int l;
cin>>l;
pos+=l-;
c[i]=l;//记录板子长度
}
pos+=d;//最后再跳一次
if (pos<n+) {//如果跳不过去
cout<<"NO";//就输出NO
return ;
}
cout<<"YES\n";
pos=;
int sum=;
for (int i=; i<=m; i++)
sum+=c[i];//板子总长度
for (int i=; i<=m; i++) {
if (pos+d+sum-<=n) pos+=d;
else pos=n-sum+;
for (int j=pos; j<=pos+c[i]-; j++)
ans[j]=i;
pos+=c[i]-;
sum-=c[i];
}
for (int i=; i<=n; i++)
cout<<ans[i]<<" ";
}
Codeforces Round #598 (Div. 3) C. Platforms Jumping的更多相关文章
- Codeforces Round #598 (Div. 3) C. Platforms Jumping 贪心或dp
C. Platforms Jumping There is a river of width n. The left bank of the river is cell 0 and the right ...
- Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #598 (Div. 3)
传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...
- Codeforces Round #598 (Div. 3) A,B,C,D{E,F待补}
A. Payment Without Change #include<bits/stdc++.h> using namespace std; #define int long long ...
- CodeForces Round #521 (Div.3) A. Frog Jumping
http://codeforces.com/contest/1077/problem/A A frog is currently at the point 00 on a coordinate axi ...
- Codeforces Round #598 (Div. 3)E(dp路径转移)
题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...
- Codeforces Round #598 (Div. 3) F. Equalizing Two Strings 构造
F. Equalizing Two Strings You are given two strings s and t both of length n and both consisting of ...
- Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp
E. Yet Another Division Into Teams There are n students at your university. The programming skill of ...
随机推荐
- Silver Cow Party POJ - 3268
#include<iostream> #include<queue> #include<cstring> using namespace std; +,INF=0x ...
- Linux下用Bash语言实现输出最大值的功能
题目链接: 题目描述 编写一个程序,输入a.b.c三个值,输出其中最大值. 输入 一行数组,分别为a b c 输出 a b c其中最大的数 样例输入 10 20 30 样例输出 30 复习下Linux ...
- java的服务是每收到一个请求就新开一个线程来处理吗?tomcat呢?
首先,服务器的实现不止有这两种方式. 先谈谈题主说的这两种服务器模型: 1.收到一个请求就处理,这个时候就不能处理新的请求,这种为阻塞 这个是单线程模型,无法并发,一个请求没处理完服务器就会阻塞,不会 ...
- SpringBoot 测试基类
每次写单元测试都要重复写一些方法.注解等,这里我写了一下测试的基类 (1) 记录测试方法运行的时间 (2)两个父类方法 print,可打印list和object对象 (3)一个属性 logger 记录 ...
- java多线程技术
如何实现线程 首先实现线程的两个方法:1.继承thread:2.实现接口Runnable类: 这边我就说一下第二种,因此第二种在开发中使用的比较多一些,能避免继承还是少避免继承. RunnableDe ...
- Codeforces Round #615 (Div. 3) A-F简要题解
contest链接:https://codeforces.com/contest/1294 A. 给出a.b.c三个数,从n中分配给a.b.c,问能否使得a = b = c.计算a,b,c三个数的差值 ...
- 进程间通讯IPC的几种方式总结
Linux进程间的通讯 Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间通信方面的侧重点有所不同.前者对Unix早期的进程间通信 ...
- jvm(5):类加载机制
类加载时机 Java虚拟机规范并没有强制约束类加载过程的第一个阶段时机,但对初始化阶段由严格规定5中必须立即对类进行初始化的情况: 遇到new.getstatic.putstatic或invokest ...
- php设计模式之面向过程实现举报功能实例代码
html <html> <head> <meta charset="UTF-8"> <title>责任链模式</title&g ...
- Apache Kafka(三)- Kakfa CLI 使用
1. Topics CLI 1.1 首先启动 zookeeper 与 kafka > zookeeper-server-start.sh config/zookeeper.properties ...