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;
}

最新文章

  1. 第十周java 学习总结
  2. Codeforces 556A Case of the Zeros and Ones(消除01)
  3. windows设备驱动安装指南
  4. 使用 Gradle 插件进行代码分析(转)
  5. 如何在RHEL7上搭建Samba服务实现Windows与Linux之间的文件共享
  6. Java数组的创建和初始化
  7. 邓_phpcms_数据库
  8. Java包装类之Integer的 &quot;==&quot; 判断数值是否相等的陷阱及原因分析
  9. bug6 项目检出JRE问题(Unbound classpath container: &#39;JRE System Library [JavaSE-1.7]&#39; in project &#39;idweb&#39;)
  10. 0x01 Spring Cloud 概述
  11. LeetCode--496--下一个更大元素I(java)
  12. Java 内存管理白皮书
  13. 【[SDOI2016]生成魔咒】
  14. django2.0新增功能流程
  15. 【坚持】Selenium+Python学习之从读懂代码开始 DAY1
  16. Nginx概述、安装及配置详解
  17. 【Hadoop系列】linux SSH原理解析
  18. js----Navigator对象,查看浏览器信息,Screen对象,查看屏幕信息
  19. hdu5542 The Battle of Chibi[DP+BIT]
  20. Objective-C中的关联(objc_setAssociatedObject,objc_getAssociatedObject,objc_removeAssociatedObjects)

热门文章

  1. Hibernate关联映射之_一对一
  2. IE6中png背景图片透明的最好处理方法
  3. gdb调试命令的使用及总结
  4. BZOJ4008 [HNOI2015]亚瑟王 【概率dp】
  5. HDU1166 敌兵布阵(树状数组实现
  6. python爬取七星彩的开奖历史记录
  7. Spring学习--集合属性
  8. 3中转换JSON数据的方式
  9. DOM遍历查找结点
  10. java基础学习(一)hashcode