[CF1091]E. New Year and the Acquaintance Estimation——Erdős–Gallai theorem+线段树 dalaos' blogs Some Links

版权声明:欢迎大家转载,转载请标明出处。 https://blog.csdn.net/ylsoi/article/details/85562777

题目大意:

给定n个点的度数序列,再添加一个点,求添加的这个点的度数为多少时,满足这n+1个点可以构成简单无向图。

思路:

首先你得要打开CF比赛里的那个wiki链接,然后有了Erdős–Gallai 定理。
k [ 1 , n ]     i = 1 k d i k ( k 1 ) + i = k + 1 n min ( d i , k ) \forall k\in [1,n]\ \ \ \sum_{i=1}^{k}d_i\leq k(k-1)+\sum_{i=k+1}^{n}\min(d_i,k)
考虑枚举答案然后将答案插入序列相应的位置,然后我们发现,为了满足条件,新的数左边的位置和右边的位置需要满足的条件是不一样的,但是都和新的数有关,我们只需要用线段树来维护一些东西就好了。

#include<bits/stdc++.h>

#define REP(i,a,b) for(int i=a,i##_end_=b;i<=i##_end_;++i)
#define DREP(i,a,b) for(int i=a,i##_end_=b;i>=i##_end_;--i)
#define debug(x) cout<<#x<<"="<<x<<" "
#define fi first
#define se second
#define mk make_pair
#define pb push_back
typedef long long ll;

using namespace std;

void File(){
	freopen("e.in","r",stdin);
	freopen("e.out","w",stdout);
}

template<typename T>void read(T &_){
	_=0; T f=1; char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())_=(_<<1)+(_<<3)+(c^'0');
	_*=f;
}

const int maxn=5e5+10;
const ll inf=1e18;
ll n,d[maxn],s[maxn],ans[maxn],cnt,sum,val[maxn];

struct Segment_Tree{
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (o<<1|1)
#define lson lc,l,mid
#define rson rc,mid+1,r
	ll Min[maxn<<2];
	void update(int o,int l,int r,int p,ll x){
		if(l==r)Min[o]=x;
		else{
			if(p<=mid)update(lson,p,x);
			else update(rson,p,x);
			Min[o]=min(Min[lc],Min[rc]);
		}
	}
	ll query(int o,int l,int r,int L,int R){
		if(L>R)return inf;
		if(L<=l && r<=R)return Min[o];
		else{
			ll ret=inf;
			if(L<=mid)ret=min(ret,query(lson,L,R));
			if(R>=mid+1)ret=min(ret,query(rson,L,R));
			return ret;
		}
	}
}T1,T2,T3;

bool cmp(const ll & x,const ll & y){
	return x>y;
}

int main(){
	File();
	read(n);
	REP(i,1,n)read(d[i]),sum+=d[i];
	sort(d+1,d+n+1,cmp);
	REP(i,1,n)s[i]=s[i-1]+d[i];

	ll p=n+1,pos,p1=n+1;
	for(ll k=1;k<=n;++k){
		while(p>1 && d[p-1]<k)--p;
		while(p1>1 && d[p1-1]<k+1)--p1;
		pos=max(k+1,p);
		ll w=k*(k-1)+(pos-k-1)*k+(s[n]-s[pos-1])-s[k];
		T1.update(1,1,n,k,w);
		T2.update(1,1,n,k,w+k);
		pos=max(k+1,p1);
		w=k*(k+1)+(pos-k-1)*(k+1)+(s[n]-s[pos-1])-s[k];
		T3.update(1,1,n,k,w);
	}

	ll k=n+1;
	p=0;
	for(ll i=0;i<=n;++i){
		while(k>1 && d[k-1]<i)--k;
		while(p<n && d[p+1]>k)++p;
		pos=max(k,p+1);
		val[i]=k*(k-1)+(pos-k)*k+(s[n]-s[pos-1])-s[k-1]-i;
	}

	p=n+1;
	for(ll i=0;i<=n;++i)if((i+sum)%2==0){
		if(val[i]<0)continue;
		while(p>1 && d[p-1]<i)--p;
		//k <= p-1
		pos=min(i,p-1);
		if(T2.query(1,1,n,1,pos)<0)continue;
		if(T1.query(1,1,n,pos+1,p-1)+i<0)continue;
		//k >= p
		if(T3.query(1,1,n,p,n)-i<0)continue;
		ans[++cnt]=i;
	}

	if(!cnt)cout<<-1<<endl;
	else REP(i,1,cnt)cout<<ans[i]<<" ";

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ylsoi/article/details/85562777
今日推荐