B. New Year and the Treasure Geolocation

Bob is a pirate looking for the greatest treasure the world has ever seen. The treasure is located at the point TT, which coordinates to be found out.

Bob travelled around the world and collected clues of the treasure location at nn obelisks. These clues were in an ancient language, and he has only decrypted them at home. Since he does not know which clue belongs to which obelisk, finding the treasure might pose a challenge. Can you help him?

As everyone knows, the world is a two-dimensional plane. The ii-th obelisk is at integer coordinates (xi,yi)(xi,yi). The jj-th clue consists of 22 integers (aj,bj)(aj,bj) and belongs to the obelisk pjpj, where pp is some (unknown) permutation on nn elements. It means that the treasure is located at T=(xpj+aj,ypj+bj)T=(xpj+aj,ypj+bj). This point TT is the same for all clues.

In other words, each clue belongs to exactly one of the obelisks, and each obelisk has exactly one clue that belongs to it. A clue represents the vector from the obelisk to the treasure. The clues must be distributed among the obelisks in such a way that they all point to the same position of the treasure.

Your task is to find the coordinates of the treasure. If there are multiple solutions, you may print any of them.

Note that you don’t need to find the permutation. Permutations are used only in order to explain the problem.Input

The first line contains an integer nn (1≤n≤10001≤n≤1000) — the number of obelisks, that is also equal to the number of clues.

Each of the next nn lines contains two integers xixi, yiyi (−106≤xi,yi≤106−106≤xi,yi≤106) — the coordinates of the ii-th obelisk. All coordinates are distinct, that is xi≠xjxi≠xj or yi≠yjyi≠yj will be satisfied for every (i,j)(i,j) such that i≠ji≠j.

Each of the next nn lines contains two integers aiai, bibi (−2⋅106≤ai,bi≤2⋅106−2⋅106≤ai,bi≤2⋅106) — the direction of the ii-th clue. All coordinates are distinct, that is ai≠ajai≠aj or bi≠bjbi≠bj will be satisfied for every (i,j)(i,j) such that i≠ji≠j.

It is guaranteed that there exists a permutation pp, such that for all i,ji,j it holds (xpi+ai,ypi+bi)=(xpj+aj,ypj+bj)(xpi+ai,ypi+bi)=(xpj+aj,ypj+bj).Output

Output a single line containing two integers Tx,TyTx,Ty — the coordinates of the treasure.

If there are multiple answers, you may print any of them.ExamplesinputCopy

2
2 5
-6 4
7 -2
-1 -3

outputCopy

1 2

inputCopy

4
2 2
8 2
-7 0
-2 6
1 -14
16 -12
11 -18
7 -14

outputCopy

9 -12

Note

As n=2n=2, we can consider all permutations on two elements.

If p=[1,2]p=[1,2], then the obelisk (2,5)(2,5) holds the clue (7,−2)(7,−2), which means that the treasure is hidden at (9,3)(9,3). The second obelisk (−6,4)(−6,4) would give the clue (−1,−3)(−1,−3) and the treasure at (−7,1)(−7,1). However, both obelisks must give the same location, hence this is clearly not the correct permutation.

If the hidden permutation is [2,1][2,1], then the first clue belongs to the second obelisk and the second clue belongs to the first obelisk. Hence (−6,4)+(7,−2)=(2,5)+(−1,−3)=(1,2)(−6,4)+(7,−2)=(2,5)+(−1,−3)=(1,2), so T=(1,2)T=(1,2) is the location of the treasure.

 

In the second sample, the hidden permutation is [2,3,4,1][2,3,4,1].


思路:数据范围允许暴力,我们先暴力一发

#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e3+100;
typedef long long LL;
struct obelisk{
	LL x,y;
}point[maxn];
struct clue{
	LL x,y;
}clues[maxn];
struct visit{
	LL x,y;
};
inline bool operator < (const struct visit &v1,const struct visit &v2)
{
	return v1.x<v2.x||(v1.x==v2.x)&&(v1.y<v2.y);
}
map<visit,LL>map1;
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  LL cnt=0;
  for(LL i=1;i<=n;i++) cin>>point[i].x>>point[i].y;
  for(LL i=1;i<=n;i++) cin>>clues[i].x>>clues[i].y;
  for(LL i=1;i<=n;i++)
  	for(LL j=1;j<=n;j++)
  	{
  		LL x=point[i].x+clues[j].x;
		LL y=point[i].y+clues[j].y;	

		visit a;a.x=x;a.y=y;
		map1[a]++;
	}
	for(map<visit,LL>::iterator iter=map1.begin();iter!=map1.end();iter++)
	{
		if(iter->second>=n)
		{
			cout<<iter->first.x<<" "<<iter->first.y<<endl;
			return 0;
		}	
	}	
return 0;
}
 

但是如果数据范围扩大了呢。这里有个小结论。

此题完全可以开到1e6去卡n^2的做法

这时候考虑结论:
(tx,ty)=(xpi+ai,ypi+bi);
由于最后保证所有到的宝藏点一样
那么sum(xpi+ai,ypi+bi)=n*(tx,ty)
那么(tx,ty)=sum(xpi+ai,ypi+bi)/n

类似对称性,两个点关于宝藏点的点对称,距离不变。

这样就可以O(n)了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值