zoukankan      html  css  js  c++  java
  • 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,使得 a x + a y = a z + a w a_{x}+a_{y}=a_{z}+a_{w} ax+ay=az+aw

    思路:

    比赛的时候想到了题解的思路,但是觉得 n 2 n^{2} n2的复杂度过不了。
    后来看了看讨论区,其实当n足够大时,一定是有解的。一个数的范围是2.5e5,他们的和的范围为5e5,所以在有限次一定能得到答案。
    当n比较小,没有答案时,时间复杂度才是 n 2 n^{2} n2
    讨论区有人提出当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;
    }
    
    
  • 相关阅读:
    SVN打tag
    validate命令---rman进行备份和回复的验证
    通达OA 小飞鱼工作流在线培训教程(一)HTML基础介绍
    How to improve Java&#39;s I/O performance( 提升 java i/o 性能)
    mybatis批量插入、批量删除
    Java測试覆盖率工具----Cobertura,EclEmma
    Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
    灵活数据源的固定行列交叉报表的制作
    HDFS学习笔记(1)初探HDFS
    JPEG压缩图像超分辨率重建算法
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853022.html
Copyright © 2011-2022 走看看