hdu4334 Trouble

Trouble

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


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
2012 Multi-University Training Contest 4
唉,当时,也是用了各种方法优化啊,用搜索,反正是能用的都用了,花了不少时间啊,还是超时,后来才发现,要把第一层和第二层合成一层,第三四合成一层最后是一层,这样
就能过了,我还排序去重,最后发现,没有变的多快,估计,是因为,测试数据得出的没有多少重复吧!
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 40001
__int64 dp[2][MAXN],num[5][MAXN];
bool cmp(__int64 a,__int64 b)
{
    return a<b;
}
int main ()
{
    int tcase,n,i,j,k,len0,len1,len2,s;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d",&n);
        for(i=0;i<5;i++)
            for(j=0;j<n;j++)
            {
                scanf("%I64d",&num[i][j]);
            }
        s=0;
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            {
                dp[0][s++]=num[0][i]+num[1][j];
            }
        sort(dp[0],dp[0]+s,cmp);
        for(i=1,j=0;i<s;i++)
        {
            if(dp[0][i]!=dp[0][j])
            {
                dp[0][++j]=dp[0][i];
            }
        }
        len0=j+1;
        s=0;
         for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            {
                dp[1][s++]=num[2][i]+num[3][j];
            }
        sort(dp[1],dp[1]+s,cmp);
        for(i=1,j=0;i<s;i++)
        {
            if(dp[1][i]!=dp[1][j])
            {
                dp[1][++j]=dp[1][i];
            }
        }
        len1=j+1;
        sort(num[4],num[4]+n,cmp);
        for(i=1,j=0;i<n;i++)
        {
            if(num[4][i]!=num[4][j])
            {
                num[4][++j]=num[4][i];
            }
        }
        len2=j+1;
        bool flag=true;
        for(i=0;i<len2;i++)
        {
            j=0;k=len1-1;
           while(k>=0&&j<len0)
           {
               __int64 temp=dp[0][j]+dp[1][k]+num[4][i];
               if(temp>0)
               {
                   k--;
               }
               else if(temp<0)
               {
                   j++;
               }
               else if(temp==0)
               {
                   printf("Yes\n");
                   flag=false;
                   break;
               }
           }
           if(!flag)
           {
               break;
           }
        }
        if(flag)
        {
            printf("No\n");
        }
    }
    return 0;
}

你可能感兴趣的:(hdu4334 Trouble)