数据量小得可怜,完全可以最用朴素的方法。
本代码:快速排序,二分查找输出。
#include <stdio.h> typedef struct node { int val,idx; }node; node c[10000]; int d[10000]; void swap(node *a,node *b) { int t; t = a->val; a->val = b->val; b->val = t; } void quick(int left,int right) { int l,r; l = left +1; r = right; if(left >=right)return; int key = c[left].val; while(1) { while(c[r].val> key)r--; while(c[l].val<key&&l<r)l++; if(l>=r)break; swap(&c[l],&c[r]); if(c[l].val == c[left].val)r--; else l++; } swap(&c[left],&c[r]); if(left<l-1)quick(left, l-1); if(r+1<right)quick(r+1, right); } int binary0(int l,int r,int key) { int mid = (l+r)/2; if(c[mid].val == key)return c[mid].idx; if(c[mid].val>key)return binary0(l, mid-1, key); else return binary0(mid+1, r, key); } int main(int argc, const char * argv[]) { int n,i; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%d",&c[i].val); d[i] = c[i].val; //c[i].idx = i; } quick(0, n-1); c[0].idx = 1; for (i=1; i<n; i++) { if(c[i].val!=c[i-1].val){c[i].idx = c[i-1].idx+1;} else c[i].idx = c[i-1].idx; } for (i=0; i<n; i++) { if(i!=0)printf(" "); printf("%d",binary0(0, n-1, d[i])); } printf(" "); } }