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: 算是模拟 ...
随机推荐
- java心跳发送
java心跳发送: 大家都知道.如果你在互联网公司,并且开发的是产品那你一定接触不到.心跳机制.心跳包 那什么是心跳机制呢? 心跳机制就是定时发送一个自定义的结构体(心跳包).确保连接的有效的机制. ...
- 探究css中各种情况下的元素的垂直和水平居中的问题(面试题)
今天各种纠结,真的是不想写东西(ps 我比较懒)但是老是有人问这个问题,于是我就本着分享精神还是整理一下,好了废话不多说 开始上代码 问题:外边是一个容器,容器中还有一个容器,那么请问怎么让里边的容器 ...
- thinkPHP5.0的学习研究【基础】
2017年6月19日13:25:56 基础:1.ThinkPHP5的环境要求如下: PHP >= 5.4.0 PDO PHP Extension MBstring P ...
- java ScriptEngine 使用
Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...
- redis持久化RDB详细操作步骤
1.xshell远程登录服务器ssh root@192.168.142.130 2.切换到redis目录 3.创建一个配置文件s2-redis.conf 4.编辑文件 vi s2-redis.conf ...
- centos 安装Phpstorm
下载: http://www.jetbrains.com/phpstorm/download/#section=linux 解压: tar -zxf PhpStorm-8.0.1.tar.gz # 然 ...
- CryptoJS加密
<script type="text/javascript" src="CryptoJS/core.min.js"></script>& ...
- 基于卡方的独立性检验原理及R语言实现
在读到<R语言实战>(第二版)P143页有关卡方独立性检验所记 假设检验 假设检验(Test of Hypothesis)又称为显著性检验(Test of Ststistical Sign ...
- rails dependent
dependent 可以設定當物件刪除時,也會順便刪除它的 has_many 物件: class Event < ActiveRecord::Base has_many :attendees, ...
- [转]eclipse中的常用快捷键
1.选中你要加注释的区域,用ctrl+shift+C 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上注释3.要修改在eclispe中的命令的快捷键方式我们只需进入windo ...