UVa LA 3695 - Distant Galaxy 前缀和,状态拆分,动态规划 难度: 2
题目
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的更多相关文章
- LA 3695 Distant Galaxy
给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on ...
- 【UVALive】3695 Distant Galaxy(......)
题目 传送门:QWQ 分析 好喵啊~~~~ 不会做 正解看蓝书P53吧 代码 #include <cstdio> #include <algorithm> using name ...
- UVALive - 3695 Distant Galaxy
InputThere are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ ...
- UVaLive 3695 Distant Galaxy (扫描线)
题意:给平面上的 n 个点,找出一个矩形,使得边界上包含尽量多的点. 析:如果暴力那么就是枚举上下边界,左右边界,还得统计个数,时间复杂度太高,所以我们考虑用扫描线来做,枚举上下边界, 然后用其他方法 ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
- hdu Distant Galaxy(遥远的银河)
Distant Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- LA3695 Distant Galaxy
Distant Galaxy https://vjudge.net/problem/UVALive-3695 You are observing a distant galaxy using a te ...
- BZOJ_4197_[Noi2015]寿司晚宴_状态压缩动态规划
BZOJ_4197_[Noi2015]寿司晚宴_状态压缩动态规划 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被 ...
- 状态压缩动态规划 状压DP
总述 状态压缩动态规划,就是我们俗称的状压DP,是利用计算机二进制的性质来描述状态的一种DP方式 很多棋盘问题都运用到了状压,同时,状压也很经常和BFS及DP连用,例题里会给出介绍 有了状态,DP就比 ...
随机推荐
- leecode第七十题(爬楼梯)
class Solution { public: int climbStairs(int n) { vector<unsigned long long> num;//斐波那契数列 num. ...
- openstack 重启服务命令
重启openstack的整个服务openstack-service restart 1. 重启dashboardservice httpd restart service memcached rest ...
- AtCoder Beginner Contest 110 D - Factorization
D - Factorization 思路:把相同的质因子看成相同的小球,求把这些小球放进n个盒子里的方案数. 代码: #pragma GCC optimize(2) #pragma GCC optim ...
- selenium Grid2 分布式自动化测试环境搭建
一.Selenium Server 环境配置 1.selenium grid的组成与作用:由一个集线器hub和多个客户机node组成,如果你的程序需要在不用的浏览器,不同的操作系统上测试,而且比较多的 ...
- Basic Calculator 基本计算器
2018-09-27 22:02:36 一.Basic Calculator II 问题描述: 问题求解: sign用来保存前一个符号,用num来记录数字,如果碰到一个符号或者到达结尾,则需要进行入栈 ...
- 通过wifi 连接 adb 到 手机
网上很多文章都需要先用 usb 线连接先做一下设置,然后才能通过下面的方法连接 julian@julian-ThinkPad-T450:~/tools/android_sdk/platform-too ...
- [Spring] 04 Denpendency Injection
DI Dependency Injection 依赖注入:从程序代码中移除依赖关系的一种设计模式. 这样就可以更容易地管理和测试应用程序. DI使我们的程序编码 loosely coupled.松耦合 ...
- android -------- 混淆打包报错(warning - InnerClass annotations are missing corresponding EnclosingMember annotations)
最近做Android混淆打包遇到一些问题,Android Sdutio 3.1 版本打包的 错误如下: Android studio warning - InnerClass annotations ...
- Spring Boot 针对 Java 开发人员的安装指南
Spring Boot 可以使用经典的开发工具或者使用安装的命令行工具.不管使用何种方式,你都需要确定你的 Java 版本为 Java SDK v1.8 或者更高的版本.在你开始安装之前,你需要确定你 ...
- 自定义Exception异常
自定义异常构建 首先写一个自定义异常,继承Exception,代码如下 public class NoMappingParamString extends Exception { /*无参构造函数*/ ...