【问题描述】
FJ的N头牛在一条无限长的道路上练习赛跑。每头牛均在道路上一个各不相同的起点出发,以某个速度匀速前进。
为了防止跑得快的牛在超过跑得慢的牛时发生碰撞,FJ把道路分成许多条跑道,这样,处在同一条跑道上牛永远不会占据同一个位置。
FJ还规定,牛不能更换跑道,也不能改变速度。
FJ想知道,在一次持续T分钟的赛跑中,他至少需要划分出多少条跑道。
【输入格式】
第1行:2个整数N(1 <= N <= 100,000)和T(1 <= T <= 1,000,000,000)
接下来N行,每行2个整数,表示一头牛的初始位置和速度。数据以位置递增的顺序给出。位置为非负整数,速度为正整数。
【输出格式】
第1行:1个整数,表示在T分钟(包含第T分钟)的赛跑中,至少需要多少条跑道
【输入样例】
5 3
0 1
1 2
2 3
3 2
6 1
【输出样例】
3
【数据范围】
1 <= N <= 100,000
1 <= T <= 1,000,000,000
本来以为直接模拟找,但是花式WA
后来还是查找A了这道题,详解看程序
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define maxn 100005
using namespace std;
struct data
{
int x,y;
friend bool operator< (data a,data b)
{
a.x<b.x;
}
}a[maxn];
bool cmp(long long a,long long b)
{
return a>b;
}
long long n,m;
long long end[maxn],d[maxn];
void in()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y;
end[i]=a[i].x+a[i].y*m; //计算每只奶牛的终点
}
}
void task()
{
int cnt=0;
d[++cnt]=end[1];
for(int i=2;i<=n;i++)
{
//因为起点有序,所以只要找到一个终点小于i的奶牛,就不用新建跑道
int p=upper_bound(d+1,d+cnt+1,end[i],cmp)-d; //找到小于于i的终点的第一个跑道
if(p>cnt) d[++cnt]=end[i]; //新建一条跑道
else d[p]=end[i]; //不用新建跑道则更新该跑道的终点
}
printf("%d\n",cnt);
}
int main()
{
freopen("in.txt","r",stdin);
in();
task();
return 0;
}