Going Home-(Drawer Principle)

Topic link: click to enter

topic

Insert picture description here
Insert picture description here

Title

Give an array of length n and ask whether there are x, y, z, w (the four numbers are different from each other), satisfying a [x] + a [y] = a [z] + a [w];

Ideas

Violently enumerate x and y, and get the sum C, 2 <= C <= 5e6.
There are n ^ 2 pairs (x, y ), and the sum value C has about 5e6 values. When n ^ 2> 5e6, according to the drawer principle, there must be one sum value corresponding to more than two (x, y) pairs, However, at this time, the corresponding (x, y) pairs may have the same value, but for a sum value C, when it appears four times, we must be able to find the answer, so the complexity exceeds O( 4 * C ), so The final time complexity is O( min (n ^ 2, C) ).

Code

#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;
}

Guess you like

Origin blog.csdn.net/Cosmic_Tree/article/details/114895671