离散化

1
2
3
4
5
6
7
8
9
10
11
// c++11
vector<int> a,b;
for(int i=0;i<n;i++){
cin >> a[i];
b[i] = a[i];
}
b.resize(distance(b.begin(),unique(b.begin(),b.end())));

inline int getid(int x){
return lower_bound(b.begin(),b.end(),x) - b.begin() + 1;
}
1
2
3
4
5
6
7
8
for(int i=0;i<n;i++){
A[i] = B[i];
}
sort(B,B+n);
int size = unique(B,B+n) - B;
for(int i=0;i<n;++){
A[i] = lower_bound(B,B+size,A[i]) - B + 1;
}
Thank you for your support!