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
Output
Input
Output
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.
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; }
相关文章推荐
- Code Forces 538 C. Tourist's Notes(贪心)
- CF 538C(Tourist's Notes-贪心)
- #300 (div.1 2) C. Tourist's Notes
- HDU 1009 FatMouse' Trade(经典贪心)
- hdu 5281 Senior's Gun(贪心)
- pid=1009 FatMouse' Trade - 简单贪心
- uva 10026 Shoemaker's Problem 贪心
- hdu 1009 FatMouse' Trade(贪心)
- Jack's Notes8——委托的使用(用三层实现TreeView)
- 4004 The Frog's Games --二分+贪心
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
- Problem 1567 - D - Sloth's Angry ( 递归+贪心)
- HDU 3177 Crixalis's Equipment 贪心
- HDU 4415 Assassin's Creed(贪心)
- HDU 4004 The Frog's Games(基本算法-贪心,搜索-二分)
- uva - 10905 - Children's Game(贪心,qsort排序)
- HDU1009 - FatMouse' Trade (贪心)
- Jack's Notes12——全选、全不选、反选(循环遍历给按钮动态注册事件)
- uva 10026 Shoemaker's Problem _贪心