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

本文探讨了如何通过算法找出望远镜观测到的遥远星系中,包含最多星系的矩形边界问题。该算法首先对星系坐标进行排序,并通过枚举矩形的上下边界来确定可能的最大星系数量。

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?

\epsfbox{p3694.eps}

Input 

There are multiple test cases in the input file. Each test case starts with one integerN , (1$ \le$N$ \le$100) , 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.

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

&lt;think&gt;好的,用户想了解在编程或技术背景下&ldquo;distant&rdquo;相关的概念或用法。首先,我需要回忆一下在计算机科学和信息技术中常见的包含&ldquo;distant&rdquo;的术语或概念。可能的方面包括网络通信中的距离、分布式系统、远程操作相关的技术,或者算法中的距离度量等。 首先想到的是&ldquo;远程过程调用&rdquo;(Remote Procedure Call, RPC),这里的&ldquo;远程&rdquo;对应distant,指调用不同地址空间(通常在不同机器上)的过程或函数。另外,&ldquo;分布式计算&rdquo;(Distributed Computing)也涉及多台计算机协同工作,可能分布在不同的地理位置,这也是distant的一种体现。 接着是网络方面的术语,比如&ldquo;延迟&rdquo;(Latency),即数据从一点传输到另一点的时间,这可能涉及到地理距离的影响,比如远距离通信中的延迟问题。还有&ldquo;地理分布式系统&rdquo;(Geographically Distributed Systems),例如内容分发网络(CDN),通过将服务器部署在全球各地来减少用户访问的延迟。 在数据结构和算法中,可能有&ldquo;编辑距离&rdquo;(Edit Distance)或&ldquo;汉明距离&rdquo;(Hamming Distance),这些用于衡量两个字符串之间的差异程度。虽然这里的distance不一定指物理距离,但属于技术中的距离概念。 此外,还有&ldquo;远程终端&rdquo;(Remote Terminal)或&ldquo;远程桌面&rdquo;(Remote Desktop),这些技术允许用户访问和控制位于远端的计算机,属于distant的应用场景。 需要确认这些术语的正确性,例如RPC确实涉及远程调用,分布式系统的定义是否符合用户需求。同时,考虑用户可能的深层需求,比如他们是否在寻找特定的技术实现,或者想了解相关概念的应用场景。还要注意用户提供的引用中的&ldquo;module&rdquo;和&ldquo;protocol implementation&rdquo;,可能引导到模块化或协议相关的远程通信技术。 最后,要确保回答的结构清晰,涵盖不同技术领域中的distant应用,并生成相关问题,帮助用户进一步探索。同时,检查是否有遗漏的重要概念,如物联网中的远程传感器、云计算中的远程服务等,确保覆盖全面。&lt;/think&gt;在编程或技术语境中,&ldquo;distant&rdquo;通常与远程通信、分布式系统或空间/逻辑距离相关的概念相关。以下是几个关键方向: ### 1. **远程过程调用(RPC)** - 允许程序调用另一个地址空间(通常是另一台机器)的函数或方法,例如 gRPC[^1]。其核心是隐藏网络通信细节,使远程调用像本地调用一样。 - 示例代码片段: ```python # 使用 gRPC 的简单客户端调用 import grpc channel = grpc.insecure_channel(&#39;localhost:50051&#39;) stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name=&#39;Alice&#39;)) ``` ### 2. **分布式系统(Distributed Systems)** - 系统组件分布在多个物理或逻辑节点上,通过消息传递协作,例如: - **分布式数据库**:如 Cassandra,数据分片存储在不同节点。 - **区块链**:节点通过共识算法达成一致性。 ### 3. **网络延迟与地理距离** - 远距离通信需处理信号传播延迟(如跨洋光缆延迟约 60ms/1000km),影响协议设计(如 TCP 拥塞控制)。 ### 4. **距离度量算法** - **编辑距离(Edit Distance)**:衡量字符串差异,用于拼写检查。 $$ \text{edit\_distance}(s, t) = \min\{ \text{插入}, \text{删除}, \text{替换} \} $$ - **欧氏距离(Euclidean Distance)**:用于机器学习聚类(如 K-means): $$ d(\mathbf{p}, \mathbf{q}) = \sqrt{\sum_{i=1}^n (p_i - q_i)^2} $$ ### 5. **远程设备控制** - 如 IoT 中通过 MQTT 协议控制远距离传感器: ```python import paho.mqtt.client as mqtt client = mqtt.Client() client.connect(&quot;broker.hivemq.com&quot;, 1883) client.publish(&quot;sensor/temperature&quot;, &quot;25&deg;C&quot;) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值