Codeforces 538 C. Tourist's Notes
2 seconds
256 megabytes
standard input
standard output
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| ≤ 1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| ≤ 1.
The first line contains two space-separated numbers, n and m (1 ≤ n ≤ 108, 1 ≤ m ≤ 105) — the number of days of the hike and the number of notes left in the journal.
Next m lines contain two space-separated integers di and hdi (1 ≤ di ≤ n, 0 ≤ hdi ≤ 108) — the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.
If the notes aren't contradictory, print a single integer — the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).
8 2
2 0
7 0
2
8 3
2 0
7 0
8 3
IMPOSSIBLE
For the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).
In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.
题意就是一个人爬山,不小心把记录自己爬过山的海拔的数据给(我也不知道怎么搞的)丢了一部分,每次爬山相邻两天海拔距离不超过1,问你爬的最高海拔是多少。。。
eg:
数据(a,b),假设从第一天到第a天一直减1,则第一天的海拔为 b + (a - 1);从第pre_a天海拔为pre_b,到第a天海拔为b,假设中间的最高海拔为x,则可列出不等式(x - pre_b) + (x - b) <= a - pre_a,整理得x = (pre_b + b + a - pre_a)/ 2;从最后一条数据(a,b)到第n天,假设一直加1,则第n天的海拔为b+(n-a)。每次求出一个值,求出他们的最大值即可。
自己写的代码wa了,然后看题解又写的,菜的抠脚。。。
代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,a,b;
while(~scanf("%d%d",&n,&m)){
scanf("%d%d",&a,&b);
int pre_a=a,pre_b=b;
int ans=b+a-1;
bool flag=true;
for(int i=1;i<m;i++){
scanf("%d%d",&a,&b);
if(abs(b-pre_b)>a-pre_a) flag=false;
int tmp=(pre_b+b+a-pre_a)/2;
ans=max(ans,tmp);
pre_a=a;pre_b=b;
}
int tmp=pre_b+(n-pre_a);
ans=max(ans,tmp);
if(flag) printf("%d\n",ans);
else printf("IMPOSSIBLE\n");
}
return 0;
}
Codeforces 538 C. Tourist's Notes的更多相关文章
- Codeforces Round #300 C. Tourist's Notes 水题
C. Tourist's Notes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pr ...
- C. Tourist's Notes
C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Tourist's Notes CodeForces - 538C (贪心)
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist no ...
- CodeForces - 340 C - Tourist Problem
先上题目: A - Tourist Problem Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- 【Codeforces Round 1114】Codeforces #538 (Div. 2)
Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...
- Codeforces 538 A. Cutting Banner-substr()函数字符串拼接
A. Cutting Banner time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 【codeforces 732F】Tourist Reform
[题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...
- C. Tourist's Notes
题目链接 题意:n天内登山,相邻两次登山的高度差的绝对值小于等于1,也就是说每次高度变化只能是:0,1,-1.我们已经知道n天中部分天数人所在的山的高度,求最大的登山高度. 输入: n m n 是天 ...
随机推荐
- spring笔记(一)
1. 回顾 Struts与Hibernate可以做什么事? Struts, Mvc中控制层解决方案 可以进行请求数据自动封装.类型转换.文件上传.效验… Hibernate, 持久层的解决方案: 可以 ...
- thead tfoot tbody标签的使用
这三个都是<body>元素的子标签,不常用,因为其只是对<tr>标签做了一个区分 <thread>用于包裹表格头信息 <tfoot>用于包裹表格最后一行 ...
- [POI2011]Lightning Conductor
题面在这里 description 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\). 对于每个\(1\le i\le n\),找到最小的非负整数\(p\), 满足对于任意的\(1 ...
- [LOJ#6437][BZOJ5373]「PKUSC2018」PKUSC
[LOJ#6437][BZOJ5373]「PKUSC2018」PKUSC 试题描述 九条可怜是一个爱玩游戏的女孩子. 最近她在玩一个无双割草类的游戏,平面上有 \(n\) 个敌人,每一个敌人的坐标为 ...
- [NOI.AC省选模拟赛3.23] 染色 [点分治+BFS序]
题面 传送门 重要思想 真的是没想到,我很久以来一直以为总会有应用的$BFS$序,最终居然是以这种方式出现在题目中 笔记:$BFS$序可以用来处理限制点对距离的题目(综合点分树使用) 思路 本题中首先 ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ...
- 你对javascript的原生操作或者工具了解多少呢?
1.有一个长度未知的数组a,如果它的长度为0就把数字1添加到数组里面,否则按照先进先出的队列规则让第一个元素出队. 分析:这道题主要是考核了数组的队列方法和栈方法.另外,原题还有字数限制的,只有在字数 ...
- 2017年研究生数学建模D题(前景目标检测)相关论文与实验结果
一直都想参加下数学建模,通过几个月培训学到一些好的数学思想和方法,今年终于有时间有机会有队友一起参加了研究生数模,but,为啥今年说不培训直接参加国赛,泪目~_~~,然后比赛前也基本没看,直接硬刚.比 ...
- Active Directory Domain Services in Windows Server 2016/2012
Applies To: Windows Server 2016, Windows Server 2012 R2, Windows Server 2012 You will find links to ...
- Ant Design 使用小结
最近公司做了一个系统,因为页面涉及的表单交互非常多,如果使用之前的 Node + Express 的开发模式效率是非常低的,因此经过考虑,最后决定使用 Node + React 的开发模式,并且使用了 ...