HDU 4007 Dave(离散化)

Dave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4095    Accepted Submission(s): 1390


Problem Description
Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there are N (1<=N<=1000) people on the ground. Now he wants to know how many people will be in a square with the length of R (1<=R<=1000000000). (Including boundary).
 
Input
The input contains several cases. For each case there are two positive integers N and R, and then N lines follow. Each gives the (x, y) (1<=x, y<=1000000000) coordinates of people.
 
Output
Output the largest number of people in a square with the length of R.
 
Sample Input
3 2
1 1
2 2
3 3
Sample Output
3
HITE If two people stand in one place, they are embracing.
 
结构体离散化,先对横坐标排序,选定一个横坐标x作为初始值,x+R作为结束值,接下来的点凡是在这个范围内的,把点的纵坐标作为x值存到另一个结构体中y所对应的结构体的y值存为1,找到这里时直接加y值,将y+R-1所对应的结构体的y值标为-1,查找到这里是,说明,已超出了初始y值所能达到的最大边界,减去的即是初始y所对应得点
#include <iostream> 
#include <algorithm> 
#include <cstring> 
#include <cstdio>
#include <vector> 
#include <queue> 
#include <cstdlib> 
#include <iomanip>
#include <cmath> 
#include <ctime> 
#include <map> 
#include <set> 
using namespace std; 
#define lowbit(x) (x&(-x)) 
#define max(x,y) (x>y?x:y) 
#define min(x,y) (x<y?x:y) 
#define MAX 100000000000000000 
#define MOD 1000000007
#define pi acos(-1.0) 
#define ei exp(1) 
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int n,R;
struct Node
{
    int x;
    int y;
}node[1005],pos[1005];
bool cmp(Node a,Node b)
{
    return a.x<b.x;
}
int main()
{
    while(scanf("%d%d",&n,&R)!=EOF)
    {
        int ans=0,cnt=0,inf=0,start,end,tot=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&node[i].x,&node[i].y);
        }
        sort(node,node+n,cmp);
        for(int i=0;i<n;i++)
        {
            tot=0;
            start=node[i].x,end=node[i].x+R;
            for(int j=i;j<n;j++)
            {
                if(node[j].x>=start && node[j].x<=end)
                {
                    pos[tot].x=node[j].y;
                    pos[tot++].y=1;
                    pos[tot].x=node[j].y+R+1;
                    pos[tot++].y=-1;
                }
            }
            sort(pos,pos+tot,cmp);
            for(int j=0;j<tot;j++)
            {
                cnt+=pos[j].y;
                inf=max(inf,cnt);
            }
            ans=max(inf,ans);
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7300491.html