Codeforces Round #300——C贪心——Tourist's Notes

Description

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

Sample Input

Input

8 22 07 0

Output

2

Input

8 32 07 08 3

Output

IMPOSSIBLE

Hint

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.

/*
   坑点在于第一天和最后一天还要有个max1的判断
   先对日期进行排序
   从i到i+1天所能走的最长的路a[i+1].d - a[i].d
   必须要走的路为 abs(a[i+1].h - a[i].h)
   剩下的是可以宽松的路程,来回除2最后再加上最后到得位置(最高)可以一开始往上再往下,也可以先往上再往下再往上
   再判掉不可能的情况就esay了
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct edge{
    int d, h;
}a[100010];

bool cmp(edge i,edge j){
    return i.d < j.d;
}
int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= m ;i++)
            scanf("%d%d", &a[i].d, &a[i].h);
        sort(a + 1, a + 1 + m, cmp);
        int max1 = 0;
        int flag = 0;
        for(int i = 1; i < m - 1; i++){
            int t1 = max(a[i+1].h, a[i].h);
            int t2 = min(a[i+1].h, a[i].h);
            if(a[i+1].d - a[i].d - t1 + t2 < 0) {flag = 1; break;}
            max1 = max(max1, (a[i+1].d - a[i].d - t1 + t2 )/2 + t1);
        }
        if(m >= 2){
      int  t1 = max(a[m].h, a[m-1].h);
      int  t2 = min(a[m].h, a[m-1].h);
        if(a[m].d - a[m-1].d - t1 + t2 < 0) {flag = 1;}
        max1 = max(max1, (a[m].d - a[m-1].d - t1 + t2)/2 + t1 );
        }
        max1 = max(a[m].h + n - a[m].d, max1);
        max1 = max(max1, a[1].h + a[1].d - 1);
        if(flag == 1) printf("IMPOSSIBLE\n");
        else
       printf("%d\n", max1);
    }
   return 0;
}

  

Codeforces Round #300——C贪心——Tourist's Notes

时间: 2024-12-12 18:07:22

Codeforces Round #300——C贪心——Tourist's Notes的相关文章

贪心 Codeforces Round #300 A Cutting Banner

题目传送门 1 /* 2 贪心水题:首先,最少的个数为n最大的一位数字mx,因为需要用1累加得到mx, 3 接下来mx次循环,若是0,输出0:若是1,输出1,s[j]--: 4 注意:之前的0的要忽略 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <string> 10 #include <algorithm> 11 #include

Codeforces Round #300 (A,B,C,D)

题目传送:Codeforces Round #300 A. Cutting Banner 思路:一看题就会错意了,然后一顿猛敲,果不其然的被hack了,然后才发现只需要剪中间那一段就可以了,然后又傻逼得少写一个等号,还是被hack了,心累啊 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #in

水题 Codeforces Round #300 A Cutting Banner

题目传送门 1 /* 2 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 3 判断是否余下的是 "CODEFORCES" :) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath>

Codeforces Round #300

A. Cutting Banner 关键字:[读题][字符串][枚举] 坑点:能不能只减去某一段字符串把剩下的按原来的顺序连起来组成"CODEFORCES",而减下一段"CODEFORCES"是不行的 e.g. "CODEFAAAAORCES"是可行的         而 "AACODEFORCESAA"是不可行的 B. Quasi Binary 题意:给出一个数n,求最少用几个形如"1010"这样只包含'

Codeforces Round #300 - Quasi Binary(贪心)

Quasi Binary Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 - ar

Codeforces Round #300 B

B. Quasi Binary A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 - are quasibinary and numbers 2, 12, 900 are not. You are given a positive integer n. Represent it as a s

Codeforces Round #300 E - Demiurges Play Again

E - Demiurges Play Again 感觉这种类型的dp以前没遇到过... 不是很好想.. dp[u] 表示的是以u为子树进行游戏得到的值是第几大的. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PII pair<int, int> #define y1 skldjfskldjg #define y

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 538C. Tourist&#39;s Notes

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 t