Codeforce A. Going Home (Thinking + Rejection)

Topic link

The main idea of ​​the topic: give you n, let you find four subscripts, which are different from each other to satisfy ax + ay = az + aw a_x+a_y=a_z+a_wax+aand=afrom+aw.


Idea: In fact, this question is really not easy to understand, because the data n given by him is 2e 5 , and his ai a_iaiIt is 2.5e 6. First look at n it is 2e 5 , which means that it must be impossible that the complexity is O(n 2 ). Then you can only find another way. Generally, you think of enumeration, but this problem is difficult The difficulty is that he can’t let you enumerate. But you can’t enumerate. So let’s keep looking. We have to find out ax + ay = S a_x+a_y=Sax+aand=S also hasaz + zw = S a_z+z_w=Safrom+withw=S andx, y, z, wx,y,z,wx,and ,with ,w is not equal. Then I can know that his largest S is 5e6.So these numbers, if there is an answer, must be inthe range of(0, 5e6), because each number appears at most once, the worst The situation is to cover all the numbers in this range.If you come to another number, it must still be in this interval.There is a pair of numbers in the original interval, and then the next pair will make it meet our requirements.This is the worst case with an answer. This is similar to what we learned to tolerate the pigeon cage.
When we can't find our answer, then after all the numbers are enumerated, we don't find two pairs that meet the requirements, and the complexity is O(n *(n-1)/2).
All our final complexity is O(min(2C ,n(n-1)/2));
that is to say, if n*(n-1)/2 is greater than 2C then mustfind the answerat 2C, and will not continue to enumerate.

#include <set>
#include <map>
#include <queue>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<ll,ll> pii;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
const int maxn=5e6+10;
#define inf 0x3f3f3f3f
#define sf scanf
#define pf printf
const int mod=1e9+7;
const int MOD=1e9+7;

inline int read() {
    
    
    int x=0;
    bool t=false;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=true,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return t?-x:x;
}
ll n,m,d;
ll a[maxn];
ll x[maxn],y[maxn];
int main() {
    
    
    cin>>n;
    for(int i=1; i<=n; i++) {
    
    
        scanf("%lld",&a[i]);
    }
    for(ll i=1; i<=n; i++) {
    
    
        for(ll j=i+1; j<=n; j++) {
    
    
            ll sum=a[i]+a[j];
            if(x[sum]!=i&&x[sum]!=j&&y[sum]!=i&&y[sum]!=j) {
    
    
                if(x[sum]&&y[sum]) {
    
    
                    puts("YES");
                    printf("%lld %lld %lld %lld\n",x[sum],y[sum],i,j);
                    return 0;
                }
            }
            x[sum]=i;
            y[sum]=j;
        }
    }
    puts("NO");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/114777462