Codeforces Round #300--c greedy--tourist ' s Notes

Source: Internet
Author: User

Description

A tourist hiked along the mountain range. The hike lasted for N days, during the tourist noted height above the sea level. On the i-th day height is equal to some integer hI. The tourist pick smooth enough route for he hike, meaning that the between any and 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 a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could has been distorted. Now the tourist wonders, what could is 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 does not represent any possible height values that meet limits | Hi - hi + 1|≤1.

Input

The first line contains the space-separated numbers, n and m (1≤ n ≤108, 1 ≤ m ≤105)-the Number of the days of the hike and the number of notes in the journal.

Nextmlines contain, space-separated integersDIand h D i   ( 1≤ D i n ,  0≤ H D i ≤10< sup class= "Upper-index" >8)  -the number of the day when The  i -th Note was made a nd height on the  D I -th day. It is guaranteed that the notes was given in the chronological order, i.e. for All  I & Nbsp;from 1 To  m -1 the following condition Holds:  D I < d i + 1.

Output

If the notes aren ' t contradictory, print a single integer-the maximum possible height value throughout the whole route.

If the notes does 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  h 7  and  h 8 does not hold, thus the information is inconsistent.

/* Pit point is the first day and the last day to have a max1 judgment first on the date of the longest road from I to I+1 day can go a[i+1].d-a[i].d must go to the road for ABS (A[I+1].H-A[I].H) The remainder is can be relaxed distance, Back and forth in addition to 2 finally plus the last to the position (highest) can start up and down, you can first go up and down and then go up and then esay the impossible situation * * * #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", &AMP;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 greedy--tourist ' s Notes

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.