HDU 3490 推公式

Manhattan Distance

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)



Problem Description
The kingdom of Henryy is the most civilized country in the world. Meanwhile, the capital city H is as well the most beautiful city on the earth. Currently, the city is to build up database to maintain the statistics of the buildings in the city.
You are a little P in the company that takes this project. Your task is to calculate the maximum distance between two buildings in the city.
The Manhattan Distance is defined as: for two points P1(x1, y1) and P2(x2, y2) on a two dimension Cartesian plane, the Manhattan Distance D(P1, P2) = |x1-x2|+|y1-y2|.
Your database should support this query: after updating a data that a building has built or demolished, it should return the maximum distance between existed buildings:



N is the current number of buildings, and Pi is the coordinates of the ith building.
 

Input
The first line contains an integer T (T<=10), indicating the number of the test cases.
For each test case, the first line contains an integer M (M<=100000), the number of records of the changes of buildings.
Then M lines followed, each line starting with a positive integer: 1 for building that has built, and 2 for demolished. A string with less than 9 characters follows, indicating the name of the building. If this building is just built, two integers of the coordinates of this building are given afterwards.
Notice that no two buildings share the same name, even when one of these buildings has been demolished, but might share the same coordinates. It is also guaranteed that the building exists when a demolishing record is given.
A blank line is followed after each test case.
 

Output
For each record, output an integer in a single line, indicating the maximum Manhattan Distance among existing buildings.
Print a blank line after each test case.
 

Sample Input
  
  
1 8 1 mgj -2 1 2 mgj 1 kmoaktmr 4 -4 1 mauxizu 3 -2 2 kmoaktmr 1 md -1 4 2 md 1 umos -5 0
 Sample Output
  
  
0 -1 0 3 0 10 0 10

题意:给你n个操作,1代表在(x,y)这个坐标修建一个城市,2代表拆除一个城市,每次操作后,输出图中城市间的最大曼哈顿距离,如果没有城市,输出-1.


题解:对于(x1-x2)+(y1-y2),最多有两种可能,两者同号或者异号。
当同号时|x1-x2|+|y1-y2|=(x1+y1)-(x2+y2)。
当异号时|x1-x2|+|y1-y2|=(x1-y1)-(x2-y2)。
所以我们可以用map保存城市的坐标,用两个multiset保存每个城市的x-y和x+y,对于每次操作后,输出两个multiset中最小元素和最大元素的差的最大值即可。代码是实现思想的,很容易理解,就不加注释了。
注意题目要每个测试样例输出一行空行。



#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
multiset<int>sp1,sp2;
struct node{
	int l,r;
}e[100005];
int cnt;
map<string,int>spp;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		cnt=0;
		spp.clear();
		sp1.clear();
		sp2.clear();
		int x,n,i;
		char c[11];
		scanf("%d",&n);
		for(i=1;i<=n;i++){
			scanf("%d%s",&x,c);
			multiset<int>::iterator it;
			if(x==1){
				int l,r;
				scanf("%d%d",&l,&r);
				spp[c]=++cnt;
				e[cnt].l=l;
				e[cnt].r=r;
				sp1.insert(l-r);
				sp2.insert(l+r);
			}
			else{
				int lab=spp[c];
				int l=e[lab].l,r=e[lab].r;
				it=sp1.find(l-r);
				sp1.erase(it);
				it=sp2.find(l+r);
				sp2.erase(it);
			}
			if(sp1.empty()||sp2.empty()){
				printf("-1\n");
				continue;
			}
			it=sp1.begin();
			int mu1=*it;
			it=sp1.end();
			it--;
			int mu2=*it;
			it=sp2.begin();
			int mu3=*it;
			it=sp2.end();
			it--;
			int mu4=*it;
			printf("%d\n",max(mu2-mu1,mu4-mu3));
		}
		printf("\n"); 
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值