hdu 4334 Trouble

Trouble

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1159    Accepted Submission(s): 375


Problem Description
Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. Please help him.
The 5-sum problem is defined as follows: Given 5 sets S_1,...,S_5 of n integer numbers each, is there a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0?
 

 

Input
First line of input contains a single integer N (1≤N≤50). N test-cases follow. First line of each test-case contains a single integer n (1<=n<=200). 5 lines follow each containing n integer numbers in range [-10^15, 1 0^15]. I-th line denotes set S_i for 1<=i<=5.
 

 

Output
For each test-case output "Yes" (without quotes) if there are a_1 in S_1,...,a_5 in S_5 such that a_1+...+a_5=0, otherwise output "No".
 

 

Sample Input
2 2 1 -1 1 -1 1 -1 1 -1 1 -1 3 1 2 3 -1 -2 -3 4 5 6 -1 3 2 -4 -10 -1
 

 

Sample Output
No Yes
 

 

Source
 

 

Recommend
zhoujiaqi2010

AC代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
#define ll __int64
ll a[400],b[400],c[400],e[400],u[400];
ll f[400*400],g[400*400];
int flag;
bool cmp( ll x,ll y)
{
 return x<y;
}
int main( )
{
  int i,tests,j,t;
  int n,cnt,cnt1;
  scanf("%d",&tests);
 
  while(tests--)
  {
 cnt=0,cnt1=0;
 flag=0;
 scanf("%d",&n);
 for( i=1;i<=n;i++) scanf("%I64d",&a[i]);
 for( i=1;i<=n;i++) scanf("%I64d",&b[i]);
 for( i=1;i<=n;i++)
 {
   for( j=1;j<=n;j++)
   {
  f[++cnt]=a[i]+b[j];
   }
 }
 for( i=1;i<=n;i++) scanf("%I64d",&c[i]);
 for( i=1;i<=n;i++) scanf("%I64d",&e[i]);
 for( i=1;i<=n;i++)
 {
   for( j=1;j<=n;j++)
   {
  g[++cnt1]=c[i]+e[j];
   }
 }
  
 for( i=1;i<=n;i++) scanf("%I64d",&u[i]);
 sort(f+1,f+cnt+1,cmp);
 sort(g+1,g+cnt1+1,cmp);
 sort(u+1,u+n+1,cmp);
 for( i=1;i<=n;i++)
 {
   ll  tmp=u[i];
   j=1,t=cnt;
   while(j<=cnt1&&t>=1)//对于这一段,开始用最小的加最大的,如果和小于要查找的话,那么说明大的不够用,小的就加,即j++

反之就 k--了


   {
  if(g[j]+f[t]==-tmp)
  {
    flag=1;break;
     }
     if(g[j]+f[t]<-tmp) j++;
     else t--;
   }
    }
    if(flag) printf("Yes\n");
    else printf("No\n");
 
  }
  return 0;
 
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4334

你可能感兴趣的:(HDU)