1501C. Going Home

time limit per test2 seconds memory limit per test256 megabytes
inputstandard input outputstandard output It was the third month of
remote learning, Nastya got sick of staying at dormitory, so she
decided to return to her hometown. In order to make her trip more
entertaining, one of Nastya’s friend presented her an integer array a.

Several hours after starting her journey home Nastya remembered about
the present. To entertain herself she decided to check, are there four
different indices x,y,z,w such that ax+ay=az+aw.

Her train has already arrived the destination, but she still hasn’t
found the answer. Can you help her unravel the mystery?

Input The first line contains the single integer n (4≤n≤200000) — the
size of the array.

The second line contains n integers a1,a2,…,an (1≤ai≤2.5⋅106).

Output Print “YES” if there are such four indices, and “NO” otherwise.

If such indices exist, print these indices x, y, z and w
(1≤x,y,z,w≤n).

If there are multiple answers, print any of them.

Examples inputCopy 6 2 1 5 2 7 4 outputCopy YES 2 3 1 6 inputCopy 5 1
3 1 9 20 outputCopy NO Note In the first example a2+a3=1+5=2+4=a1+a6.
Note that there are other answer, for example, 2 3 4 6.

In the second example, we can’t choose four indices. The answer 1 2 2
3 is wrong, because indices should be different, despite that
a1+a2=1+3=3+1=a2+a3

用O(n^2)的方法显然不现实
可以用vector来储存和为定值的两个一组的数字,每次遍历检验j和i+j是否是答案,如果是就输出。不是就加入到vector中。
复杂度O(n(n-1)/2)
其实就是抽屉原理,值域5e6,最差情况循环5e6+1次就能得出结果

//cyc
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mst(a) memset(a,0,sizeof a)
#define int long long
using namespace std;
typedef pair<int,int> pii;
const int maxn=2e5+5;
const int maxp=2e6+5e5+5;
int vis[maxn];
vector<pii> vec[maxp*2];
signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,v;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>vis[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i;j++){
            for(auto&it:vec[vis[j]+vis[i+j]]){
                if(it.fi!=(i+j)&&it.se!=j&&it.fi!=j&&it.se!=(i+j)){
                    cout<<"YES"<<endl;
                    cout<<it.fi<<" "<<it.se<<" "<<i+j<<" "<<j<<endl;
                    return 0;
                }
            }
            vec[vis[j]+vis[i+j]].pb({j,i+j});
        }
    }
    cout<<"NO"<<endl;
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值