1. 程式人生 > >Good Bye 2018 E. New Year and the Acquaintance Estimation

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;
}