[Input Example]c++
2spa
10code
1 2 3 4 5 6 7 8 9 9orm
10blog
27 22 15 30 29 12 20 13 6 10排序
[Output Example]input
10 9 8 7 6 5 4 3 1 1string
3 4 6 1 2 8 5 7 10 9hash
1.排序io
#include <stdio.h> #include <stdlib.h> #include <string.h> int N; typedef struct _SIS SIS; struct _SIS { int pos; int val; }; SIS Sis[300005]; int order[300005]; int comp(const void *a, const void *b) { SIS *Sa=(SIS *)a; SIS *Sb=(SIS *)b; return (*Sa).val-(*Sb).val; } int main(void) { int tc, T, i; freopen("input.txt", "r", stdin); setbuf(stdout, NULL); scanf("%d", &T); for(tc = 0; tc < T; tc++) { scanf("%d", &N); for(i = 1; i <= N; i++) { scanf("%d", &Sis[i].val); Sis[i].pos=i; } qsort(Sis+1,N,sizeof(Sis[1]),comp); int num=1; int countsame=1; for(i=N;i>=1;i--) { order[Sis[i].pos]=num; if(i>=2) { if(Sis[i].val==Sis[i-1].val) { countsame++; } else { num+=countsame; countsame=1; } } } for(i = 1; i <= N; i++) { printf("%d ", order[i]); } printf("\n"); } return 0;//Your program should return 0 on normal termination. }
2.數字範圍不是很大,hash
#include <stdio.h> #include <string.h> int N; int Si[300005]; int order[300005]; int SiHz[32001]; int SiHzOrder[32001]; int main(void) { int tc, T, i; freopen("input.txt", "r", stdin); setbuf(stdout, NULL); scanf("%d", &T); for(tc = 0; tc < T; tc++) { scanf("%d", &N); memset(SiHz,0,sizeof(SiHz)); int max=0; for(i = 1; i <= N; i++) { scanf("%d", &Si[i]); if(Si[i]>max) max=Si[i]; SiHz[Si[i]]++; } int num=1; for(i = max; i >= 1; i--) { if(SiHz[i]) { SiHzOrder[i]=num; num+=SiHz[i]; } } for(i = 1; i <= N; i++) { printf("%d ", SiHzOrder[Si[i]]); } printf("\n"); } return 0;//Your program should return 0 on normal termination. }