CodeForce 538C 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 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 ≤ 1081 ≤ 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 ≤ n0 ≤ 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 - 1the 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 test(s)
input
8 2
2 0
7 0
output
2
input
8 3
2 0
7 0
8 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 inequality between h7 and h8 does not hold, thus the information is inconsistent.

题意:一个旅行者在外旅行n天,第i天所在地方的海拔为hi,相邻两天的海拔之差不超过1。给出部分天数的海拔,问这个人在这n天中,所到地方的最高海拔是多少?如果根据给出的数据不符合题意描述,即出现相邻两天的海拔之差超过1,输出“IMPOSSIBLE”。

分析:对于给出第一条数据(a,b),假设从第一天到第a天一直减1,则第一天的海拔为 b + (a - 1);从第pre_a天海拔为pre_b,到第a天海拔为b,假设中间的最高海拔为x,则可列出不等式(x - pre_b) + (x - b) <= a - pre_a,整理得x = (pre_b + b + a - pre_a)/ 2;从最后一条数据(a,b)到第n天,假设一直加1,则第n天的海拔为b+(n-a)。每次求出一个值,求出他们的最大值即可。

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main() {
    int n, m, a, b;
    while(cin >> n >> m) {
        cin >> a >> b;
        int pre_a = a, pre_b = b;
        int ans = b + a - 1; // 假设从第一天到第a天一直减1
        bool flag = true;
        for(int i = 1; i < m; i++) {
            cin >> a >> b;
            if(abs(b - pre_b) > a - pre_a) flag = false; //相邻两天的海拔之差大于1
            int tmp = (pre_b + b + a - pre_a) / 2;
            ans = max(ans, tmp);
            pre_a = a;
            pre_b = b;
        } //假设从第pre_a天到第a天的最大高度为h,则(h-pre_b)+(h-b)<=(a-pre_a),整理的h=(pre_b+b+a-pre_a)/2
        int tmp = pre_b + (n - pre_a); //从第pre_b天到第n天一直加1
        ans = max(ans, tmp);
        if(flag) cout << ans << endl;
        else cout << "IMPOSSIBLE" << endl;
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值