C. Tourist's Notes
2 seconds
256 megabytes
standard input
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.
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.
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).
8 2
2 0
7 0
2
8 3
2 0
7 0
8 3
IMPOSSIBLE
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*log(n));
1 #include<iostream>
2 #include<algorithm>
3 #include<queue>
4 #include<stdlib.h>
5 #include<stack>
6 #include<stdio.h>
7 #include<string.h>
8 using namespace std;
9 typedef struct node
10 {
11 int day;
12 int hi;
13 } ss;
14 ss ans[1000005];
15 int main(void)
16 {
17 int n,m;
18 while(scanf("%d %d",&n,&m)!=EOF)
19 {
20 int i,j;
21 int flag = 0;
22 for(i = 0; i < m; i++)
23 scanf("%d %d",&ans[i].day,&ans[i].hi);
24 int maxx = -1;
25 for(i = 0; i < m; i++)
26 {
27 if(i == 0)
28 {
29 maxx = max(ans[i].day-1+ans[i].hi,maxx);
30 }
31 else
32 {
33 if(abs(ans[i].hi-ans[i-1].hi)>ans[i].day-ans[i-1].day)
34 flag = 1;
35 else
36 {
37 int l = max(ans[i-1].hi,ans[i].hi);
38 int r = 1e9;
39 while(l<=r)
40 {
41 int mid = (l+r)/2;
42 int x = abs(mid-ans[i].hi);
43 int y = abs(mid-ans[i-1].hi);//printf("%d\n",mid);
44 if(x+y<=ans[i].day-ans[i-1].day)
45 {
46 maxx = max(maxx,mid);
47 l = mid+1;
48 }
49 else r = mid-1;
50 }
51 }
52 } if(i == m-1)
53 {
54 maxx = max(maxx,n-ans[i].day+ans[i].hi);
55 }
56 }if(flag)printf("IMPOSSIBLE\n");
57 else
58 printf("%d\n",maxx);
59 }
60 return 0;
61 }
C. Tourist's Notes的更多相关文章
- 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 ...
- Tourist's Notes CodeForces - 538C (贪心)
A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist no ...
- C. Tourist's Notes
题目链接 题意:n天内登山,相邻两次登山的高度差的绝对值小于等于1,也就是说每次高度变化只能是:0,1,-1.我们已经知道n天中部分天数人所在的山的高度,求最大的登山高度. 输入: n m n 是天 ...
- Codeforces Round #300(A.【字符串,多方法】,B.【思维题】,C.【贪心,数学】)
A. Cutting Banner time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #300 解题报告
呜呜周日的时候手感一直很好 代码一般都是一遍过编译一遍过样例 做CF的时候前三题也都是一遍过Pretest没想着去检查... 期间姐姐提醒说有Announcement也自信不去看 呜呜然后就FST了 ...
- C. Tourist Problem
http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...
- codeforces 340C Tourist Problem(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Tourist Problem Iahub is a big fan of tou ...
- Codeforces Round #198 (Div. 2) C. Tourist Problem
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- Redis——面试官考题
总结: 本文在一次面试的过程中讲述了 Redis 是什么,Redis 的特点和功能,Redis 缓存的使用,Redis 为什么能这么快,Redis 缓存的淘汰策略,持久化的两种方式,Redis 高可用 ...
- SPI详解2
串行外设接口 (SPI) 总线是一种运行于全双工模式下的同步串行数据链路.用于在单个主节点和一个或多个从节点之间交换数据. SPI 总线实施简单,仅使用四条数据信号线和控制信号线(请参见图 1). 图 ...
- JAVA中复制数组的方法
在JAVA里面,可以用复制语句"A=B"给基本类型的数据传递值,但是如果A,B是两个同类型的数组,复制就相当于将一个数组变量的引用传递给另一个数组;如果一个数组发生改变,那么 引用 ...
- 对Javascript中的对象Object改变内存及其变量改变的图解
Object 存储变量时,变量属性的内存改变图解 左边: 对象的内存 中间:变量属性的内存 右边:属性值的内存 [图一]创建一个对象,存obj1 变量--里面存age 属性和属性值--12. ...
- three.js很好玩
能用鼠标拉着转. https://files.cnblogs.com/files/blogs/714801/%E7%A9%BA%E9%97%B4%E5%87%A0%E4%BD%95.7z var po ...
- 零基础学习java------25--------jdbc
jdbc开发步骤图 以下要用到的products表 一. JDBC简介 补充 JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口,各个数据库厂商趋势线这个接口,提 ...
- 02-爬取http://www.allitebooks.org/网站,获取图片url,书名,简介,作者
import requests from lxml import etree from bs4 import BeautifulSoup import json class BookSpider(ob ...
- 【Python】【Basic】MacOS上搭建Python开发环境
1. Python3 1.1. 下载地址:https://www.python.org/downloads/mac-osx/ 1.1.1. PKG包安装: 没啥可说的,点点点,下一步而已,不用手动配置 ...
- ACE_Message_Block实现浅析
ACE_Message_Block实现浅析1. 概述ACE_Message_Block是ACE中很重要的一个类,和ACE框架中的重要模式的实现 如ACE_Reactor, ACE_Proactor, ...
- 【Services】【Web】【LVS】lvs基础概念
1.简介 1.1. 作者:张文嵩,就职于阿里 1.2. LVS是基础四层路由.四层交换的软件,他根据请求报文的目标IP和目标PORT将其调度转发至后端的某主机: 1.3. IPTABLES的请求转发路 ...