结构体:
int n,i,b[N];
struct stu{
int x,id;
bool operator<(stu &stu1)const{
return x<stu1.x;
}
}s[N];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&s[i].x);
s[i].id=i;
}
sort(s+1,s+1+n);
for(i=1;i<=n;i++)b[s[i].id]=i;
for(i=1;i<=n;i++)printf("%d ",b[i]);
}
STL(unique大法)
int n,i,a[N],b[N];
int main(){
scanf("%d"&n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(a+1,a+1+n);
int len=unique(a+1,a+1+n)-a-1;
for(i=1;i<=n;i++){
b[i]=lower_bound(a+1,a+1+n,b[i])-a;
}
for(i=1;i<=n;i++)printf("%d ",b[i]);
return 0;
}
STL(map大法)
int n,i,a[N],b[N],cnt;
map<int,int>mp;
int main(){
scanf("%d"&n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(a+1,a+1+n);
for(i=1;i<=n;i++)
mp[a[i]]=++cnt;
for(i=1;i<=n;i++)
printf("%d ",mp[b[i]]);
return 0;
}
标签:大法,int,scanf,离散,STL,main
From: https://www.cnblogs.com/0shadow0/p/18048941