题目链接:

拍照

Time Limit: 6000/3000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

Problem Description
 
小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左航行,有的船只向右航行。小明希望拍下这一美丽的风景,并且把尽可能多的船只都完整地拍到一张照片中。

小明位于河的边上,并且可以在河边的任意位置进行拍照,照相机的视野恰好为90度角,只能以垂直于河边的方向进行拍照。河上的船只全都可看作是平行于河边的一条线段,跟河边的距离各不相同,有的正在向左移动,有的正在向右移动,但移动速度恰好都是一样的。小明可以等待恰当的时间让尽量多的船只都走进照相机的视野里,你不需要考虑船只之间会互相遮挡视野的情况。

![http://acm.hdu.edu.cn/data/images/C715-1003-1.jpg](http://acm.hdu.edu.cn/data/images/C715-1003-1.jpg)

 
Input
 
第一行为T,表示输入数据组数。

下面T组数据,对于每组数据:

第一行是一个数n(1≤n≤10^4),表示船只的数量。

接下来n行,每行四个整数
x,y,z,d(−10^6≤x<y≤10^6,1≤z≤10^4),表示船只的左端点位置、右端点位置、距离河边的距离,以及航行的方向。d为−1表示向左航行,1表示向右航行。

 
Output
 
对第i组数据,输出

Case #i:

然后输出一行,仅包含一个整数,表示最多可以拍到多少完整的船只。

 
Sample Input
 
3
2
1 3 1 1
2 4 1 -1
2
1 3 1 -1
2 4 1 1
1
1 4 1 1
 
Sample Output
 
Case #1:
2
Case #2:
1
Case #3:
0
 
题意:
 
 
思路:
 
[y-z,x+z]为岸上能看到这艘船的位置,这是一条线段,update(l,1)update(r+1,-1),然后就可以对于每一点可以query这点能看到的船的数量了;
范围太大,就可以把 这些线段离散化,只保留他们的端点,然后把这些端点的范围离散到[1,2*N]的范围内,大小的关系不变;
当然向左走和向右走的船要分开update,query;
最后得到答案后就要找最大值了,对于任意的一点,它能看到的最大的船的数目是这点后面所有点能看到的向左走的船的最大数目+这点之前所有点能看到的向右走的船的最大数目;
 
还有就是这个没必要用树状数组,可以直接最后扫一遍就可以得到每个点的能看到的船的数目了;
 
 
 
AC代码:
 
 
//#include <bits/stdc++.h>
//有树状数组的
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e4+; int n,sum[][*N],a[*N],b[*N],pre[*N],nex[*N];
int num,mmax; struct node
{
int l,r,d;
}po[*N]; int findpos(int x)
{
int l=,r=mmax;
while(l<=r)
{
int mid=(l+r)>>;
if(b[mid]<x)l=mid+;
else r=mid-;
}
return l;
} int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int flag)
{
while(x<=mmax)
{
sum[flag][x]+=y;
x+=lowbit(x);
}
}
int query(int x,int flag)
{
int s=;
while(x>)
{
s+=sum[flag][x];
x-=lowbit(x);
}
return s;
} int main()
{
int t;
read(t);
int Case=;
while(t--)
{
mst(sum,);
int x,y,z,d,cnt=;
num=;
mmax=; read(n);
Riep(n)
{
read(x),read(y),read(z),read(d);
if(x+z<y-z)continue;
po[cnt].l=y-z;
po[cnt].r=x+z;
if(d==-)po[cnt++].d=;
else po[cnt++].d=d;
a[++num]=y-z;
a[++num]=x+z;
}
sort(a+,a+num+);
b[++mmax]=a[];
for(int i=;i<=num;i++)
{
if(a[i]!=a[i-])b[++mmax]=a[i];
} for(int i=;i<cnt;i++)
{
po[i].l=findpos(po[i].l);
po[i].r=findpos(po[i].r);
update(po[i].l,,po[i].d);
update(po[i].r+,-,po[i].d);
}
for(int i=;i<=mmax;i++)
{
pre[i]=max(query(i,),pre[i-]);
nex[i]=query(i,);
}
int ans=;
nex[mmax+]=;
for(int i=mmax;i>;i--)
{
nex[i]=max(nex[i+],nex[i]);
ans=max(ans,pre[i]+nex[i]);
}
printf("Case #%d:\n",Case++);
print(ans);
}
return ;
}
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e4+; int n,sum[][*N],a[*N],b[*N],pre[*N],nex[*N],sum1[*N],sum2[*N];
int num,mmax; struct node
{
int l,r,d;
}po[*N]; int findpos(int x)
{
int l=,r=mmax;
while(l<=r)
{
int mid=(l+r)>>;
if(b[mid]<x)l=mid+;
else r=mid-;
}
return l;
} int main()
{
int t;
read(t);
int Case=;
while(t--)
{
mst(sum,);
int x,y,z,d,cnt=;
num=;
mmax=; read(n);
Riep(n)
{
read(x),read(y),read(z),read(d);
if(x+z<y-z)continue;
po[cnt].l=y-z;
po[cnt].r=x+z;
if(d==-)po[cnt++].d=;
else po[cnt++].d=d;
a[++num]=y-z;
a[++num]=x+z;
}
sort(a+,a+num+);
b[++mmax]=a[];
for(int i=;i<=num;i++)
{
if(a[i]!=a[i-])b[++mmax]=a[i];
} for(int i=;i<cnt;i++)
{
po[i].l=findpos(po[i].l);
po[i].r=findpos(po[i].r);
sum[po[i].d][po[i].l]++;
sum[po[i].d][po[i].r+]--;
}
for(int i=;i<=mmax;i++)
{
sum1[i]=sum1[i-]+sum[][i];
sum2[i]=sum2[i-]+sum[][i];
nex[i]=sum1[i];
pre[i]=max(pre[i-],sum2[i]);
}
int ans=;
nex[mmax+]=;
for(int i=mmax;i>;i--)
{
nex[i]=max(nex[i+],nex[i]);
ans=max(ans,pre[i]+nex[i]);
}
printf("Case #%d:\n",Case++);
print(ans);
}
return ;
}

hdu-5714 拍照(二分)的更多相关文章

  1. HDU 5714 拍照 前缀和

    拍照 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5714 Description 小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左 ...

  2. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  3. hdu 2413(最大匹配+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...

  4. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  5. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...

  6. HDU 1025 DP + 二分

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025 求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn) f[i]存储长度为i的最小 ...

  7. hdu 2289 要二分的杯子

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2289 大意是 一个Cup,圆台形,给你它的顶部圆的半径,底部圆的半径,杯子的高度,和此时里面装的水的体 ...

  8. HDU 1025 LIS二分优化

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...

  9. HDU 5200 Trees 二分

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5200 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

随机推荐

  1. MFC打开文件对话框

    { CString FilePathName; CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 if(dlg.DoModal()==IDO ...

  2. JavaScript寻踪OOP之路

    上一集中,重点介绍了谁动了你的代码.这里先总结一下:咱们的代码从敲下来到运行出结果,经历了两个阶段:分析期与运行期.在分析期,JavaScript分析器悄悄动了我们的代码:在运行期,JavaScrip ...

  3. CSS超出2行省略号

    overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; line- ...

  4. cocos2d-x android黑屏后返回游戏卡顿

    转自:http://blog.csdn.net/wolfking_2009/article/details/8824931 2013年5月17日更新:对于之前说的资源释放问题,cocos2d-x 2. ...

  5. C++的优秀特性2:inline 函数

    (转载请注明原创于潘多拉盒子) Inline函数是C++的一个很小的特性,在不计较效率的情况下,这个特性似乎可有可无.然而,C++天生是为最为广泛的应用场景设计的,因此,总会有关于效率的问题.其实,除 ...

  6. Microchip 125 kHz RFID System Design Guide

    Passive RFID Basics - AN680 INTRODUCTION Radio Frequency Identification (RFID) systems use radio fre ...

  7. PL/pgSQL学习笔记之三

    http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1.2. Supported Argument and Resul ...

  8. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  9. Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学

    E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...

  10. [Angular2 Form] Use RxJS Streams with Angular 2 Forms

    Angular 2 forms provide RxJS streams for you to work with the data and validity as it flows out of t ...