Going Home - ( 抽屉原理 )

题目链接:点击进入
题目

在这里插入图片描述
在这里插入图片描述

题意

给一个长度为 n 的数组,问是否存在 x , y , z , w ( 四个数互不相同 ) ,满足 a [ x ] + a [ y ] = a [ z ] + a [ w ] ;

思路

暴力枚举 x 与 y ,得出和值 C ,2 <= C <= 5e6。
有 n ^ 2 对( x , y ),和值 C 大约有 5e6 种取值,当 n ^ 2 > 5e6 时,根据抽屉原理,必有一个和值对应两个以上的( x , y )对,但是,此时可能对应的( x , y )对有相同值,可是对于一个和值 C ,当它出现四次的时候,我们一定可以找到答案,那么复杂度超过O( 4 * C ),所以最终时间复杂度为O( min ( n ^ 2 , C ) )。

代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
//#define int long long
//#define double long double
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e7+10;
const int mod=1e9+7;
const double eps=1e-9;
int t,n,m,a[maxn];
vector<pii>v[maxn];
int main()
{	
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    	cin>>a[i];
    for(int i=1;i<=n;i++)
    {
    	for(int j=1;j<i;j++)
    	{
    		for(auto t:v[a[i]+a[j]])
    		{
    			if(t.first==i||t.first==j) continue;
    			if(t.second==i||t.second==j) continue;
				cout<<"YES"<<endl;
				cout<<t.first<<' '<<t.second<<' '<<i<<' '<<j<<endl;
				return 0;
			}
    		v[a[i]+a[j]].push_back({i,j});
		}
	}
	cout<<"NO"<<endl;
    return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值