C. Going Home (Thinking + Drawer Principle)

https://codeforces.com/contest/1501/problem/C


Ideas:

When I did it, I looked at the range and n, and I only knew that there would be collisions, which would not be so big, but the specific structure was not very good.

Given that the value range is 2.5e6, open an array of 5e6, and throw the n² violent enumeration into the bucket. That is cnt[a[i] + a[j]] ++;

If this cnt >= 2, then the answer is found.

In the worst case, a certain position will inevitably appear in the cycle 5e6 + 1 times group cnt[i] >= 2

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e6+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[200010],b[maxn],c[maxn],cnt[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++) cin>>a[i];
  for(LL i=1;i<=n;i++){
    for(LL j=i+1;j<=n;j++){
        cnt[a[i]+a[j]]++;
        if(!b[a[i]+a[j]] ) b[a[i]+a[j]]=i;
        if(!c[a[i]+a[j]] ) c[a[i]+a[j]]=j;
        if(cnt[a[i]+a[j]]>=2&&b[a[i]+a[j]]!=i&&b[a[i]+a[j]]!=j&&c[a[i]+a[j]]!=i&&c[a[i]+a[j]]!=j){
            cout<<"YES"<<"\n";
            cout<<b[a[i]+a[j]]<<" "<<c[a[i]+a[j]]<<" "<<i<<" "<<j<<"\n";
            return 0;
        }
    }
  }
  cout<<"NO"<<"\n";
return 0;
}


 


 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/114767368