原题链接:
(https://codeforces.com/contest/1501/problem/C)
原题:
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
input
6
2 1 5 2 7 4
output
YES
2 3 1 6
input
5
1 3 1 9 20
output
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
题解:
题意中让找出某两个数相加等于另外两个数相加的数组中的四个数。
对于这题不难看出可以用pair来储存其中前两个数的和以及这两个数,
当这个和第一次出现时将其及其两个数储存在pair里面当再出现这个和时判断此时的i和j是否和之间储存的两个有重复的,如果都不重复则输出‘YES’再输出呢四个数,否则输出’NO‘。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 200005
#define M 5000000
using namespace std;
int n,a[N];
pair<int ,pair<int,int> > p[M];
int main()
{
ios::sync_with_stdio(0);//使cin输入的速度更快。
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
int x=a[i]+a[j];
p[x].first++;
if(p[x].first==1)
p[x].second.first=i , p[x].second.second=j;
else
{
int z=p[x].second.first, w=p[x].second.second;
if(z!=i&&w!=i&&z!=j&&w!=j)
{
cout<<"YES\n"<<z<<" "<<w<<" "<<i<<" "<<j;
return 0;
}
}
}
cout<<"NO";
}