题意:
小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左航行,有的船只向右航行。小明希望拍下这一美丽的风景,并且把尽可能多的船只都完整地拍到一张照片中。
小明位于河的边上,并且可以在河边的任意位置进行拍照,照相机的视野恰好为90度角,只能以垂直于河边的方向进行拍照。河上的船只全都可看作是平行于河边的一条线段,跟河边的距离各不相同,有的正在向左移动,有的正在向右移动,但移动速度恰好都是一样的。小明可以等待恰当的时间让尽量多的船只都走进照相机的视野里,你不需要考虑船只之间会互相遮挡视野的情况。
分析:
如果只有一个方向的船,很easy,只要寻找位置就好,跟时间没什么关系
那么先分析静止状态(实际上这题和时间没多大卵关系)
对于每艘船,有一个刚好能看到的点(x+z),和一个刚好看不到的点(y-z)
将所有向左开的船按(x+z)排序,从右往左扫一遍,把该船对应的y-z入优先队列,如果碰到比他小的y-z就pop出来,存每个船对应的x+z点能看到船数
对向右开的船也做一遍
然后按照向左开的船,扫一遍,维护向右开的比当前位置靠左的观测点能看到的船的数目的最大值,因为向左的观测点一定会在一定时间以后与当前观测点相遇
答案就是max(维护的最大值+当前值)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <map>
#define pii pair<int,int>
#define mp(a,b) make_pair<a,b>
#define xx first
#define yy second
#define mem(a) memset( a, 0, sizeof(a) )
using namespace std;
typedef long long ll;
struct ship
{
int x, y, d, z;
}p[100005];
int n;
pii l[100005], r[100005];
int lans[100005], rans[100005];
//此处可用map实现去重功能,不过实际不需要
bool cmp( pii a, pii b )
{
return a.xx > b.xx;
}
void solve()
{
int i, x, y, z, d, cnt = 1, cnt1 = 1, cnt2 = 1;
cin >> n;
for( i = 1; i <= n; i ++ ){
scanf("%d %d %d %d", &x, &y, &z, &d);
if( x+z < y-z ) continue;
p[cnt].x = x;
p[cnt].y = y;
p[cnt].z = z;
p[cnt].d = d;
pii s(x+z, cnt);
if( d == -1 ){
l[cnt1++] = s;
}
else{
r[cnt2++] = s;
}
cnt ++;
}
n = cnt-1;
sort( l+1, l+cnt1, cmp );// 注意!! sort默认升序排列 优先队列默认大根堆降序排列
sort( r+1, r+cnt2, cmp );
mem(lans);mem(rans);
priority_queue<int>q;
while( !q.empty() ) q.pop();
int tmp = 0;
int res = 0;
for( i = 1; i < cnt1; i ++ ){
int id = l[i].yy;
q.push( p[id].y-p[id].z );
while( !q.empty() && q.top() > l[i].xx ){
q.pop();
}
tmp = q.size();
lans[i] = tmp;
res = max( tmp, res );
}
tmp = 0;
while( !q.empty() ) q.pop();
for( i = 1; i < cnt2; i ++ ){
int id = r[i].yy;
q.push( p[id].y-p[id].z );
while( !q.empty() && q.top() > r[i].xx ){
q.pop();
}
tmp = q.size();
rans[i] = tmp;
res = max( tmp, res );
}
while( !q.empty() ) q.pop();
int j = cnt2-1;
int max1 = 0;
res = 0;
for( i = cnt1-1; i >= 1; i -- ){
while( r[j].xx <= l[i].xx && j >= 1 ){
max1 = max( rans[j], max1 );
j --;
}
res = max( res, max1+lans[i] );
}
printf("%d\n", res);
}
int main()
{
int T, cas = 1;
cin >> T;
while( T -- ){
printf("Case #%d:\n", cas++);
solve();
}
}