Codeforces1091E

coding

Codeforces 1091E


题意:给定n个点的度数,请你添加第n+1个点,输出所有可能的第n+1个点的度数

做法:1. 查看链接知道了下面这个定理

A sequence of non-negative integers $ d_1\geq\cdots\geq d_n$ can be represented as the degree sequence of a finite simple graph on $n$ vertices if and only if $ d_1+\cdots+d_n$ is even and$\sum {i=1}^{k}d{i}\leq k(k-1)+\sum {i=k+1}^{n}\min(d{i},k)$ holds for every k in ${\displaystyle 1\leq k\leq n}$.

2.排序之后,$[low(p), up(p)]$ 表示在$[p, p+1]$ 之间插入新的点度数$d_t$的取值范围。

(1) $d_{p+1} \leq d_t \leq d_p$

(2) 当 $d_t$ 在不等式左侧时 $$ d_t \leq k(k+1)+\sum {i=k+1}^{n}\min(d{i},k+1) - \sum {i=1}^{k}d{i}, p \leq k \leq n \ \Downarrow \ up(p) = min(up(p + 1) , p(p+1) + \sum_{i=p+1}^n min(d_i,p+1) - \sum_{i=1}^p d_i) $$ (3) 当 $d_t$ 在不等式右侧时 $$ \sum_{i=1}^k d_i - k(k-1) - \sum_{i=k+1}^n min(d_i,k) <= d_t , 1 \leq k \leq p \ \Downarrow\ low(p) = max(\sum_{i=1}^p d_i - p(p-1) - \sum_{i=p+1}^n min(d_i,p), low(p-1)) \ $$

3.猜测答案为连续的一段值,将上一步求出的区间并起来

4.又因为无向简单图,每条边对度数贡献为2,由此可以确定答案的奇偶性

5.确定上下界时,需要维护$\sum_{i = k+1}^{n} min(d_i,k)$ 和$\sum_{i = k}^{n} min(d_i,k)$ 我们倒着枚举$k$,用优先队列维护小于等于当前$k$的集合,以及这个集合内所有元素的和,就可以预处理出这个式子,因为k是递减的因此保证了正确性

6.一定要分析清楚再写。。。代码细节很多

#include <bits/stdc++.h>

typedef long long ll;

constexpr int N = 600005;

constexpr ll inf = 1e18;

using namespace std;

int n;

priority_queue<ll> q;

ll f1[N], f2[N], sum[N], low[N], up[N], d[N];

int main() {

scanf("%d",&n);

for(int i = 1; i <= n; ++i) scanf("%lld",&d[i]);

sort(d+1,d+1+n); reverse(d+1,d+1+n);

for(int i = 1; i <= n; ++i) sum[i] = sum[i-1] + d[i];

d[0] = n; d[n+1] = 0;

for(int i = 0; i <= n; ++i) up[i] = inf, low[i] = -inf;

ll tmp = 0;

for(int i = n; i >= 0; --i) {

while(!q.empty() && q.top() > i) tmp -= q.top(), q.pop();

f2[i] = tmp + 1ll*(n - i - q.size())*i;

if(d[i] <= i) q.push(d[i]), tmp += d[i];

f1[i] = tmp + 1ll*(n - i + 1 - q.size())*i;

}

for(int i = n; i >= 0; --i) {

if(i!=n) up[i] = min(up[i+1], 1ll*i*(i+1) + f1[i+1] - sum[i]);

else up[i] = 1ll*i*(i+1) - sum[i];

}

for(int i = 1; i <= n; ++i) {

low[i] = max(low[i-1], sum[i] - 1ll*i*(i-1) - f2[i]);

}

for(int i = 0; i <= n; ++i) low[i] = max(low[i], d[i+1]), up[i] = min(up[i], d[i]);

ll L = inf, R = -inf;

for(ll i = 0; i <= n; ++i) if(low[i] <= up[i]) L = min(L, low[i]), R = max(R, up[i]);

if(L > R) printf("-1");

for(ll i = L; i <= R; ++i) if(!((sum[n]-i)&1)) printf("%lld ",i);puts("");

return 0;

}

以上是 Codeforces1091E 的全部内容, 来源链接: utcz.com/z/508849.html

回到顶部