codeforces 637D D. Running with Obstacles(dp,水题,贪心)
题目链接:
2 seconds
256 megabytes
standard input
standard output
A sportsman starts from point xstart = 0 and runs to point with coordinate xfinish = m (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than s meters (in this case for these s meters his path should have no obstacles), and after that he can jump over a length of not more than d meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle.
On the way of an athlete are n obstacles at coordinates x1, x2, ..., xn. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point.
The first line of the input containsd four integers n, m, s and d (1 ≤ n ≤ 200 000, 2 ≤ m ≤ 109, 1 ≤ s, d ≤ 109) — the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.
The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ m - 1) — the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order.
If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes).
If the athlete can get from start to finish, print any way to do this in the following format:
- print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should run for "X" more meters;
- print a line of form "JUMP Y" (where "Y" should be a positive integer), if the sportsman starts a jump and should remain in air for "Y" more meters.
All commands "RUN" and "JUMP" should strictly alternate, starting with "RUN", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish.
3 10 1 3
3 4 7
RUN 2
JUMP 3
RUN 1
JUMP 2
RUN 2
2 9 2 3
6 4
IMPOSSIBLE 题意:n个障碍,坐标分别是啊a[i],终点是m,助跑s米才能最远跳d米,问最后是否能到达m,以及是怎样到达的;
思路:把障碍都标记看是否与上一障碍之间的距离是否够s米,不够的话就将这个与上一障碍合并,我采用一个数组标记的方法标记合并的最前面的障碍位置,再从后往前找,看跑和跳是否满足条件,是的话用ans数组记录下来,不行的话就return 0 结束;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
int a[N],dp[N],ans[*N];
int n,m,s,d;
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&d);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+);
if(a[]->=s)dp[]=;
else dp[]=;
for(int i=;i<=n;i++)
{
if(a[i]-a[i-]->=s)dp[i]=i;
else dp[i]=dp[i-];
}
/*for(int i=1;i<=n;i++)
{
cout<<dp[i]<<"#"<<endl;
}*/
int pos=,flag=,cnt=;
stack<int>st;
for(int i=n;i>; )
{
if(dp[i]==i)
{
st.push(a[i]+);
st.push(a[i]-);
i--;
}
else
{
st.push(a[i]+);
i=dp[i];
st.push(a[i]-);
i--;
}
}
while(!st.empty())
{
int x=st.top();
st.pop();
if(flag)
{
if(x-pos>=s)
{
ans[cnt++]=x-pos;
pos=x;
}
else
{
cout<<"IMPOSSIBLE"<<"\n";
return ;
}
flag=;
}
else
{
if(x-pos<=d)
{
ans[cnt++]=x-pos;
pos=x;
}
else
{
cout<<"IMPOSSIBLE"<<"\n";
return ;
}
flag=;
}
}
if(a[n]!=m-)
{
ans[cnt++]=m-a[n]-;
}
for(int i=;i<cnt;i++)
{
if(i%)printf("RUN %d\n",ans[i]);
else printf("JUMP %d\n",ans[i]);
}
return ;
}
codeforces 637D D. Running with Obstacles(dp,水题,贪心)的更多相关文章
- Codeforces Round #303 (Div. 2) D. Queue 水题贪心
题目: 题意:给你n个数值,要求排列这个序列使得第k个数值的前K-1个数的和>=第k个数值的个数尽可能多: #include <iostream> #include <cstd ...
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- codeforces Gym 100187L L. Ministry of Truth 水题
L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- Codeforces Round #185 (Div. 2) B. Archer 水题
B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...
- Educational Codeforces Round 14 A. Fashion in Berland 水题
A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...
- Codeforces Round #360 (Div. 2) A. Opponents 水题
A. Opponents 题目连接: http://www.codeforces.com/contest/688/problem/A Description Arya has n opponents ...
- Codeforces Round #190 (Div. 2) 水果俩水题
后天考试,今天做题,我真佩服自己... 这次又只A俩水题... orz各路神犇... 话说这次模拟题挺多... 半个多小时把前面俩水题做完,然后卡C,和往常一样,题目看懂做不出来... A: 算是模拟 ...
随机推荐
- 安装Struts2 类库
现在,如果一切正常,那么你可以继续设置您的Struts 2框架.以下是简单的步骤,下载并安装在机器上Struts2. 请选择是否要安装Hibernate在Windows或Unix,然后继续进行下一个步 ...
- EasyUI入门视频教程
EasyUI入门视频教程02 http://www.tudou.com/programs/view/TBlaIcNU5ck/
- sql生成器(含凝视)问题修复版
接上篇http://blog.csdn.net/panliuwen/article/details/47406455 sql生成器--生成含凝视的sql语句 今天我使用自己写的sql生成器了.自我感觉 ...
- Linux进程间通信(二) - 消息队列
消息队列 消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据. 消息队列和之前讨论过的管道和FIFO有很大的区别,主要有以下两点(管道请查阅我的另一篇文章 ...
- php编译参数选项 具体参数含义可以用./configure --help来查看
php编译参数选项 PHP_INSTALL_PATH=/data/web/php MYSQL_INSTALL_PATH=/data/web/mysql ./configure --prefix=${ ...
- centos7.0 tomcat9.0 ip访问 manager
版本:Tomcat 9.0 问题:新安装的tomcat,用其他机器访问tomcat的Server Status.Manager App.Host Manager三个页面均显示403(本机访问没有问题) ...
- vue项目在APP禁止页面缩放
veu-cli自动生成的项目中,index.html中meta 标签内容如下,放在手机上浏览 是可以放大缩小的<meta name="viewport" content=&q ...
- Java从零开始 第一天
java是一门优秀的编程语言.java也有很多优点.从零开始.这是第一天,不是为什么,而是让自己学的更多. 1.下载JDK 在浏览器输http://www.oracle.com/index.html. ...
- mysql怎么在已建好的表中添加自增序列
alter table 表明 change id id int not null auto_increment unique;
- python基础19 -------面向对象终结篇(介绍python对象中各种内置命令)
一.isinstance()和issubclass()命令 1.isinstance(对象,类型) 用来判定该对象是不是此类型或者说是该对象是不是此类的对象,返回结果为True和False,如图所示. ...