4723: [POI2017]Flappy Bird
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 246 Solved: 99
[ Submit][ Status][ Discuss]
Description
《飞扬的小鸟》是一款风靡的小游戏。在游戏中,小鸟一开始位于(0,0)处,它的目标是飞到横坐标为X的某个位置
上。每一秒,你可以选择点击屏幕,那么小鸟会从(x,y)飞到(x+1,y+1),或者不点击,那么小鸟会飞到(x+1,y-1)
。在游戏中还有n个障碍物,用三元组(x[i],a[i],b[i])描述,表示在直线x=x[i]上,y<=a[i]或者y>=b[i]的部分
都是障碍物,碰到或者擦边都算游戏失败。请求出小鸟从(0,0)飞到目的地最少需要点击多少次屏幕。
Input
第一行包含两个整数n(0<=n<=500000),X(1<=n<=10^9)。
接下来n行,每行三个整数x[i],a[i],b[i](0<x[i]<X,-10^9<=a[i]<b[i]<=10^9)。
数据保证x[i]<x[i+1]。
Output
如果无论如何都飞不到目的地,输出NIE,否则输出点击屏幕的最少次数。
Sample Input
4 11
4 1 4
7 -1 2
8 -1 3
9 0 2
4 1 4
7 -1 2
8 -1 3
9 0 2
Sample Output
5
HINT
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 5E5 + 50;
typedef long long LL;
typedef double DB;
const LL INF = 1E16;
int n,X;
LL L,R,a[maxn],b[maxn],x[maxn];
int getint()
{
char ch = getchar(); int ret = 0,A = 1;
while (ch < '0' || '9' < ch)
{
if (ch == '-') A = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret * A;
}
int main()
{
n = getint(); X = getint();
for (int i = 1; i <= n; i++)
x[i] = getint(),a[i] = getint(),b[i] = getint();
++n; x[n] = X; a[n] = -INF; b[n] = INF;
for (int i = 1; i <= n; i++)
{
LL l = floor((DB)(a[i] + x[i]) / 2.00) + 1LL;
LL r = ceil((DB)(b[i] + x[i]) / 2.00) - 1LL;
LL delta = x[i] - x[i-1];
if (R + delta < l) {puts("NIE"); return 0;}
if (L > r) {puts("NIE"); return 0;} if (L < l) L = l;
if (R > r) R = r; else R = min(R + delta,r);
if (L > R) {puts("NIE"); return 0;}
}
cout << L;
return 0;
}