CF 538C (Tourist & amp; #39; s Notes-Greedy)

Source: Internet
Author: User

CF 538C (Tourist & #39; s Notes-Greedy)

 

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 lastedNDays, during each day the tourist noted height above the sea level. OnI-Th day height was equal to some integerHI. 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 allI'S from 1N? -? 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 lost ed away. moreover, the numbers in the notes cocould have been distorted. now the tourist wonders what cocould 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 possible height values that meet limits |HI? -?HI? +? 1 |? ≤? 1.

Input

The first line contains two space-separated numbers,NAndM(1? ≤?N? ≤? 108, 1? ≤?M? ≤? 105)-the number of days of the hike and the number of notes left in the journal.

NextMLines contain two space-separated integersDIAndHDI(1? ≤?DI? ≤?N, 0? ≤?HDI? ≤? 108)-the number of the day whenI-Th note was made and height onDI-Th day. It is guaranteed that the notes are given in the chronological order, I. e. for allIFrom 1M? -? 1the following condition holds:DI? DI? +? 1.

Output

If the notes aren't contradicloud, 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 test (s) input
8 22 07 0
Output
2
Input
8 32 07 08 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 inequalityH7 andH8 does not hold, thus the information is inconsistent.


Greedy. Try to climb each segment as much as possible.

 

 

 

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        
         using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i
         
          =0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (100000000)#define MAXM (100000+10)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}typedef long long ll;int n,m;int main(){//freopen("c.in","r",stdin);//freopen(".out","w",stdout);cin>>n>>m;int d1,h1,ans;For(i,m){int d,h;scanf("%d%d",&d,&h);if (i!=1){if (d-d1
          

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.