HDU4334(多重循环)
来源:互联网 发布:js常量定义 编辑:程序博客网 时间:2024/05/16 05:19
Trouble
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3459 Accepted Submission(s): 1091
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?
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
221 -11 -11 -11 -11 -131 2 3-1 -2 -34 5 6-1 3 2-4 -10 -1
Sample Output
NoYes多重循环,也能过,注意优化数组的访问方式#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef __int64 ll;ll da1[201],da2[201],da3[201];ll two[201*201];ll four[201*201];int main(){int i,j,k1,t,n,k2;bool flag=false;cin>>t;while(t--){scanf("%d",&n);for(j=0;j<n;j++){scanf("%I64d",da1+j);}for(j=0;j<n;j++){scanf("%I64d",da2+j);}k1=0;for(i=0;i<n;i++){for(j=0;j<n;j++){two[k1++]=da1[i]+da2[j];}}sort(two,two+k1);for(j=0;j<n;j++){scanf("%I64d",da1+j);}for(j=0;j<n;j++){scanf("%I64d",da2+j);}k2=0;for(i=0;i<n;i++){for(j=0;j<n;j++){four[k2++]=da1[i]+da2[j];}}sort(four,four+k2);for(j=0;j<n;j++){scanf("%I64d",da1+j);}sort(da1,da1+n);flag=false;for(i=0;i<n;i++){int left=0;int right=k2-1;while(left<k1&&right>=0){if(-da1[i]==four[right]+two[left]){flag=true;break;}if(four[right]+two[left]<-da1[i])left++;else right--;}if(flag)break;} if(flag) puts("Yes"); else puts("No");}return 0;}
- HDU4334(多重循环)
- hdu4334
- hdu4334
- 多重循环
- 多重循环
- 多重循环
- HDU4334 Trouble
- hdu4334 trouble
- 跳出多重循环
- java跳出多重循环
- python跳出多重循环
- 跳出多重for循环
- java跳出多重循环
- Java跳出多重循环
- Java跳出多重循环
- 多重循环的优化
- 代码优化:多重循环
- iBtais 多重嵌套循环
- 浅谈国际化
- SVM入门经典讲解(相当好)
- Spring3+MyBatis3整合无法输出SQL语句问题的解决
- 在Eclipse中导入hadoop
- 【wpf】利用wpf写的一个登录界面--美感为零的程序员伤不起
- HDU4334(多重循环)
- 批处理常用命令总结
- 光流OpticalFlow介绍与OpenCV实现
- 强行删除oracle新建的用户
- 【Android UI设计与开发】第10期:顶部标题栏(一)ActionBar详细概述和简单示例
- 简易消息提示框Toast和通知的使用
- yoda's Crypter 1.2源码分析
- 【堆】【优先队列】POJ2051-Argus-【容易题】
- HTML5 离线缓存