Codeforces Round #300 C. Tourist's Notes 水题

C. Tourist's Notes

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/538/problem/C

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

8 2
2 0
7 0

 

Sample Output

2

 

HINT

 

题意

给你n天,我们知道m天爬到了k米高,然后每天最多爬1m或者掉下来1m,然后问你最高的可能性高度是多高

题解:

数学问题……

直接想公式就好

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************


struct node
{
    ll x,y;
};
bool cmp(node a,node b)
{
    return a.x<b.x;
}
node a[maxn];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
        cin>>a[i].x>>a[i].y;
    sort(a,a+m,cmp);
    ll ans=max(a[0].x+a[0].y-1,n-a[m-1].x+a[m-1].y);
   // cout<<ans<<endl;
    for(int i=0;i<m-1;i++)
    {
        if(abs(a[i].y-a[i+1].y)>abs(a[i].x-a[i+1].x))
        {
            printf("IMPOSSIBLE");
            return 0;
        }
        int dota=abs(a[i].x-a[i+1].x)-abs(a[i].y-a[i+1].y);
        ans=max(ans,max(a[i].y,a[i+1].y)+dota/2);

    }
    cout<<ans<<endl;
}

 

posted @ 2015-04-27 03:32  qscqesze  阅读(299)  评论(0编辑  收藏  举报