C. Tourist's Notes
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.

思路:二分;

分别讨论第一个点前后最后一个点后,贪心选取最大,然后再二分每两个点之间能取得的最大值,当两个点的差大于两点间的距离时无解,复杂度(n*log(n));

 1 #include<iostream>
2 #include<algorithm>
3 #include<queue>
4 #include<stdlib.h>
5 #include<stack>
6 #include<stdio.h>
7 #include<string.h>
8 using namespace std;
9 typedef struct node
10 {
11 int day;
12 int hi;
13 } ss;
14 ss ans[1000005];
15 int main(void)
16 {
17 int n,m;
18 while(scanf("%d %d",&n,&m)!=EOF)
19 {
20 int i,j;
21 int flag = 0;
22 for(i = 0; i < m; i++)
23 scanf("%d %d",&ans[i].day,&ans[i].hi);
24 int maxx = -1;
25 for(i = 0; i < m; i++)
26 {
27 if(i == 0)
28 {
29 maxx = max(ans[i].day-1+ans[i].hi,maxx);
30 }
31 else
32 {
33 if(abs(ans[i].hi-ans[i-1].hi)>ans[i].day-ans[i-1].day)
34 flag = 1;
35 else
36 {
37 int l = max(ans[i-1].hi,ans[i].hi);
38 int r = 1e9;
39 while(l<=r)
40 {
41 int mid = (l+r)/2;
42 int x = abs(mid-ans[i].hi);
43 int y = abs(mid-ans[i-1].hi);//printf("%d\n",mid);
44 if(x+y<=ans[i].day-ans[i-1].day)
45 {
46 maxx = max(maxx,mid);
47 l = mid+1;
48 }
49 else r = mid-1;
50 }
51 }
52 } if(i == m-1)
53 {
54 maxx = max(maxx,n-ans[i].day+ans[i].hi);
55 }
56 }if(flag)printf("IMPOSSIBLE\n");
57 else
58 printf("%d\n",maxx);
59 }
60 return 0;
61 }

代码库

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. Codeforces 538 C. Tourist's Notes

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

  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. C. Tourist's Notes

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

  5. Codeforces Round #300(A.【字符串,多方法】,B.【思维题】,C.【贪心,数学】)

    A. Cutting Banner time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  6. Codeforces Round #300 解题报告

    呜呜周日的时候手感一直很好 代码一般都是一遍过编译一遍过样例 做CF的时候前三题也都是一遍过Pretest没想着去检查... 期间姐姐提醒说有Announcement也自信不去看 呜呜然后就FST了 ...

  7. C. Tourist Problem

    http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...

  8. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  9. Codeforces Round #198 (Div. 2) C. Tourist Problem

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. EPOLL原理详解(图文并茂)

    文章核心思想是: 要清晰明白EPOLL为什么性能好. 本文会从网卡接收数据的流程讲起,串联起CPU中断.操作系统进程调度等知识:再一步步分析阻塞接收数据.select到epoll的进化过程:最后探究e ...

  2. 在Kubernetes上安装MySQL-PXC集群

    官方部署文档地址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html 一.部署方式 示例在k8s集群(至少3 ...

  3. Spark基础:(一)初识Spark

    1.Spark中的Python和Scala的Shell (1): Python的Spark Shell 也就是我们常说的PySpark Shell进入我们的Spark目录中然后输入 bin/pyspa ...

  4. Spark(八)【利用广播小表实现join避免Shuffle】

    目录 使用场景 核心思路 代码演示 正常join 正常left join 广播:join 广播:left join 不适用场景 使用场景 大表join小表 只能广播小表 普通的join是会走shuff ...

  5. 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)

    1.File 1.1 构造方法(只是创建已经存在文件的对象,并不能创建没有的文件) (1)public File(String pathname) (2)public File(String pare ...

  6. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  7. 安全相关,xss

    XSS XSS,即 Cross Site Script,中译是跨站脚本攻击:其原本缩写是 CSS,但为了和层叠样式表(Cascading Style Sheet)有所区分,因而在安全领域叫做 XSS. ...

  8. 2021广东工业大学新生赛决赛 L-歪脖子树下的灯

    题目:L-歪脖子树下的灯_2021年广东工业大学第11届腾讯杯新生程序设计竞赛(同步赛) (nowcoder.com) 比赛的时候没往dp这方面想(因为之前初赛和月赛数学题太多了啊),因此只往组合数学 ...

  9. YYYY-MM-DD引发的问题

    yyyy 和 YYYY 用YYYY格式化代码 2019-12-31 转 YYYY/MM/dd 格式: 2020/12/31 2020-01-01 转 YYYY/MM/dd 格式: 2020/01/01 ...

  10. recyclerView DiffUtil使用

    DiffUtil是和RecyclerView一块用的,DiffUtil用来比较两个数据集,他的最大用处是在RecyclerView刷新时,不在无脑. 以前adapter.notifyDataSetCh ...