hdu 4334 Trouble

Trouble

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


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
 

题意:

给定五个集合,从五个集合中分别取出5个数,如果存在满足a1+a2+a3+a4+a5 = 0 输出yes,否则no; 集合的大小小于200 ai的取值为 [-10^15, 1 0^15]。

思路:

将前1,2数合并(将他们组合得到的和存进数组)到up[]。将3,4行数合并到down[]。然后将得到的两个数组排序。然后枚举最后一行数。对于最后一行的每一个数维护两个指针l和r。开始分别指向up的最右端和down的最左端。

然后根据temp=up[r]+down[l]+a[i]来移动指针。如果temp>0则r--。temp<0。l++。temp=0。找到答案了。直接跳出。为什么l,r可以直接变化呢?

只是利用了贪心思想。如果temp>0。说明值大了up[r]已经没有比较的价值了。因为在down[l]后面的都是比down[l]大的。up[r]加他们肯定更大。temp<0同理。

感觉这种方法效率很高。可惜自己没想到。自己要学习的东西还有很多呀。。。。。

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

__int64 num[5][210];//数据较大必须用__int64.
__int64 up[40010],down[40010],temp;//temp是__int64啊。。又被坑了。。
int main()
{
    int i,j,t,n,l,r,flag,cnt,ptr;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        flag=0;
        cnt=ptr=0;
        for(i=0; i<5; i++)
            for(j=0; j<n; j++)
                scanf("%I64d",&num[i][j]);
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
                up[cnt++]=num[0][i]+num[1][j];//存前两层的和
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
                down[ptr++]=num[2][i]+num[3][j];//存后两层的和
        sort(up,up+cnt);
        sort(down,down+ptr);
        for(i=0; i<n; i++)
        {
            r=cnt-1;
            l=0;
            while(l<ptr&&r>=0)//贪心思想减少比较次数
            {
                temp=up[r]+down[l]+num[4][i];
                if(temp>0)
                    r--;
                else if(temp==0)
                {
                    flag=1;
                    break;
                }
                else
                    l++;
            }
            if(flag)
                break;
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值