Codeforces 538C. Tourist's Notes

Posted TOTOTOTOTZZZZZ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 538C. Tourist's Notes相关的知识,希望对你有一定的参考价值。

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).

将所有笔记排序好,然后判断相邻的情况是否OK,如果不ok直接impossible,如果ok就二分找答案

技术分享
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define SIZE 100005




LL n,m;
struct note{
  LL d,h;
};
bool cmp(note n1,note n2){
  return (n1.d < n2.d);
}
note notes[SIZE];

bool ok(LL now,LL nowi){
  LL befd = notes[nowi-1].d,befh = notes[nowi-1].h;
  LL nowd = notes[nowi].d,nowh = notes[nowi].h;
  if ((now - nowh) + (now - befh) <= nowd - befd){
    return true;
  }
  return false;
}

LL search(LL l,LL r,LL nowi){
  // cout << l << r << endl;
  if (l == r){
    return l;
  }
  if (l+1 == r){
    if (ok(r,nowi)) return r;
    return l;
  }

  LL mid = l + (r - l) / 2;
  if (ok(mid,nowi)){
    return search(mid,r,nowi);
  }
  else {
    return search(l,mid-1,nowi);
  }
}

int main()
{
  // freopen("test.in","r",stdin);
  cin >> n >> m;
  for (int i=1;i<=m;i++){
    cin >> notes[i].d >> notes[i].h;
  }
  sort(notes+1,notes+1+m,cmp);
  int ok = 1;
  LL res = -1;
  for (int i=2;i<=m;i++){
    // cout << i << endl;
    LL now = notes[i].d,nowh = notes[i].h;
    LL bef = notes[i-1].d,befh = notes[i-1].h;
    if ( (now-bef) < abs(nowh-befh)){
      ok = 0; break;
    }
    LL nowres = search(max(nowh,befh),max(nowh,befh)+now-bef,i);
    res = max(nowres,res);
  }
  res = max(res,notes[m].h + (n-notes[m].d));
  res = max(res,notes[1].h + notes[1].d - 1);
  if (ok){
    cout << res;
  }
  else
    cout << "IMPOSSIBLE";
  return 0;
}
View Code

 

以上是关于Codeforces 538C. Tourist's Notes的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Global Round 1

CodeForces 732F Tourist Reform

Codeforces Round #538 (Div. 2) (CF1114)

CodeForces Contest #1114: Round #538 (Div. 2)

Codeforces Round #377 (Div. 2)F - Tourist Reform

codeforces CF732F Tourist Reform Tarjan边双连通分量