time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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).

Examples
Input
8 2
2 0
7 0
Output
2
Input
8 3
2 0
7 0
8 3
Output
IMPOSSIBLE
Note

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的更多相关文章

  1. 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 ...

  2. C. Tourist's Notes

    C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Tourist's Notes CodeForces - 538C (贪心)

    A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist no ...

  4. CodeForces - 340 C - Tourist Problem

    先上题目: A - Tourist Problem Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  5. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

  6. 【Codeforces Round 1114】Codeforces #538 (Div. 2)

    Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...

  7. Codeforces 538 A. Cutting Banner-substr()函数字符串拼接

      A. Cutting Banner   time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. 【codeforces 732F】Tourist Reform

    [题目链接]:http://codeforces.com/contest/732/problem/F [题意] 给你一张无向图; n个点,m条边; 让你把这张图改成有向边 然后定义r[i]为每个点能够 ...

  9. C. Tourist's Notes

    题目链接 题意:n天内登山,相邻两次登山的高度差的绝对值小于等于1,也就是说每次高度变化只能是:0,1,-1.我们已经知道n天中部分天数人所在的山的高度,求最大的登山高度. 输入: n m  n 是天 ...

随机推荐

  1. spring笔记二

    DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中.依赖注入的目的并非为软件系统带来更多功能,而是 ...

  2. oracle 引用类型声明

  3. [UOJ #48]【UR #3】核聚变反应强度

    题目大意:给你一串数$a_i$,求$sgcd(a_1,a_i)$,$sgcd(x,y)$表示$x,y$的次大公约数,若没有,则为$-1$ 题解:即求最大公约数的最大约数,把$a_1$分解质因数,求出最 ...

  4. 皮肤包项目的 Gradle 脚本演化

    我在做的一个项目需要有换肤功能,换肤的方案是采用第三方库 ThemeSkinning 的实现(在其基础上修复若干 bug).皮肤的制作是把相关的资源放在一个 app module 中打包成 apk,当 ...

  5. 理解First Chance和Second Chance避免单步调试

    原文链接地址:http://blog.csdn.net/Donjuan/article/details/3859160 在现在C++.Java..Net代码大行其道的时候,很多代码错误(Bug)都是通 ...

  6. BZOJ1876 [SDOI2009]SuperGCD 【高精 + GCD优化】

    题目 Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比 赛计算GCD.有一天Sheng bill很嚣张地找到了你,并要求和你比 赛,但 ...

  7. JS让任意图片垂直水平居中且页面不滚动

    说一下以前遇到的一个问题: 假设有一张小图,要实现点击查看大图的功能,而这个图的宽高可能会超过浏览器的宽高,这时候我们通过JS来改变图片的宽高,从而实现图片在浏览器居中显示且不滚屏. 方法如下: 首先 ...

  8. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C (用map 超时)

    C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. powershell for rename server name

    Rename server name if server has not yet joined AD $Hostname = "newname" $username = " ...

  10. win7---虚拟wifi无法启动承载网络

    转载自:http://nbbbs.zol.com.cn/40/32108_397955.html 解决方案:控制面板→硬件和声音→电源选项→更改计划设置→更改高级电源设置→无线适配器设置→节能模式,改 ...