-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3029.cpp
48 lines (48 loc) · 906 Bytes
/
P3029.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<bits/stdc++.h>
using namespace std;
int N;
struct cow{
int x,id;
};
struct cow c[50001];
set<int>s;
map<int,int>kind;
int len[50001];
bool cmp(struct cow c1,struct cow c2){
return c1.x<c2.x;
}
int main()
{
cin>>N;
for(int i=1;i<=N;i++)
{
cin>>c[i].x>>c[i].id;
s.insert(c[i].id);
}
int k=s.size();
sort(c+1,c+N+1,cmp);
int l=1,r=2;
int ans=1<<30;
kind[c[l].id]=1;
for(r=2;r<=N;r++)
{
if(kind.find(c[r].id)==kind.end()){
kind[c[r].id]=1;
}
else kind[c[r].id]++;
int tol=kind.size();
while(true){
kind[c[l].id]--;
if(kind[c[l].id]==0){
kind[c[l].id]++;
//if(l!=1)l--;
break;
}
l++;
}
if(tol==k){
ans=min(ans,c[r].x-c[l].x);
}
}
cout<<ans;
}