• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuptlym
博客园    首页    新随笔    联系   管理    订阅  订阅

nupt1207(重数问题)

1.统计重数及次数。

2.hash。%p,若冲突,则向后移动。

******************************

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5000000;
const int p=1200007;
struct Node
{
    int cnt,x;
}hash[maxn];
int main()
{
    //freopen("in","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        int x;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            int tmp=x%p;
            if(hash[tmp].cnt==0)hash[tmp].cnt++,hash[tmp].x=x;
            else if(hash[tmp].x==x)hash[tmp].cnt++;
            else
            {
                while((hash[tmp].cnt!=0) && (hash[tmp].x!=x))
                    tmp++;
                if(hash[tmp].cnt)hash[tmp].cnt++;
                else hash[tmp].cnt++,hash[tmp].x=x;
            }
        }
        if(n==1)
        {
            printf("%d\n%d\n",x,1);
            continue;
        }
        int ans1=0,ans2=0;
        for(int i=0;i<p+p;i++)
        {
            if(hash[i].cnt>ans2)
                ans2=hash[i].cnt,ans1=hash[i].x;
            else if(hash[i].cnt==ans2 && ans1>hash[i].x)
                        ans1=hash[i].x;
        }
        printf("%d\n%d\n",ans1,ans2);
    }
    return 0;
}

******************************

posted @ 2014-06-07 18:06  Yueming Li  阅读(149)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3