题目大意: 给出你n给出,让你找出四个下标,互不相同使得满足 a x + a y = a z + a w a_x+a_y=a_z+a_w ax+ay=az+aw.
思路: 其实这个题真的不太好理解,因为他给出的数据n是2e5的,而他的
a
i
a_i
ai是2.5e6.首先看n它是2e5的就说明一定不可能复杂度是O(n2)的.那么只能是另谋其径.一般看到这都想到枚举,但是这个题难就难在他不能让你枚举.但是你有不能不枚举.所以我们继续看.我们要找出
a
x
+
a
y
=
S
a_x+a_y=S
ax+ay=S同时还有
a
z
+
z
w
=
S
a_z+z_w=S
az+zw=S并且
x
,
y
,
z
,
w
x,y,z,w
x,y,z,w各不相等.那么我可以就可以知道他最大的S是5e6.所以这些数,要是有答案就一定在(0,5e6)这个范围之内,因为每个数最多出现一次,最坏的情况就是把所有这个范围所有数全部覆盖,如果再来一个数,一定还在这区间内,一位原本区间的数都有一对了,那么再来的这一对就会使其满足我们要求的.这是有答案并且最坏的情况.这个就跟我们学得容斥鸽笼差不多.
当我们不能找我们答案,那么就是所有数枚举之后没有找到两对符合要求的,复杂度就是O(n*(n-1)/2).
所有我们最终的复杂度就是O(min(2C ,n(n-1)/2));
也就是说如果n*(n-1)/2大于2C那么一定在2C的时候就找到答案了不会继续枚举完.
#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;
}