您的位置:首页 > 其它

Codeforces Round #300-Tourist's Notes(贪心)

2015-05-01 16:26 387 查看
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. 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 2
2 0
7 0


Output
2


Input
8 3
2 0
7 0
8 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.

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=1e5+10;
struct node
{
int x,y;
}q[maxn],p;

int main()
{
int n,m,i;
int two;
int three;
int flag;
while(~scanf("%d %d",&n,&m)){
flag=0;
scanf("%d %d",&q[0].x,&q[0].y);
int res=q[0].x+q[0].y-1;
p.x=q[0].x;
p.y=q[0].y;
for(i=1;i<m;i++){
scanf("%d %d",&q[i].x,&q[i].y);
if(fabs(q[i].y-p.y)>q[i].x-p.x){
flag=1;
}
two=(q[i].x-p.x+q[i].y+p.y)/2;
res=max(res,two);
p.x=q[i].x;
p.y=q[i].y;
}
three=p.y+(n-p.x);
res=max(res,three);
if(flag)
printf("IMPOSSIBLE\n");
else
printf("%d\n",res);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: