求调
查看原帖
求调
1269423
huge_wave楼主2024/9/8 13:45
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
struct noid{
	int x,id;
}a[N];
bool cmp(const noid&xx,const noid&yy){
	return xx.x<yy.x;
}
unordered_map<int,int> mp;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n,dd=0,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].x>>a[i].id;
		if(mp[a[i].id]==0){
			dd++;
			mp[a[i].id]=1;
		}
	}
	//cout<<dd<<"\n";
	mp.clear();
	sort(a+1,a+1+n,cmp);
	int l=1,r=0;
	int maxx=2e9;
	for(int i=1;i<=n;i++){
		if(ans==dd){
			maxx=min(maxx,a[r].x-a[l].x);
		//	cout<<1;
		}
		r++;
		if(mp[a[r].id]==0){
			ans++;
		}
		mp[a[r].id]++;
		//cout<<ans;
		while(mp[a[l].id]>1){
			mp[a[l].id]--;
			l++;
		}
	}
	cout<<maxx;
	return 0;
}            
2024/9/8 13:45
加载中...