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

55 篇文章 0 订阅
19 篇文章 3 订阅


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
      
      
3 3 1 2 3 1 0 2 4 1 1 0 2
 

Sample Output
      
      
YES NO YES
 

Source
 


#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|1
typedef 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)
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值