跳到正文

个人记录

大狗大狗叫叫叫

DengDuck约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年7月21日
更新
2026年8月15日
篇幅
约 3 分钟
获赞
1

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define X first
#define Y second
#define LL long long
#define pLL pair<LL,LL>
using namespace std;
const int N=2e6+5;
pLL P[N];
multiset<pLL>S;
int n;LL D;
inline LL Dis(pLL A,pLL B){return (A.X-B.X)*(A.X-B.X)+(A.Y-B.Y)*(A.Y-B.Y);}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%lld%lld",&P[i].X,&P[i].Y);
	D=Dis(P[1],P[2]),sort(P+1,P+n+1);
	for(int i=1,j=1;i<=n;i++)
	{
		while(D&&D<=(P[j].X-P[i].X)*(P[j].X-P[i].X))S.erase(S.lower_bound({P[j].Y,P[j].X})),j++;
		auto p=S.lower_bound({P[i].Y,P[i].X});
		for(;p!=S.end()&&(p->X-P[i].Y)*(p->X-P[i].Y)<D;p++)D=min(D,Dis(*p,{P[i].Y,P[i].X}));
		p=S.lower_bound({P[i].Y,P[i].X});
		if(p!=S.begin())
		{
			p--;
			for(;(p->X-P[i].Y)*(p->X-P[i].Y)<D;p--)
			{
				D=min(D,Dis(*p,{P[i].Y,P[i].X}));
				if(p==S.begin())break;
			}
		}
		S.insert({P[i].Y,P[i].X});
	}
	printf("%lld\n",D);
}
//Lost the swiss roll and prepare to MuLi MuLi

判断题

1.Dis 函数可以求出两个点的欧几里得距离。

2.第 16 行的 D=Dis(P[1],P[2]) 改为 D=1e18 不会影响程序的时间复杂度。

3.第 21 行的 (p->X-P[i].Y)*(p->X-P[i].Y)<D 删除不会影响程序的时间复杂度。

选择题

4.请问给出以下输入:

代码
3

1 1

1 2

2 2

输出是:

A.44。

B.11。

C.22。

D.33。

5.请问给出以下输入:

代码
5

1 1

1 9

9 1

9 9

0 10

输出是:

A.77。

B.55。

C.22。

D.33。

  1. 这个代码的最坏时间复杂度是:

A.O(nlog⁡n)\mathcal O(n\log n)。

B.O(nlog⁡2n)\mathcal O(n\log^2 n)。

C.O(n2)\mathcal O(n^2)。

D.O(n2log⁡n)\mathcal O(n^2\log n)。

原文
存档评论7 条抓取于