1382 - Distant Galaxy
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4128
You are observing a distant galaxy using a telescope above the Astronomy Tower, and you think that a rectangle drawn in that galaxy whose edges are parallel to coordinate axes and contain maximum star systems on its edges has a great deal to do with the
mysteries of universe. However you do not have the laptop with you, thus you have written the coordinates of all star systems down on a piece of paper and decide to work out the result later. Can you finish this task?
There are multiple test cases in the input file. Each test case starts with one integerN ,
(1N100)
, the number of star systems on the telescope. N lines follow, each line consists of two integers: theX and
Y coordinates of theK -th planet system. The absolute value of any coordinate is no more than109 , and you can assume that the planets are arbitrarily
distributed in the universe.
N = 0 indicates the end of input file and should not be processed by your program.
For each test case, output the maximum value you have found on a single line in the format as indicated in the sample output.
10
2 3
9 2
7 4
3 4
5 7
1 5
10 4
10 6
11 4
4 6
0
Case 1: 7
思路:
枚举矩形的上下界,然后选择左右边界。 对于确定的左边界left和右边界right, 假设是下图的R3是left, L3是right,那么数量为:
L1 + L2 + L3 - (R1+R2) + R3.
为了要使得以L3为右边界的矩形上的点最多,那么应该使得 R3-(R1+R2)的值最大。
所以,枚举上下边界,然后枚举右边界j, 同时维护保存j左边的R3-(R1+R2)的最大值,O(n^3)确定答案。
完整代码:
/*0.025s*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 105;
struct Point
{
int x, y;
bool operator < (const Point& rhs) const
{
return x < rhs.x;
}
} P[maxn];
int n, m, y[maxn], on[maxn], on2[maxn], left[maxn];
int solve()
{
sort(P, P + n);
sort(y, y + n);
m = unique(y, y + n) - y; // 所有不同的y坐标的个数
if (m <= 2) return n; // 特判,最多两种不同的y
int ans = 0;
for (int a = 0; a < m; a++)
for (int b = a + 1; b < m; b++)
{
int ymin = y[a], ymax = y[b]; // 计算上下边界分别为ymin和ymax时的解
// 计算left, on, on2
int k = 0;
for (int i = 0; i < n; i++)
{
if (i == 0 || P[i].x != P[i - 1].x) // 一条新的竖线
{
k++;
on[k] = on2[k] = 0;
left[k] = (k == 0 ? 0 : left[k - 1] + on2[k - 1] - on[k - 1]);
}
if (P[i].y > ymin && P[i].y < ymax) on[k]++;
if (P[i].y >= ymin && P[i].y <= ymax) on2[k]++;
}
if (k <= 2) return n; // 特判,最多两种不同的x
int M = 0;
for (int j = 1; j <= k; j++)
{
ans = max(ans, left[j] + on2[j] + M);
M = max(M, on[j] - left[j]);//贪心嘛,动态维护on[j] - left[j]
}
}
return ans;
}
int main()
{
int kase = 0;
while (scanf("%d", &n), n)
{
for (int i = 0; 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 0;
}
分享到:
相关推荐
Distant galaxy
SF Distant Galaxy
DISTANT
Distant supervision 相关文献 Distant supervision 相关文献
Two problems arise when using distant su- pervision for relation extraction. First, in this method, an already existing knowl- edge base is heuristically aligned to texts, and the alignment results ...
要求GCC> = 4.8 Python> = 3.7安装git clone https://github.com/fuzihaofzh/distant_supervision_nlg.gitcd distant_supervision_nlg./scripts/setup.sh预处理数据./scripts/preprocess.sh wita50k训练基线模型(可...
Blocky distant lights 2.sut
Deep_Learning_for_Distant_Speech_Recognition Mirco Ravanelli
<h1 class='distant1'>Distant Light</h1> <h1 class='distant-top'>Distant Top</h1> <h1 class='distant-front'>Distant Front</h1> <h1 class='diff1'>Diffused Light</h1> <h1 class='bevel'>Bevel</h1>
Adversarial learning for distant supervised relation extraction
Jindong Wang | Distant Domain Transfer LearningWikipedia: research problem in ma
32位处理器9b96中UART应用源代码
Distant Supervised Relation Extraction with Cost-Sensitive Loss
遥远的世界-宇宙-翻译遥远的世界宇宙的翻译#Installation:将lange 文件夹(例如DE)的内容复制到Distant Worlds Universe 文件夹(不是语言文件夹本身!)。 你应该先备份! #Credits 初始翻译来自 ! 欢迎所有帮助...
遥远的贝斯版权所有(c)2019-2021 该存储库包含distant-bes的代码,一个用于将构建结果注入实现服务的库。安装为了安装该库,请克隆此存储库并运行pip install distant-bes/ 。兼容性该库已经针对构建事件协议的...
Mon-depot-distant
遥远的阅读味道Projekt“远距离阅读品味” zum Kurs“数字人文科学”
Distant.cpp 2008/12/3 12:33 C++ Source 1 KB Distant.h 2008/12/3 12:31 C/C++ Header 2 KB MainFrm.cpp 2008/12/1 21:29 C++ Source 3 KB MainFrm.h 2008/12/1 21:29 C/C++ Header 2 KB ReadMe.txt 2008/12/1 21:...
我们提供NYT数据集,该数据集一共包含233081实体对,由FreeBase对齐,关系数量为57(如果使用53关系的可自行过滤多余的关系及句子)。
WebRTC