hdu 1280 前m大的數 雜湊,hdu1280

來源:互聯網
上載者:User

hdu 1280 前m大的數 雜湊,hdu1280
前m大的數Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10574    Accepted Submission(s): 3686


Problem Description還記得Gardon給小希布置的那個作業嗎?(上次比賽的1005)其實小希已經找回了原來的那張數表,現在她想確認一下她的答案是否正確,但是整個的答案是很龐大的表,小希只想讓你把答案中最大的M個數告訴她就可以了。 
給定一個包含N(N<=3000)個正整數的序列,每個數不超過5000,對它們兩兩相加得到的N*(N-1)/2個和,求出其中前M大的數(M<=1000)並按從大到小的順序排列。 
Input輸入可能包含多組資料,其中每組資料包括兩行: 
第一行兩個數N和M, 
第二行N個數,表示該序列。

 
Output對於輸入的每組資料,輸出M個數,表示結果。輸出應當按照從大到小的順序排列。 
Sample Input

4 41 2 3 44 55 3 6 4
 
Sample Output
7 6 5 511 10 9 9 8
 
統計每個數出現的次數 然後從大到小輸出
#include<stdio.h>#include<cstring>int main(){    int hash[10010];    int a[3010];    int n,m;    int i,j;    while(scanf("%d %d",&n,&m)!=EOF)    {        memset(hash,0,sizeof(hash));        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);        }        int xx=0;        for(i=0;i<n;i++)        {            for(j=0;j<i;j++)            {                hash[a[i]+a[j]]++;                if(a[i]+a[j]>xx)                {                    xx=a[i]+a[j];                }            }        }        for(i=xx;i>=0;i--)        {            while(hash[i]&&m>1)            {                printf("%d ",i);                hash[i]--;                m--;            }            if(m==1&&hash[i])            {                printf("%d\n",i);                break;            }        }    }    return 0;}



HDU 1800 剛接觸hash 下面程式看不太懂解釋 特別是hash與fun函數的內容

hash 就是把一個key運算得到一個hash值.
這樣可以方便對比相同的key.
 
hdu3064,我都用char的hash[1000111]了,還是要1100的記憶體,而限制1024的,這題該咋弄呀,囧,謝

這題不用那麼複雜吧, 兩次遍曆就行了啊。
開一個1000001的字元數組, 統計每個區間, 存在某個區間,就把數組元素的下標在這個區間的元素的值標記,表示存在這個值,然後再從最小的到最大的遍曆這個數組就行了。
我的AC代碼:
#include <stdio.h>
#include <string.h>
char value[1000001];
int main()
{
int n;
int i, j;
int min, max;
int count, a, b;
while (scanf ("%d", &n) == 1)
{
scanf ("%d%d", &min, &max);
for (i = min; i <= max; i ++)
{
value[i] = '0';
}
for (i = 0; i < n; i ++)
{
scanf ("%d%d", &a, &b);
for (j = a; j <= b; j ++)
{
value[j] = '1';
}
}
count = 0;
for (i = min; i <= max; i ++)
{
if (count >= 2)
{
break;
}
if (value[i] == '0')
{
count ++;
printf ("%d", i);
if (count < 2)
{
printf (" ");
}
else
{
printf ("\n");
}
}
}
}
return 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.