Codeforces1501——C. Going Home(鸽巢原理)

简介: Codeforces1501——C. Going Home(鸽巢原理)

原题链接

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

题意:

给定一个2e5的序列,保证序列的每个数都小于2.5e5,请你找出四个不同的下标x,y,z,w,使得ax+ay=az+aw

思路:

比赛的时候想到了题解的思路,但是觉得n 2 n^{2}n 2 的复杂度过不了。

后来看了看讨论区,其实当n足够大时,一定是有解的。一个数的范围是2.5e5,他们的和的范围为5e5,所以在有限次一定能得到答案。

当n比较小,没有答案时,时间复杂度才是n 2 n^{2}n 2。讨论区有人提出当n>=3162时一定出YES。

代码:

update:之前的代码fst了

正解:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const double eps = 1e-8;
const int maxn =5000005;
int n,a[maxn];
PII pos[maxn];
int main()
{
    n=read;
    rep(i,1,n) a[i]=read;
    ///sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
      for(int j=i+1;j<=n;j++){
        ll sum=a[i]+a[j];
        if(pos[sum].first==0||pos[sum].second==0){
          pos[sum].first=i;
          pos[sum].second=j;
        }
        else if(pos[sum].first!=j&&pos[sum].second!=i&&pos[sum].first!=i&&pos[sum].second!=j){
          puts("YES");
          cout<<i<<" "<<j<<" "<<pos[sum].first<<" "<<pos[sum].second<<endl;
          return 0;
        }
      }
    }
    puts("NO");
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const int maxn=2e5+100,inf=0x3f3f3f3f;
#define PI acos(-1)
int n,a[maxn];
int main()
{
    n=read;
    rep(i,1,n) a[i]=read;
    map<int,PII>mp;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++){
            int x=a[i]+a[j];
            if(mp.count(x)){
                int t1=mp[x].first,t2=mp[x].second;
                if(i!=t1&&i!=t2&&j!=t1&&j!=t2){
                    puts("YES");
                    cout<<i<<" "<<j<<" "<<t1<<" "<<t2<<endl;
                    return 0;
                }
            }
            mp[x]={i,j};
        }
    puts("NO");
    return 0;
}
目录
相关文章
|
5月前
【洛谷 P1219】[USACO1.5]八皇后 Checker Challenge 题解(深度优先搜索+回溯法)
**USACO1.5八皇后挑战**是关于在$n\times n$棋盘上放置棋子的,确保每行、每列及两条主对角线上各有一个且仅有一个棋子。给定$6$作为输入,输出前$3$个解及解的总数。例如,对于$6\times6$棋盘,正确输出应包括解的序列和总数。代码使用DFS解决,通过跟踪对角线占用状态避免冲突。当找到所有解时,显示前三个并计数。样例输入$6$产生输出为解的前三个排列和总数$4$。
37 0
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
132 0
|
算法 C语言 C++
Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)
Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)
43 0
|
机器学习/深度学习 Windows
The 2022 ICPC Asia Regionals Online Contest (I)-D题题解(DFS暴搜+剪枝+打表去重+二分)
时间复杂度:O(227 + nlog(n) + T * log(n)),227是DFS打表的时间,nlog(n)是vertor排序的时间,T*log(n)是询问次数和二分搜答案的时间。(如果算错了,评论或者私信指出谢谢)
169 0
The 2022 ICPC Asia Regionals Online Contest (I)-D题题解(DFS暴搜+剪枝+打表去重+二分)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
129 0
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
|
Java Shell
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
154 0
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
2019CCPC秦皇岛HDU - 6736 F - Forest Program(dfs找环 组合数学)
98 0
|
算法 前端开发 程序员
「LeetCode」283-移动零⚡️
「LeetCode」283-移动零⚡️
117 0
「LeetCode」283-移动零⚡️