Good Bye 2018 E. New Year and the Acquaintance Estimation

E. New Year and the Acquaintance Estimation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if aa is a friend of bb, then bb is also a friend of aa. Each user thus has a non-negative amount of friends.

This morning, somebody anonymously sent Bob the following link: graph realization problem and Bob wants to know who that was. In order to do that, he first needs to know how the social network looks like. He investigated the profile of every other person on the network and noted down the number of his friends. However, he neglected to note down the number of his friends. Help him find out how many friends he has. Since there may be many possible answers, print all of them.

Input

The first line contains one integer nn (1≤n≤5⋅1051≤n≤5⋅105), the number of people on the network excluding Bob.

The second line contains nn numbers a1,a2,…,ana1,a2,…,an (0≤ai≤n0≤ai≤n), with aiai being the number of people that person ii is a friend of.

Output

Print all possible values of an+1an+1 — the amount of people that Bob can be friend of, in increasing order.

If no solution exists, output −1−1.

Examples

input

Copy

3
3 3 3

output

Copy

3 

input

Copy

4
1 1 1 1

output

Copy

0 2 4 

input

Copy

2
0 2

output

Copy

-1

input

Copy

35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22

output

Copy

13 15 17 19 21 

Note

In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have 33 friends.

In the second test case, there are three possible solutions (apart from symmetries):

  • aa is friend of bb, cc is friend of dd, and Bob has no friends, or
  • aa is a friend of bb and both cc and dd are friends with Bob, or
  • Bob is friends of everyone.

The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.

题意:n+1个节点构成图,现在给你n个节点的度,问你第n+1个节点的度可能是多少。如果不可能构成图输出-1,否则从小到大输出可能的度数。

思路:

显然是二分或者线段树类的东西,但是二分中的条件我们并不知道。

仔细观察后发现题目中有一个链接,然后通过链接可以得到一个解决本题的关键公式:

å¨è¿éæå¥å¾çæè¿°

然后就可以利用这个二分了。

然而答案是一段合法区间,不能直接二分。所以我们先找一个合法解,对合法解的左、右部分二分即可求出答案的L~R的范围。

找合法解的时候要利用上述公式,当第k个点不满足上述公式时,我们看二分的x是否大于du[k](先把输入的度从大到小排列)。如果是,则说明x偏大。推荐一篇不错的题解:传送门

代码调试过程中错误的部分已经标出。部分细节还需要自己体会。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
ll du[maxn],pre[maxn];
ll n,m,k,a[maxn],c[maxn];
ll sum,ans,cnt,tmp;
bool cmp(ll x,ll y){return x>y;}
int jud(ll x)
{
    ll ct=0,fg=1;
    for(ll i=1;i<=n;i++)
    {
        if(fg&&x>=a[i]) {du[++ct]=x;fg=0;}
        du[++ct]=a[i];
    }
    if(fg) du[++ct]=x;
    pre[ct+1]=0;
    for(ll i=ct;i>=1;i--) pre[i]=pre[i+1]+du[i];
    ll i=ct,tmp=0;//???
    for(ll u=1;u<=ct;u++)//???
    {
        i=max(i,u+1);
        while((du[i-1]<=u)&&(i>u+1)) i--;
        tmp+=du[u];//?
        if(tmp>u*(u-1)+(i-(u+1))*u+pre[i])
        {
            if(x>=du[u]) return 1;
            return 2;
        }
    }
    return 0;
}
int main()
{
    scanf("%lld",&n);
    ans=0;
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        ans+=a[i];
    }
    sort(a+1,a+1+n,cmp);
    cnt=0;
    if(ans&1) k=1;
    else k=0;
    for(ll i=k;i<=n;i+=2)
    c[++cnt]=i;
    ll l=1,r=cnt,md=-1;
    while(l<=r)
    {
        ll mid=(l+r)>>1;
        //cout<<jud(mid)<<endl;
        if(jud(c[mid])==1) r=mid-1;
        else if(jud(c[mid])==2) l=mid+1;
        else {md=mid;break;}
    }
    if(md==-1) puts("-1");
    else {
    ll L=md,R=md;
    l=1;r=md;
    while(l<=r)
    {
        ll mid=(l+r)>>1;
        if(jud(c[mid])) l=mid+1;
        else {L=mid;r=mid-1;}
    }
    l=md;r=cnt;
    while(l<=r)
    {
        ll mid=(l+r)>>1;
        if(jud(c[mid])) r=mid-1;
        else {R=mid;l=mid+1;}
    }
    //cout<<L<<R<<endl;
    for(ll i=L;i<=R;i++)
    printf("%lld%c",c[i],i==R?'\n':' ');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/86101404