【HDU5522 BC61 div2 A】【暴力orSET】Numbers 是否存在x+y=z

来源:互联网 发布:淘宝里怒吼的表情 编辑:程序博客网 时间:2024/05/22 08:13


Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/262144 K (Java/Others)
Total Submission(s): 156    Accepted Submission(s): 114


Problem Description
There are n numbers A1,A2....An,your task is to check whether there exists there different positive integers i, j, k (1i,j,kn) such that AiAj=Ak
 

Input
There are multiple test cases, no more than 1000 cases.
First line of each case contains a single integer n.(3n100).
Next line contains n integers A1,A2....An.(0Ai1000)
 

Output
For each case output "YES" in a single line if you find such i, j, k, otherwise output "NO".
 

Sample Input
33 1 231 0 241 1 0 2
 

Sample Output
YESNOYES
 

Source
BestCoder Round #61 (div.2)
 


#include<stdio.h>#include<string.h>#include<ctype.h>#include<math.h>#include<iostream>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=0,M=0,Z=1e9+7,ms63=1061109567;int casenum,casei;set<int>sot;int n;int a[105];bool bf(){for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){for(int k=j+1;k<=n;k++){if(a[i]+a[j]==a[k])return 1;}}}return 0;}bool nm(){sot.clear();for(int i=n;i>=1;i--){for(int j=1;j<i;j++){if(sot.find(a[i]+a[j])!=sot.end())return 1;}sot.insert(a[i]);}return 0;}int main(){while(~scanf("%d",&n)){for(int i=1;i<=n;i++)scanf("%d",&a[i]);sort(a+1,a+n+1);//puts(bf()?"YES":"NO");puts(nm()?"YES":"NO");}return 0;}/*【trick&&吐槽】水题就要有水题的做法。不要想多了,这题数据很弱。哪怕是暴力也只不过需要15ms【题意】有T(1000)组数据给你n(100)个数,每个数的数值都在[0,1000]之间。让你任意选出3个数A、B、C,问你是否有情况满足A=B+C。【类型】暴力or哈希【分析】这题首先可以排个序,然后用O(Tn^3)的做法实现,大概还是可以AC的我们思考一下有没有更高效的做法,比如O(Tn^2logn)的做法呢?当然有,枚举的数范围限定在1~i,set中存i+1以上的数,哈希一下即可。【时间复杂度&&优化】O(Tn^3) or O(Tn^2logn)*/

1 0