题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1696

题意

平面上有n个整数点,找一个矩形,使得边界上包含尽量多的点。

思路

如刘书

首先可以按照x对所有点排序,然后枚举矩形左右边界

确定左右边界之后,接下来就可以枚举下边界,直接求最优上边界。

当左右下边界确定后,就能知道图中粉色部分上有多少个点,但这样离求出带上边界的矩形上有多少个点还差一点。如图,假如上边界是淡绿色边,那么还需要去掉紫色的两段左右边界上不属于矩形的前缀,再加上上边界上橙色的那段。

如何求最优上边界呢?明显,最优上边界和下边界无关,只与自身的x和左右边界有关。所以我们可以直接记录目前为止的最优上边界-也就是记录橙色部分的点减去紫色前缀中点后还剩下点的最大值-该最大值对应的就是最优上边界。

假设左边界对应ymin,右边界ymax,那么对于一条横边x = x0,设cntcorner为左右边界与横边相交位置上存在的点,cnt为左右边界夹住(不包括相交位置)的点。prefix为cntcorner累计值,也即y=ymin或ymax,x <= x0的点数,那么粉色的部分=prefix + cnt,

设另外一条横边x = x1, x1 < x0为上边界,对应cnt', cntcorner'和prefix‘,那么此时矩形上的点就是prefix + cnt + cnt' - prefix' - cntcorner'

cnt' - prefix' - cntcorner'的最大值对应最优上边界。

代码

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
const int MAXN = 1e2 + ;
typedef pair<int, int> MyPair;
MyPair pt[MAXN];
int y[MAXN];
int n; int check(int ymin, int ymax) {
int reserve = , ans = ;
for (int i = , ygap = ; i < n;) {
if (pt[i].second < ymin || pt[i].second > ymax) {
i++; continue;
}
int x = pt[i].first;
int cnt = , cntcorner = ;
for (int j = i; j < n && pt[j].first == x && pt[j].second <= ymax && pt[j].second >= ymin; i++, j++) {
if (pt[j].second == ymin || pt[j].second == ymax)cntcorner++;
else cnt++;
}
ans = max(ans, reserve + cnt + cntcorner + ygap);
reserve = max(reserve, cnt - ygap);
ygap += cntcorner;
}
return ans;
} int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
//int T;
// scanf("%d", &T);
for (int ti = ;scanf("%d", &n) == && n; ti++) {
for (int i = ; i < n; i++) {
scanf("%d%d", &pt[i].first, &pt[i].second);
y[i] = pt[i].second;
}
sort(pt, pt + n);
sort(y, y + n);
int ynum = ;
for (int i = ; i < n; i++) {
if (i && y[i] == y[i - ]) {
continue;
}
else {
y[ynum++] = y[i];
}
}
int ans = -;
if (ynum <= )ans = n;
else {
for (int i = ; i < ynum; i++) {
for (int j = i + ; j < ynum; j++) {
ans = max(ans, check(y[i], y[j]));
}
}
}
printf("Case %d: %d\n", ti, ans);
} return ;
}

UVa LA 3695 - Distant Galaxy 前缀和,状态拆分,动态规划 难度: 2的更多相关文章

  1. LA 3695 Distant Galaxy

    给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on ...

  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. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...

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

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

  7. LA3695 Distant Galaxy

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

  8. BZOJ_4197_[Noi2015]寿司晚宴_状态压缩动态规划

    BZOJ_4197_[Noi2015]寿司晚宴_状态压缩动态规划 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被 ...

  9. 状态压缩动态规划 状压DP

    总述 状态压缩动态规划,就是我们俗称的状压DP,是利用计算机二进制的性质来描述状态的一种DP方式 很多棋盘问题都运用到了状压,同时,状压也很经常和BFS及DP连用,例题里会给出介绍 有了状态,DP就比 ...

随机推荐

  1. 力扣(LeetCode) 509. 斐波那契数

    斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 F(N) = F(N ...

  2. python3.5学习第二章(1)标准库,bytes

    一.输出python库的路径: 1.sys标准库 import sysprint(sys.path) 结果: 'E:\\python练习\\python35学习\\Day2', 'E:\\python ...

  3. sort-插入排序

    void sort_insertion(vector<int> &v) { for(int i=1;i<v.size();i++) { for(int j=i;j>0; ...

  4. 类似于placehoder效果的图标展示

    在做app开发的时候往往会有那个注册登录啊,什么的页面,里面就会包含这那种类似于placeholder的效果的图标,当时我也是和ios和安卓混合开发一款app里面的页面全是我写,最开始就是登陆啊,注册 ...

  5. jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional, 原 Inferred type 'S' for type parameter 'S' is not within its bound;

    jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional 在使用springboot 方法报错: Inferred type 'S' for type para ...

  6. 雷林鹏分享:C# 封装

    C# 封装 封装 被定义为"把一个或多个项目封闭在一个物理的或者逻辑的包中".在面向对象程序设计方法论中,封装是为了防止对实现细节的访问. 抽象和封装是面向对象程序设计的相关特性. ...

  7. hihocoder-1419 后缀数组四·重复旋律4 求连续重复次数最多的子串

    对于重复次数,如果确定了重复子串的长度len,那重复次数k=lcp(start,start+len)/len+1.而暴力枚举start和len的复杂度是O(n^2),不能接受.而有一个规律,若我们只枚 ...

  8. python模块--time & datetime

    time模块 #获取当前时间的时间戳 import time >>> time.time() 1535004894.0959966 #日期字符串转化成时间戳 >>> ...

  9. C++的字符串多行输入

    #include<iostream> using namespace std; int main() { int r, c; char grid[50][51]; cout << ...

  10. python 小练习 5

    Py从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992, 这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和 ...