HDU 3972 1 M possible (hash)

来源:互联网 发布:java单例读取文件 编辑:程序博客网 时间:2024/05/22 01:43

http://acm.hdu.edu.cn/showproblem.php?pid=3972


1 M possible

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 2048/1024 K (Java/Others)
Total Submission(s): 950    Accepted Submission(s): 316


Problem Description
There are 3*N+2 nonnegative integers. Except two special integers, the rest 3*N integers can form N triple groups: { Vi, Vj, Vk| Vi=Vj=Vk}, each integer are at most in one triple group.

Your task is to find the two special integers.

Now, suneast is so boring. He wants to compete with you. As there is no computer available for him, he has to find the integers use his eyes! Unbelievable! So just for fairness, your program can access ONLY 1MB memory!

Tips: Impossible itself says 1 M possible ------ Tourist’s quote (a topcoder target member)
 

Input
The first line of the input contains an integer T(T<=3), indicating the number of test cases.

Each case begin with a line containing an integers N(2<=N<=300000, N=2(mod3) ).

The second lines contains N nonnegative integers Vi (1<=Vi<=2147483647).
 

Output
For each test case, print a line containing two integers a, b (a<=b) separate by a blank. a, b is the desire two special integers.
 

Sample Input
2 2 1 2 23 1 3 1 4 5 2 1 1 3 1 4 5 2 1 1 3 1 4 5 2 1 2 1
 

Sample Output
1 2 1 2
 

Author
isuneast
 

Source
2011 Multi-University Training Contest 14 - Host by FZU
 


注意如果是5个1,有3个可以放一起,剩余的两个是special的,输出两个1,题意一定要搞清楚啊,一开始误解题意WA了好多次

哈希乱搞,注意内存只有1MB,STL的MAP显然不能用(还有指针域),开两个10万的int型数组刚刚好,散列冲突就直接往后找空位。当心如果某个数出现三次,千万不要把这个点删除,这对散列冲突会有影响,还有把记录数量的变量我用short和char替代int,最后消耗的内存居然是一样的,可能是和struct的内存分配有关吧。


#include<cstdio>#include<cstring>#define mm 100001#define MOD 90007using namespace std;struct hash{    int k,v;}h[mm];int a[2];int cnt=0;void f(int x){    int ha=x%MOD;    while (h[ha].k!=-1 && h[ha].k!=x)    {        ha++;        if (ha>=mm)            ha=0;    }    if (h[ha].k==-1)    {        h[ha].k=x;        h[ha].v=0;    }    h[ha].v++;    if (h[ha].v==3)        h[ha].v=0;}int main(){    #ifndef ONLINE_JUDGE        freopen("t.txt","r",stdin);    #endif // ONLINE_JUDGE    int tt,n,x;    scanf ("%d",&tt);    for (int t=0;t<tt;t++)    {        for (int i=0;i<mm;i++)        {            h[i].k=h[i].v=-1;        }        scanf("%d",&n);        for (int i=0;i<n;i++)        {            scanf("%d",&x);            f(x);        }        cnt=0;        for (int i=0;i<mm;i++)        {            if (h[i].k!=-1)            {                if (h[i].v==2)                {                    a[cnt++]=h[i].k;                    a[cnt++]=h[i].k;                    break;                }                if (h[i].v==1)                {                    a[cnt++]=h[i].k;                    if (cnt==2) break;                }            }        }        if (a[0]<a[1])        {            printf("%d %d\n",a[0],a[1]);        }        else        {            printf("%d %d\n",a[1],a[0]);        }    }    return 0;}


1 0
原创粉丝点击