全WA求调
查看原帖
全WA求调
151425
千年知乎_天才楼主2024/5/8 23:52
#include<bits/stdc++.h>
#define int long long
using namespace std;
int input(){
	char ch=cin.get();
	int v=0,sign=1;
	for(;!isdigit(ch);ch=cin.get())
		if(ch=='-')sign=-sign;
	for(;isdigit(ch);ch=cin.get())
		v=(v<<3)+(v<<1)+(ch^'0');
	return v*sign;
}
const int maxn=1e5+5;
struct node{int x,y;}cow[maxn];
int n,k;
typedef long long ll;
ll cross(int a,int b,int c){
	return 1ll*(cow[c].x-cow[a].x)*(cow[b].y-cow[a].y)-1ll*(cow[b].x-cow[a].x)*(cow[c].y-cow[a].y);
}
ll dist(int a,int b){
	return 1ll*(cow[a].x-cow[b].x)*(cow[a].x-cow[b].x)+1ll*(cow[a].y-cow[b].y)*(cow[a].y-cow[b].y);
}
int hull[maxn*2],tt;
bool ex[maxn];
priority_queue<ll> pq;
void erase(int i){
	for(int j=1;j<=n;j++)
		if(ex[j])pq.push(dist(i,j));
}
int rd(int i){return (i+1)%tt;}
int A,B,C;
int acute(int i){
	i=hull[i];
	return abs(A*cow[i].x+B*cow[i].y-C);
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	memset(ex,true,sizeof ex);
	n=input(),k=input();
	for(int i=1;i<=n;i++)
		cow[i].x=input(),cow[i].y=input();
	if(n<=(k<<1))
		for(int i=1;i<=n;i++)
			for(int j=1;j<i;j++)
				pq.push(dist(i,j));
	else{
		for(int i=0;i<k;i++){
			vector<int> ord;
			for(int i=1;i<=n;i++) if(ex[i]) ord.push_back(i);
			sort(ord.begin(),ord.end(),[](int a,int b){
				if(cow[a].x!=cow[b].x) return cow[a].x<cow[b].x;
				return cow[a].y<cow[b].y;
			});
			hull[0]=ord[0];
			tt=1;
			for(int i=1;i<(int)ord.size();i++){
				while(hull[tt-1]!=ord[0]&&1ll*(cow[ord[i]].y-cow[hull[tt-1]].y)*(cow[ord[i]].x-cow[hull[tt-2]].x)>=1ll*(cow[ord[i]].y-cow[hull[tt-2]].y)*(cow[ord[i]].x-cow[hull[tt-1]].x)) tt--;
				hull[tt++]=ord[i];
			}
			for(int i=(int)ord.size()-2;i>=0;i--){
				while(hull[tt-1]!=ord.back()&&1ll*(cow[hull[tt-1]].y-cow[ord[i]].y)*(cow[hull[tt-2]].x-cow[ord[i]].x)>=1ll*(cow[hull[tt-2]].y-cow[ord[i]].y)*(cow[hull[tt-1]].x-cow[ord[i]].x)) tt--;
				hull[tt++]=ord[i];
			}
			tt--;
			int mx,my,cur;
			if(tt==2){
				mx=hull[0],my=hull[1];
				goto end;
			}
			cur=0;
			while(cross(hull[tt-1],hull[0],hull[(cur+1)%tt])>cross(hull[tt-1],hull[0],hull[cur])) cur=(cur+1)%tt;
			mx=hull[cur];
			if(dist(hull[cur],hull[0])>dist(hull[cur],hull[tt-1])) my=hull[0];
			else my=hull[tt-1];
			for(int i=0;i<tt;i++){
				A=cow[hull[i]].y-cow[hull[rd(i)]].y;
				B=cow[hull[i]].x-cow[hull[rd(i)]].x;
				C=A*cow[hull[i]].x+B*cow[hull[i]].y;
				while(acute(rd(cur))>acute(cur))cur=rd(cur);
				if(dist(hull[cur],hull[i])>dist(mx,my))
					mx=hull[cur],my=hull[i];
				if(dist(hull[cur],hull[rd(i)])>dist(mx,my))
					mx=hull[cur],my=hull[rd(i)];
			}
			end:pq.push(dist(mx,my));
			ex[mx]=ex[my]=false;
			erase(mx),erase(my);
		}
	}
	while(--k)pq.pop();
	cout<<pq.top();
	return 0;
}
#include<bits/stdc++.h>
#define int long long
using namespace std;
int input(){
	char ch=cin.get();
	int v=0,sign=1;
	for(;!isdigit(ch);ch=cin.get())
		if(ch=='-')sign=-sign;
	for(;isdigit(ch);ch=cin.get())
		v=(v<<3)+(v<<1)+(ch^'0');
	return v*sign;
}
const int maxn=1e5+5;
struct node{int x,y;}cow[maxn];
int n,k;
typedef long long ll;
ll cross(int a,int b,int c){
	return 1ll*(cow[c].x-cow[a].x)*(cow[b].y-cow[a].y)-1ll*(cow[b].x-cow[a].x)*(cow[c].y-cow[a].y);
}
ll dist(int a,int b){
	return 1ll*(cow[a].x-cow[b].x)*(cow[a].x-cow[b].x)+1ll*(cow[a].y-cow[b].y)*(cow[a].y-cow[b].y);
}
int hull[maxn*2],tt;
bool ex[maxn];
priority_queue<ll> pq;
void erase(int i){
	for(int j=1;j<=n;j++)
		if(ex[j])pq.push(dist(i,j));
}
int rd(int i){return (i+1)%tt;}
int A,B,C;
int acute(int i){
	i=hull[i];
	return abs(A*cow[i].x+B*cow[i].y-C);
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	memset(ex,true,sizeof ex);
	n=input(),k=input();
	for(int i=1;i<=n;i++)
		cow[i].x=input(),cow[i].y=input();
	if(n<=(k<<1))
		for(int i=1;i<=n;i++)
			for(int j=1;j<i;j++)
				pq.push(dist(i,j));
	else{
		for(int i=0;i<k;i++){
			vector<int> ord;
			for(int i=1;i<=n;i++) if(ex[i]) ord.push_back(i);
			sort(ord.begin(),ord.end(),[](int a,int b){
				if(cow[a].x!=cow[b].x) return cow[a].x<cow[b].x;
				return cow[a].y<cow[b].y;
			});
			hull[0]=ord[0];
			tt=1;
			for(int i=1;i<(int)ord.size();i++){
				while(hull[tt-1]!=ord[0]&&1ll*(cow[ord[i]].y-cow[hull[tt-1]].y)*(cow[ord[i]].x-cow[hull[tt-2]].x)>=1ll*(cow[ord[i]].y-cow[hull[tt-2]].y)*(cow[ord[i]].x-cow[hull[tt-1]].x)) tt--;
				hull[tt++]=ord[i];
			}
			for(int i=(int)ord.size()-2;i>=0;i--){
				while(hull[tt-1]!=ord.back()&&1ll*(cow[hull[tt-1]].y-cow[ord[i]].y)*(cow[hull[tt-2]].x-cow[ord[i]].x)>=1ll*(cow[hull[tt-2]].y-cow[ord[i]].y)*(cow[hull[tt-1]].x-cow[ord[i]].x)) tt--;
				hull[tt++]=ord[i];
			}
			tt--;
			int mx,my,cur;
			if(tt==2){
				mx=hull[0],my=hull[1];
				goto end;
			}
			cur=0;
			while(cross(hull[tt-1],hull[0],hull[(cur+1)%tt])>cross(hull[tt-1],hull[0],hull[cur])) cur=(cur+1)%tt;
			mx=hull[cur];
			if(dist(hull[cur],hull[0])>dist(hull[cur],hull[tt-1])) my=hull[0];
			else my=hull[tt-1];
			for(int i=0;i<tt;i++){
				while(cross(hull[i],hull[rd(i)],hull[(cur+1)%tt])>cross(hull[i],hull[rd(i)],hull[cur])) cur=(cur+1)%tt;
				if(dist(hull[cur],hull[i])>dist(mx,my))
					mx=hull[cur],my=hull[i];
				if(dist(hull[cur],hull[rd(i)])>dist(mx,my))
					mx=hull[cur],my=hull[rd(i)];
			}
			end:pq.push(dist(mx,my));
			ex[mx]=ex[my]=false;
			erase(mx),erase(my);
		}
	}
	while(--k)pq.pop();
	cout<<pq.top();
	return 0;
}

两个代码唯一区别就是判距离直线最远点的方法不同,但是前面一个全WA,后面AC。 求大佬指点。

2024/5/8 23:52
加载中...