zoukankan      html  css  js  c++  java
  • C. Tourist's Notes

    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 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 ≤ 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 - 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*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
    ",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
    ");
    57         else
    58         printf("%d
    ",maxx);
    59     }
    60     return 0;
    61 }

    代码库

  • 相关阅读:
    js判断选择时间不能小于当前时间的代码
    shell脚本编程之for语句、if语句使用介绍
    linux命令 chattr超级权限控件
    教你配置linux服务器登陆欢迎信息
    PHP基础入门教程 PHP循环函数
    php获取客户端ip地址
    PHP获取域名、IP地址的方法
    两日期间的间隔
    mysql 案例 ~ pt-archiver 归档工具的使用
    mysql 案例 ~ pt修复工具的使用
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/6002327.html
Copyright © 2011-2022 走看看