[POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

Description

Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to retire by the end of the year. The Goldmine management would like to reward him in acknowledgment of his conscientious work. As a reward Byteman may receive a small lot
- a rectangular piece of the mine‘s area with sides of length s and w parallel to the axes of the coordinate system. He may choose the location of the lot. Of course, a value of the lot depends on the location. The value of the lot is a number of gold nuggets
in the area of the lot (if a nugget lies on the border of the lot, then it is in the area of the lot). Your task is to write a program which computes the maximal possible value of the lot (the value of the lot with the best location). In order to simplify
we assume that the terrain of the Goldmine is boundless, but the area of gold nuggets occurrence is limited.

Task

Write a program which:

1.reads the location of gold nuggets,

2.computes the maximal value of a lot (i.e. the maximal number of gold nuggets on the lot of given size),

3.writes the result .

Input

In the first line there are two positive integers s and w separated by a single space, (1<=s,w<=10 000); they denote the lengths of lot‘s sides - parallel to the OX-axe and OY-axe respectively. There is one positive integer n written in the second line,
(1<=n<=15 000). It denotes the number of nuggets in the area of the Goldmine. In the following n lines there are written the coordinates of the nuggets. Each of these lines consists of two integers x and y, (-30 000<=x,y<=30 000), separated by a single space
and denoting the x and the y coordinate of a nugget respectively.

Output

The out should contain exactly one integer equal to the value of the most valuable lot of a given size.

Sample Input

1 2

12

0 0

1 1

2 2

3 3

4 5

5 5

4 2

1 4

0 5

5 0

2 3

3 2

Sample Output

4

题目大意

给定二维平面上的N个点,寻找一个w*s(高w宽s,被这个点坑好久)的矩形,使得其中包含最多的点。

上海邀请赛B题的题意和这一题类似。

思路

黑书原题。

将每一个点看作是两个事件从左到右进行扫描(可以维护队列控制队头和队尾的差不超过宽度,也可以按照时间顺序排序后进行处理),每个点抽象成长度为矩形高度的线段,线段经过次数最多的点便是结果。

这样就把O(n^2)降维至O(n*log(A)),A为点纵坐标的范围。

在这里可以提交

http://acm.cs.ecnu.edu.cn/problem.php?problemid=1350

ps:太弱了,不仅当场没出这道题而且后来做的时候还W一天,基础还是有待夯实啊。

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
#include <vector>
#include <cstring>
using namespace std;
struct node{
    int t,pos,st;
}ha[200000];
int x1,x2,v;
int _max;
int n,w,h;
int cmp(node x,node y)
{
    return (x.t<y.t);//按照时间先后顺序排序,先删除点再添加点
}
int maxv[400000],addv[400000];
void maintain(int o,int L,int R)
{
    int lc=o<<1;
    int rc=lc+1;
    maxv[o]=0;
    if (R>L)
    {
        maxv[o]=max(maxv[lc],maxv[rc]);
    }
    maxv[o]+=addv[o];
}
void update(int o,int L,int R)
{
    int lc=o<<1;
    int rc=lc+1;
    if (x1<=L&&x2>=R) addv[o]+=v;
    else {
        int M=L+(R-L)/2;
        if (x1<=M) update(lc,L,M);
        if (x2>M) update(rc,M+1,R);
    }
    maintain(o,L,R);
}
void query(int o,int L,int R,int add)
{
    int lc=o<<1;
    int rc=lc+1;
    if (x1<=L&&x2>=R) _max=max(_max,maxv[o]+add);
    else {
        int M=L+(R-L)/2;
        if (x1<=M) query(lc,L,M,addv[o]+add);
        if (x2>M) query(rc,M+1,R,addv[o]+add);
    }
}
int main()
{
    while(~scanf("%d%d%d",&w,&h,&n))
    {
        memset(maxv,0,sizeof(maxv));
        memset(addv,0,sizeof(addv));
        _max=0;
        int o,p,pt=0;
        memset(ha,0,sizeof(ha));
        for (int i=1;i<=n;i++)
            {
                scanf("%d%d",&o,&p);
                ha[++pt].t=o;
                ha[pt].pos=p+30001;
                ha[pt].st=1;
                ha[++pt].t=o+w+1;
                ha[pt].pos=p+30001;
                ha[pt].st=-1;
            }
        sort(ha+1,ha+pt+1,cmp);//将所有时间点排序
        for (int i=1;i<=pt;i++)
        {
            x1=ha[i].pos;
            x2=ha[i].pos+h;
            x2=min(x2,60001);
            v=ha[i].st;
            update(1,1,60001);//更新点数
            if (ha[i].t!=ha[i+1].t||i==pt)//直至同一时间点的所有更新完成之后才输出
                {
                 x1=1;
                 x2=60001;
               <span style="white-space:pre">	</span> query(1,1,60001,0);
            <span style="white-space:pre">	</span>}
        }
        printf("%d\n",_max);
    }
}

[POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

时间: 2024-10-06 21:45:58

[POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线的相关文章

hdu 5091 Beam Cannon(线段树扫描线)

题目链接:hdu 5091 Beam Cannon 题目大意:给定N个点,现在要有一个W?H的矩形,问说最多能圈住多少个点. 解题思路:线段的扫描线,假设有点(x,y),那么(x,y)~(x+W,y+H)形成的矩形,以框的右下角落的位置是可以圈住(x,y) 点,所以N个点即为N个矩形,求覆盖的最大次数,扫描线裸题. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&

HDU5091 Beam Cannon(线段树扫描线)

#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int maxn=40005; const int bw=20000; #define lson(x) ((x)<<1) #define rson(x) (((x)<<1)|1) int lc[maxn<<2],rc[maxn&

线段树+扫描线 HDOJ 5091 Beam Cannon

题目传送门 1 /* 2 题意:给出若干个点的坐标,用一个矩形去覆盖,问最多能覆盖几个点 3 线段树+扫描线:思路是先建一棵以[y, y + h]的树,左右儿子[x, x + w] 4 以这棵树为范围,从左到右扫描,更新点数,x的+1, x+w的-1(超过矩形范围) 5 ans = 每次更新时所覆盖点数的最大值 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <iostream> 10 #includ

hdu 5091 Beam Cannon(线段树+扫描线+离散化)

Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 457    Accepted Submission(s): 175 Problem Description Recently, the γ galaxies broke out Star Wars. Each planet is warring for resou

POI 2001 Goldmine 线段树 扫描线

题目链接 http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=1350 http://main.edu.pl/en/archive/oi/8/kop 求平面n个点(n<=15000),用一个 长宽为 s w的矩阵去套,能套到的最多的点数,在边上的点也算 其实跟之前矩形嵌套求面积类似 (POJ的atlantic)用类似扫描线的做法,把点当做边 (y 到  y值+w),插入线段树,这样就维护了y方向,x方向就用类似队列维护, 在距离大于s的时候,就

HDOJ 5091 Beam Cannon 扫描线

线段树+扫描线: 我们用矩形的中心点来描述这个矩形,然后对于每个敌舰,我们建立一个矩形中心的活动范围,即矩形中心在该范围内活动就可以覆盖到该敌舰.那么我们要求的问题就变成了:任意一个区域(肯定也是矩形的)最多能被矩形覆盖的最大值. Beam Cannon Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 159    Accepted S

hdu 3996 Gold Mine 最大权闭合子图

Gold Mine Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2374    Accepted Submission(s): 514 Problem Description Long long ago, there is a gold mine.The mine consist of many layout, so some are

上海邀请赛之热身赛2_2013成都邀请赛

先写总结. 感觉这次跟scf和sjc组队有种瞬间碉堡了的感觉,虽然是临时组建的队伍凑齐准备去上海参加邀请赛,从这次比赛磨练配合. 今天比赛难度比前天那次的难度低,感觉更适合我们来练习. 话说好像比赛提早了5分钟,我们三个人都不知道,五分钟后一看A题学长已经A了,一想肯定特水...我就没看题,sjc和scf两个看了题,scf就开始敲了,我刚开始负责翻译题,虽然我英语是个渣渣...没办法,没翻译他们几乎做不出题...我就没做题,翻译了B和G.scf敲了貌似好久才完成(赛后重做,特水,基本3分钟就可以

A Computer Graphics Problem 4176 2013上海邀请赛

A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 968    Accepted Submission(s): 688 Problem Description In this problem we talk about the study of Computer Graphics.