给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上。

题解里是构造了这样的几个数组,图中表示的很明白了。

首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on[i]表示位于竖线i上两条水平线之间(并不在水平线上)的点数,on2[i]表示位于竖线i上两条水平线之间加上水平线边界上的点数。

所以矩形框上的点数为:

left[j]-left[i]+on[i]+on2[j]

枚举右边界竖线j,j确定后维护on[i]-left[i]的最大值。

 //#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int n, m, y[maxn], on[maxn], on2[maxn], left[maxn]; struct Point
{
int x, y;
bool operator < (const Point& rhs) const
{
return x < rhs.x;
}
}p[maxn]; int solve()
{
sort(p, p + n);
sort(y, y + n);
m = unique(y, y + n) - y; //m为不同y坐标的个数
if(m <= )
return n; int ans = ;
for(int a = ; a < m; ++a)
for(int b = a + ; b < m; ++b)
{
int ymin = y[a], ymax = y[b]; //计算left, on, on2
int k = ; //k记录竖线的条数
for(int i = ; i < n; ++i)
{
if(i == || p[i].x != p[i-].x)
{ //这是一条新的竖线
++k;
on[k] = on2[k] = ;
left[k] = k == ? : left[k-] + on2[k-] - on[k-];
}
if(p[i].y > ymin && p[i].y < ymax)
++on[k];
if(p[i].y >= ymin && p[i].y <= ymax)
++on2[k];
}
if(k <= )
return n; int M = ;
for(int j = ; j <= k; ++j)
{
ans = max(ans, left[j] + on2[j] + M);
M = max(M, on[j] - left[j]);
}
} return ans;
} int main(void)
{
#ifdef LOCAL
freopen("3695in.txt", "r", stdin);
#endif int kase = ;
while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; ++i)
{
scanf("%d%d", &p[i].x, &p[i].y);
y[i] = p[i].y;
}
printf("Case %d: %d\n", ++kase, solve());
}
return ;
}

代码君

小结:大白书上面的题感觉思路之奇妙,仔细琢磨也不能百分百领会其精髓。一定是我等弱渣太弱了,做题太少了。

LA 3695 Distant Galaxy的更多相关文章

  1. UVa LA 3695 - Distant Galaxy 前缀和,状态拆分,动态规划 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. 【UVALive】3695 Distant Galaxy(......)

    题目 传送门:QWQ 分析 好喵啊~~~~ 不会做 正解看蓝书P53吧 代码 #include <cstdio> #include <algorithm> using name ...

  3. UVALive - 3695 Distant Galaxy

    InputThere are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ ...

  4. UVaLive 3695 Distant Galaxy (扫描线)

    题意:给平面上的 n 个点,找出一个矩形,使得边界上包含尽量多的点. 析:如果暴力那么就是枚举上下边界,左右边界,还得统计个数,时间复杂度太高,所以我们考虑用扫描线来做,枚举上下边界, 然后用其他方法 ...

  5. hdu Distant Galaxy(遥远的银河)

    Distant Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. LA3695 Distant Galaxy

    Distant Galaxy https://vjudge.net/problem/UVALive-3695 You are observing a distant galaxy using a te ...

  7. 【poj3141】 Distant Galaxy

    http://poj.org/problem?id=3141 (题目链接) 题意 给出平面上n个点,找出一个矩形,使边界上包含尽量多的点. solution 不难发现,除非所有输入点都在同一行或同一列 ...

  8. uva 1382 - Distant Galaxy

    题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91208#problem/G 题意:  给出平面上的n个点,找出一个矩形,使得边 ...

  9. 【巧妙预处理系列+离散化处理】【uva1382】Distant Galaxy

    给出平面上的n个点,找一个矩形,使得边界上包含尽量多的点. [输入格式] 输入的第一行为数据组数T.每组数据的第一行为整数n(1≤n≤100):以下n行每行两个整数,即各个点的坐标(坐标均为绝对值不超 ...

随机推荐

  1. 【mysql5.6】SQL基础

    我买了本深入浅出MySQL, 记录一下笔记. 一.数据定义语言(DDL) 1.创建数据库  create database name; 2.显示所有的数据库  show databases; 3.选择 ...

  2. oc和swift的混编

    参考:http://blog.sina.com.cn/s/blog_8d1bc23f0102v5tl.html swift中使用oc类的方法 1.创建一个oc.h文件 2.添加需要倒入的oc类的头文件 ...

  3. 深入浅出ES6(十四):let和const

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 回溯到1995年,当Brendan Eich在设计第一版JavaScript时, ...

  4. hdu 4462(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4462 思路:由于数据不大,可以直接将所有的状态表示出来,然后枚举,判断能否将方格全部覆盖. http: ...

  5. win7系统中任务计划程序的使用与查询

    任务计划程序是电脑中的一个好工具,用好了,会让我们使用电脑变的很便捷,具体经验教程如下所示: 工具/原料 装有win7系统的电脑 方法/步骤 在桌面找到“我的电脑”,右击,弹出窗口,找到“管理”,如下 ...

  6. Linux下ps -ef和ps aux的区别及格式详解

    Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区别呢?两者没太大差别,讨论这个问题,要追溯到Unix系统中的两种风格, System V风格和BSD 风格 ...

  7. win7 64 + Ubuntu 14.04.1 64双系统安装,详解UEFI ~ GPT和legacy ~ MBR区别

    win7 64 + Ubuntu 14.04.1 64双系统安装 背景:我的笔记本之前的系统是window 7 64 + Ubuntu 14.04.1,用UEFI引导系统.安装过程是先装的win7,再 ...

  8. Project Euler 101 :Optimum polynomial 最优多项式

    Optimum polynomial If we are presented with the first k terms of a sequence it is impossible to say ...

  9. React-非dom属性-ref标签

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  10. C#枚举硬件设备(升级版)

    原文:C#枚举硬件设备(升级版) 先取设备类型: ; } }