coder_new

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[Input Example]

2

10

1 2 3 4 5 6 7 8 9 9

10

27 22 15 30 29 12 20 13 6 10

[Output Example]

10 9 8 7 6 5 4 3 1 1

3 4 6 1 2 8 5 7 10 9

1.排序

#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.
}

 

 

posted on 2014-03-04 10:26  coder_new  阅读(240)  评论(0编辑  收藏  举报