题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4334
比赛时,用的是枚举+二分,时间复杂度 ( 2 * n^3 * logn ),十几秒的时间,TLE到屎。。。。。。
想改用哈希,但是不懂肿么哈希。。。
赛后问B哥他们。。。 散列哈希。。。
蒙了,神马散列?。。。 原来在数据结构课本里有的。。。 木有仔细学(貌似曾看到,但觉得木有用。。。)。。。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define ll (v<<1)
#define rr (v<<1|1)
#define tmid (l+r>>1);
#define l64 __int64
#define mod 999997
using namespace std;
struct HASH{
l64 val;
int next;
}has[41000];
int cnt,head[mod];
int fx[41000],tol;
int n;
l64 s[5][210];
void add_hash(l64 val){
int x=val%mod;
if(x<0) x=-x;
for(int i=head[x];i!=-1;i=has[i].next)
if(has[i].val==val) return;
has[cnt].val=val;
has[cnt].next=head[x];
head[x]=cnt++;
fx[tol++]=x;
}
void deal(){
int i,j;
cnt=tol=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
add_hash(s[0][i]+s[1][j]);
}
void init_(){
while(tol--)
head[fx[tol]]=-1;
}
bool find_hash(l64 val){
int x=val%mod;
if(x<0) x=-x;
for(int i=head[x];i!=-1;i=has[i].next)
if(has[i].val==-val) return true;
return false;
}
bool res(){
int i,j,k,tmp;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++){
if(find_hash(s[2][i]+s[3][j]+s[4][k]))
return true;
}
return false;
}
int main(){
int tt,t;
int i,j;
scanf("%d",&t);
memset(head,-1,sizeof(head));
for(tt=1;tt<=t;tt++){
scanf("%d",&n);
for(i=0;i<5;i++)
for(j=0;j<n;j++)
scanf("%I64d",&s[i][j]);
deal();
if(res()) puts("Yes");
else puts("No");
init_();
}
return 0;
}