題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=1029php
題目大意:ios
多組數據,每組數據先給一個n,而後給n各數字,找出n各數字中出現了至少(n+1)/2次的數字並輸出(保證n爲奇數)數組
看到這題被分類進了dp我仍是有點懵逼,我不知道我這算不算dp作法。ide
我是用的桶排序思想,spa
因爲數據範圍如此,直接進行遍歷桶是會炸時間的,code
因此多開一個數組記錄下出現過的數字就行。blog
題不難排序
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int barrel[1000005],n,have[1000005],tot; int main(){ while(scanf("%d",&n)!=EOF){ tot=0;memset(barrel,0,sizeof(barrel)); for(int i=1;i<=n;i++){ int a=read(); if(barrel[a]==0)have[++tot]=a; barrel[a]++; } int num=(n+1)/2; for(int i=1;i<=tot;i++) if(barrel[have[i]]>=num){ cout<<have[i]<<endl;break; } } return 0; }