首页 > 其他分享 >Shanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)

Shanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)

时间:2023-04-12 13:08:40浏览次数:52  
标签:const 边界 R3 int Shanghai 枚举 maxn 2006 1382


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?



Shanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)_#include


Input

There are multiple test cases in the input file. Each test case starts with one integerN , (1Shanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)_#include_02NShanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)_#include_02100) , 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

N = 0

Output

For each test case, output the maximum value you have found on a single line in the format as indicated in the sample output.

Sample Input

10 2 3 
9 2 
7 4 
3 4 
5 7 
1 5 
10 4 
10 6 
11 4 
4 6 
0


Sample Output


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;
}


标签:const,边界,R3,int,Shanghai,枚举,maxn,2006,1382
From: https://blog.51cto.com/u_5535544/6185466

相关文章

  • SPEC2006的学习与总结
    SPEC2006的学习与总结摘要最近特别想进行一些性能验证工作.所以研究了spec2006然后想整理一下之前的内容.想着将内容整理一下.这次主要是抄别人的.知识来源:https://blog.csdn.net/wkl_venus/article/details/127688671获取测试结果的命令nohuprunspec--repor......
  • SPECCPU2006的学习与使用
    SPECCPU2006的学习与使用摘要这个周末问题不是很多,陪孩子写作业时顺便研究了下SPEC2006虽然比较落后了.但是总比没有要强一些.其实集团有资源,但是联系不到人,只能自己学习和研究了.找了很多华为博客上面的知识点.但是依旧有很多问题想着先总结这,希望有时间慢慢完......
  • POJ 2773 Happy 2006 二分+容斥原理(二进制枚举或dfs)
    Happy2006TimeLimit: 3000MS MemoryLimit: 65536KTotalSubmissions: 14003 Accepted: 4946DescriptionTwopositiveintegersaresaidtoberelativelyprimetoeachotheriftheGreatCommonDivisor(GCD)is1.Forinstance,1,3,5,7,9...areallrelativel......
  • [百度贴吧]部分CPU的SPEC2006int 结果
    这些测试成绩基本上是本人自己测试的结果。下表中有来自spec官网的两个成绩,因为测试年份较早,系统环境和编译器都较老,测试成绩本人实测的还差,所以仅作为参考。部分测试启用......
  • bzoj 2594 [Wc2006]水管局长数据加强版
    2594:[Wc2006]水管局长数据加强版TimeLimit: 25Sec  MemoryLimit: 128MBSubmit: 3509  Solved: 1119[Submit][Status][Discuss]DescriptionSC省M......
  • bzoj 2006 [NOI2010] 超级钢琴 线段树求区间极值+优先队列
    挺神奇的一道题,唯一想不通的是为什么放在主席树的题单里..首先暴力找出所有的合法区间显然是不可能的。考虑怎么贪心,假如固定每个L作为左端点,那么合法的区间就是[L+l-1,L......
  • go 神奇的错误 time.Now().Format("2006-01-02 13:04:05") 比北京时间大8小时
    困倦的时候写了个个获取本地时间,打印总比当前时间大8小时,找了很久原因 packagemainimport("fmt""time")funcmain(){now:=time.Now()f......
  • HDOJ2006求奇数的乘积
    求奇数的乘积TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):103371    AcceptedSubmission(s):......
  • B2006 地球人口承载力估计--解题思路
    地球人口承载力估计地球人口承载力估计题目描述假设地球上的新生资源按恒定速度增长。照此测算,地球上现有资源加上新生资源可供\(x\)亿人生活\(a\)年,或供\(y\)亿......
  • 构造AVL树基础 + 力扣1382. 将二叉搜索树变平衡
    构造AVL树基础定义对于任意一个节点,左子树和右子树的高度差不能超过1。怎么计算标注节点的高度计算平衡因子如何维持平衡如果平衡被打破需要根据不同的情况来旋......