zoukankan      html  css  js  c++  java
  • 排序序号问题

    [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("
    ");    
        }
    
        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("
    ");    
        }
    
        return 0;//Your program should return 0 on normal termination.
    }
  • 相关阅读:
    Spring----BeanPostProcessor
    spring lifeCycle
    lambda表达式
    mybatis ----SqlSessionManager
    带权随机
    Protocol Buffers学习笔记
    Java的Lambda表达式
    mysql之group_concat函数
    Java排序之归并排序
    Java排序之计数排序
  • 原文地址:https://www.cnblogs.com/mr-redrum/p/3579834.html
Copyright © 2011-2022 走看看