HDU 1425 sort (hash)

來源:互聯網
上載者:User

標籤:hdu   1425   hash   

sortTime Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29001    Accepted Submission(s): 8799


Problem Description給你n個整數,請按從大到小的順序輸出其中前m大的數。
 
Input每組測試資料有兩行,第一行有兩個數n,m(0<n,m<1000000),第二行包含n個各不相同,且都處於區間[-500000,500000]的整數。
 
Output對每組測試資料按從大到小的順序輸出前m大的數。
 
Sample Input
5 33 -35 92 213 -644
 
Sample Output
213 92 3

本題由於資料的特殊性,還能用hash思想來做:

#include<iostream>#include<stdio.h>int hash[1000010];int main(int argc, char *argv[]){    int n,m;    int t;    int cnt;    //freopen("1425.in","r",stdin);    while(scanf("%d %d",&n,&m)==2)    {        memset(hash,0,sizeof(hash));        for(int i=0;i<n;++i)        {            scanf("%d",&t);            hash[t+500000]=1;        }        for(int i=1000010;i>0;i--)        {            if(hash[i])            {                printf("%d",i-500000);                m--;                if(m)                    printf(" ");            }            if(m==0) break;        }        printf("\n");    }    return 0;}



HDU 1425 sort (hash)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.