uoj278 【UTR #2】题目排列顺序

题目

读进来\(f\)之后,把权值作为第一关键字从小到大排序,位置作为第二关键字从大到小排序,这样排序后的第\(i\)个位对应的位置就应该填数字\(i\)

权值作为第一关键字,保证了出现在其之前的数权值必它小的填的数也比它小,权值必它大的填的数也比它大;权值相同时按照位置从大到小排序,这样权值相同的一组就是递减的,就不会相互影响了

代码

#include<bits/stdc++.h>
#define re register
inline int read() {
	char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=1e5+5;
int n,b[maxn];
struct N{int v,p;}a[maxn];
inline int cmp(N A,N B) {return A.v==B.v?A.p>B.p:A.v<B.v;}
int main() {
	n=read();
	for(re int i=1;i<=n;i++) a[i].v=read(),a[i].p=i;
	std::sort(a+1,a+n+1,cmp);
	for(re int i=1;i<=n;i++) b[a[i].p]=i;
	for(re int i=1;i<=n;++i) printf("%d ",b[i]);puts("");
	return 0;
}

posted @ 2019-09-06 21:40  asuldb  阅读(160)  评论(0编辑  收藏  举报