Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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

题意:给定五个集合,每个集合有n个数,从每个集合各取一个数使和为0。如果能输出Yes,不能则输出No。

    看了网上的题解有很多解法,个人感觉这题暴力就能过。

    首先将一二两个集合合并为数组a,再将三四两个集合合并数组b,然后进行从小到大排序,

    设第五个数组为数组c,依次遍历c中的每一个数,看在a,b中是否存在两个数的和与c中的数的和为0,

    将数组a,b进行sort排序,然后一个正序即从小到大遍历,一个逆序从大到小遍历,

    如果三个数的和小于0,a数组后移一位,否则b后移一位。

    这题WA了两次,第一次WA后发现else if用的是三个i,忘了改成i,j,k了。

    第二次WA后发现Yes输出成了YES,No输出成了NO。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
long long a[],b[],c[],d[],e[],f[],g[];
long long i,j,x,k,l,n,t,flag=,p;
int main()
{
scanf("%lld",&t);
while(t--)
{
flag=;
//输入
scanf("%lld",&n);
for(i=;i<n;i++)
scanf("%lld",&d[i]);
for(i=;i<n;i++)
scanf("%lld",&e[i]);
for(i=;i<n;i++)
scanf("%lld",&f[i]);
for(i=;i<n;i++)
scanf("%lld",&g[i]);
for(i=;i<n;i++)
scanf("%lld",&c[i]);
//合并
int cnt1=;
for(i=;i<n;i++)
for(j=;j<n;j++)
a[cnt1++]=d[i]+e[j];
int cnt2=;
for(i=;i<n;i++)
for(j=;j<n;j++)
b[cnt2++]=f[i]+g[j]; //三四合并
sort(a,a+cnt1);
sort(b,b+cnt2);
//计算
for(i=;i<n;i++) //第五个数组 c[]
{
j=;
k=cnt2-;
while(j<cnt1 && k>=)
{
if(a[j]+b[k]+c[i]==0LL)
{
flag=;
break;
}
else if(a[j]+b[k]+c[i]<)
j++;
else
k--;
} }
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return ;
}

HDU 4334 Trouble (暴力)的更多相关文章

  1. HDU 4334 Trouble

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDU 4334 Trouble (数组合并)

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 4334——Trouble——————【贪心&水题】

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. HDU 4334 Trouble(哈希|线性查找)

    给定五个集合.问是否能从五个集合各取一个元素,使得元素之和为0. 这道题有两种做法,一种是哈希,然而之前没写过哈希.....比赛后从大神那copy了一份. 这里说还有一种. 对于这五个集合分为三组.1 ...

  5. [hdu P4334] Trouble

    [hdu P4334] Trouble Hassan is in trouble. His mathematics teacher has given him a very difficult pro ...

  6. Trouble HDU - 4334

    Hassan is in trouble. His mathematics teacher has given him a very difficult problem called 5-sum. P ...

  7. HDU OJ 4334 Trouble 2012 Multi-University Training Contest 4

    题目:click here 题意: 给定5组数据,每组数据选择一个数,看是否能找到5个数的和为零. 分析: 千万不要~~T~~ 普通线性查找: #include <iostream> #i ...

  8. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  9. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

随机推荐

  1. html,body { margin:0; padding:0;border:0}

    body,html /* 设置窗口DIV为浏览器大小*/ { margin:; padding:; height:100%; } 下面代码 <!DOCTYPE html> <html ...

  2. jQuery EasyUI datagrid实现本地分页的方法

    http://www.codeweblog.com/jquery-easyui-datagrid%e5%ae%9e%e7%8e%b0%e6%9c%ac%e5%9c%b0%e5%88%86%e9%a1% ...

  3. chrome 插件开发

    写在开头: 相当有意思的UI界面,编码实现,浏览速度.对于一天浏览器使用超过10个小时的人来说,能够定制自己的浏览器,是相当的具有诱惑力. Getting Started 1:目前不支持标准发布版本的 ...

  4. 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样)

    1.新建一个继承自UITableViewCell的子类  2. 在initWithStyle:方法中进行子控件的初始化 1> 将有可能显示的所有子控件都添加到contentView中 2> ...

  5. linux (centos) 单机50w+链接 内核参数配置

    1 突破系统最大fd   查看当前文件描述符的限制数目的命令: ulimit -n .修改文件描述符的限制数目 2.1 临时改变当前会话: ulimit -n 2.2 永久变更需要下面两个步骤: ./ ...

  6. FCKeditor漏洞利用

    FCKeditor漏洞利用 查看编辑器版本 FCKeditor/_whatsnew.html fckeditor/editor/dialog/fck_about.html —————————————— ...

  7. redhat RHEL 5.5 下载地址

    redhat RHEL 5.5 下载地址 RHEL 5 update 5 已经release许久了, redhat RHEL 5.5 下载地址: RHEL 5 安装 序列号 rhel-server-5 ...

  8. Java-TCP Socket编程

    TCP 的 Java 支持 协议相当于相互通信的程序间达成的一种约定,它规定了分组报文的结构.交换方式.包含的意义以及怎样对报文所包含的信息进行解析,TCP/IP 协议族有 IP 协议.TCP 协议和 ...

  9. Socket 学习入门

    http://www.codeproject.com/Articles/13071/Programming-Windows-TCP-Sockets-in-C-for-the-Begin

  10. 在Sublime Text3 开发Node.js遇到的一个小问题

    原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 以前的Sublime Text 2包管理出现问题了,不能安装新包,让人开发很捉急,今天装了个3,这个问题解决了 那我们就 ...