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: 算是模拟 ...
随机推荐
- Markdown常用语法学习
Markdown常用语法学习,这些就够用了. 演示地址: https://github.com/YalongYan/Markdown-- 特别提示: 标题'##'后面必须加一个空格,否则编译不对.# ...
- 微信小程序的文件结构 —— 微信小程序教程系列(1)
所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/548 ...
- Java程序发送邮件
之前上网有看到过别人总结的使用java程序发送邮件,于是自己下来练习,把自己学习的一些心得总结出来. 首先我们这里需要采用两个jar包: 需要的朋友可以自行上网去CSDN类似的网站上面找 顺便把自己测 ...
- PBR探索
原理 根据能量守恒,以及一系列光照原理得出微表面BRDF(Bidirectional Reflectance Distribution Function)公式 // D(h) F(v,h) G(l,v ...
- CentOS6下安装PHP7
更新软件源[1] wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm wget http://rpm ...
- 自定义弹窗 VS AlertDialog分享弹窗
一.摘要 弹窗通常用于提示用户进行某种操作,比如:点击分享按钮,弹窗分享对话框:双击返回按钮,弹窗退出对话框:下载文件,提示下载对话框等等,分享对话框/退出对话框/下载对话框,都可以直接使用Alert ...
- Linux 3 -grep
七. grep家族: 1. grep退出状态: 0: 表示成功: 1: 表示在所提供的文件无法找到匹配的pattern: 2: 表示参数中提供的文件不存在. 见如下示例: /> grep 'ro ...
- C# ADO.NET学习
Connetction 对象: 数据库服务器 数据库名字 登录名.密码 连接数据库所需要的其他参数 Command对象: ExecuteScalar();//首行首列的内容 ExecuteNomQue ...
- Python 3 面向对象进阶
Python 3 面向对象进阶 一. isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的 ...
- android6.0 外部存储设备插拔广播以及获取路径(U盘)【转】
本文转载自:https://blog.csdn.net/zhouchengxi/article/details/53982222 这里我将U盘作为例子来说明解析. android4.1版本时U盘插拔时 ...