题目链接:点击进入
题目
题意
给一个长度为 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;
}