给出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. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  2. Hadoop 系统配置 map 100% reduce 0%

    之前在本地配置了hadoop伪分布模式,hdfs用起来没问题,mapreduce的单机模式也没问题. 今天写了个程序,想在伪分布式上跑一下mapreduce,结果出现 map 100% reduce ...

  3. css 内联元素inline 行框全解

    首先看一篇文章: CSS框模型:一切皆为框 — 从行框说起 一 行框 看图说话 上图代表了框模型中的行框.line-height 属性设置行间的距离(行高).该属性会影响行框的布局.在应用到一个块级元 ...

  4. 解决IE不支持position:fixed问题

    #box { /* 非IE6浏览器使用固定元素 */ position:fixed; top:0; left:0; /* IE6改为绝对定位,并通过css表达式根据滚动位置更改top的值 */ _po ...

  5. SGU 107

    107. 987654321 problem time limit per test: 0.25 sec. memory limit per test: 4096 KB For given numbe ...

  6. Manacher算法 O(n) 求最长回文子串

    转自:http://bbs.dlut.edu.cn/bbstcon.php?board=Competition&gid=23474 其实原文说得是比较清楚的,只是英文的,我这里写一份中文的吧. ...

  7. Codeforces Round #336 (Div. 2) D. Zuma 区间dp

    D. Zuma   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...

  8. Shell练习 统计单词个数,降序排列

    原文:https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of e ...

  9. hdu 4762 Cut the Cake (大数乘法)

    猜公式: ans=n/m^(n-1) #include<stdio.h> #include<string.h> struct BigNum { ]; int len; }; i ...

  10. 关于SIGPIPE导致的程序退出

    http://www.cppblog.com/elva/archive/2008/09/10/61544.html 收集一些网上的资料,以便参考: http://blog.chinaunix.net/ ...