HDU 4334 Trouble (暴力)
2016-05-24 10:13
274 查看
Trouble
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。
Time Limit: 5000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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[40005],b[40005],c[205],d[205],e[205],f[205],g[205]; long long i,j,x,k,l,n,t,flag=0,p; int main() { scanf("%lld",&t); while(t--) { flag=0; //输入 scanf("%lld",&n); for(i=0;i<n;i++) scanf("%lld",&d[i]); for(i=0;i<n;i++) scanf("%lld",&e[i]); for(i=0;i<n;i++) scanf("%lld",&f[i]); for(i=0;i<n;i++) scanf("%lld",&g[i]); for(i=0;i<n;i++) scanf("%lld",&c[i]); //合并 int cnt1=0; for(i=0;i<n;i++) for(j=0;j<n;j++) a[cnt1++]=d[i]+e[j]; int cnt2=0; for(i=0;i<n;i++) for(j=0;j<n;j++) b[cnt2++]=f[i]+g[j]; //三四合并 sort(a,a+cnt1); sort(b,b+cnt2); //计算 for(i=0;i<n;i++) //第五个数组 c[] { j=0; k=cnt2-1; while(j<cnt1 && k>=0) { if(a[j]+b[k]+c[i]==0LL) { flag=1; break; } else if(a[j]+b[k]+c[i]<0) j++; else k--; } } if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
相关文章推荐
- 逻辑运算符与关系运算符
- UVA 10325 The Lottery( 容斥原理)
- Python实现二叉树结构与进行二叉树遍历的方法详解
- HDU 2841 Visible Trees 数论+容斥原理
- android 调用百度地图,高德地图第三方APP进行导航
- ActiveMQ之细嚼慢咽
- UVA11806-Cheerleaders(容斥原理+二进制)
- HDU1695 GCD (欧拉函数+容斥原理)
- 网易视频云技术分享:HBase高可用原理与实践
- Java的异常处理
- Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
- Drupal 8 安装模块时报错
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增消息管理
- 查看Linux系统版本信息
- python callable()函数
- HDU 5651 xiaoxin juju needs help 逆元
- HDU 1521 排列组合 指数型母函数
- Maven安装与环境配置(Windows)
- HDU 1023 Traning Problem (2) 大数卡特兰数
- HDU 2082 母函数模板题