Tourist's Notes CodeForces - 538C (贪心)
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).
Examples
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天,每一天都记录自己在海拔多高,但是他的记录从小偷偷走了,只剩下m天的记录,
他知道每相邻的两天海拔高度相差不超过1,现在让你计算他最大可能能在多高。
思路:
根据读入的具体日期的高度差来判断是否合理并且查出最大的高度可能,
注意第一天和第n天并不是一定在0海拔。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n; int m;
pii a[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin >> n >> m;
repd(i, 1, m)
{
cin >> a[i].fi >> a[i].se;
}
sort(a + 1, a + 1 + m);
m = unique(a + 1, a + 1 + m) - a - 1;
int isok = 1;
int x, y, cha;
repd(i, 1, m - 1)
{
x = abs(a[i].se - a[i + 1].se);
y = a[i + 1].fi - a[i].fi;
// cout<<x<<" "<<y<<endl;
if (x > y)
{
isok = 0;
break;
}
}
int ans = 0;
if (isok)
{
repd(i, 1, m - 1)
{
x = max(a[i].se, a[i + 1].se);
y = min(a[i].se, a[i + 1].se);
cha = a[i + 1].fi - a[i].fi - 1;
cha -= x - y;
ans = max(ans, x);
ans = max(ans, (cha + 1) / 2 + x);
}
x = n - a[m].fi + a[m].se;
ans = max(ans, x);
x = a[1].fi - 1 + a[1].se;
ans = max(ans, x);
cout << ans << endl;
} else
{
cout << "IMPOSSIBLE" << endl;
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Tourist's Notes CodeForces - 538C (贪心)的更多相关文章
- Codeforces Round #300 C. Tourist's Notes 水题
C. Tourist's Notes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/pr ...
- Codeforces 538 C. Tourist's Notes
C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- C. Tourist's Notes
C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
随机推荐
- 禁止crontab -r清空定时任务列表误操作
关于crontab 1.附件cron.sh放/usr/bin目录下面 2.在/etc/bashrc文件末尾添加 alias crontab='/usr/bin/cron.sh' 或者在当前root用户 ...
- PJzhang:python基础进阶的10个疗程-two
猫宁!!! 第2节:python基本图形绘制 保留字是python基础语法的支撑 默写python代码是件挺恐怖的事情!!! 2008年android操作系统诞生 计算时代 编程语言也是一个江湖 C语 ...
- Git 的配置
一般配置用户层面配置 Git 的配置从上到下分三层 system/global/local,使用三个不同参数进行设置,每个层次的配置存储在不同的位置, 1)./etc/gitconfig 文件:包含了 ...
- zping ping包工具20180605.exe测试版
链接: https://pan.baidu.com/s/1WB3BZn0r9n4DRU_8bNC65g 提取码: mybi zping的第一个exe版本由于未对兼容性进行测试,使用python3.6编 ...
- 第十三章 字符串 (四)之Scanner类
一.Scanner简述 Scanner扫描器类本质上是由正则表达式实现的,可以接受任何能产生数据的数据源对象,默认以空白符进行分词(包括\n等),使用各种next方法进行扫描匹配,获取匹配的数据. 二 ...
- POSTGRESQL日期函数大全
一:各个数据中的连接符 SqlServer的连接符:+(加号) Sqlite的连接符:.(点) PostgreSQL的连接符:||(或) 二: 今天在PostgreSQL计算日期的时候,翻了一些资料, ...
- JS中的继承(上)
JS中的继承(上) 学过java或者c#之类语言的同学,应该会对js的继承感到很困惑--不要问我怎么知道的,js的继承主要是基于原型(prototype)的,对js的原型感兴趣的同学,可以了解一下我之 ...
- 怎样理解 instanceof
instanceof 运算符用来判断一个对象在其原型链中是否存在一个构造函数的 prototype 属性. 也就是说, instanceof 判断的实际上是某个对象是否为某个构造函数的实例, 因为es ...
- 20-Perl 正则表达式
1.Perl 正则表达式正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等.Pe ...
- Spring 配置文件注入
一.Spring配置文件注入 package com.zxguan.demo; public class Person { private String name; private int age; ...