题目地址:CF1091E New Year and the Acquaintance Estimation

首先,易知 \(ans\) 的奇偶性与所有给出的数的和的奇偶性相同

其次,易证 \(ans\) 的取值为一段连续的奇偶性相同的数,因此只需要到一个上界和下界(或者判断出无解输出 \(-1\) )

接下来的问题就是怎么判断一个状态合不合法以及如果不合法需要加还是减

题目中给出了一个Wiki的链接:Graph realization problem

链接中给出了问题的解法:Erdős–Gallai theorem

Erdős–Gallai定理的基本内容为:

一个非负整数序列 \(d_1≥d_2≥\cdots≥d_n\) 可以表示为 \(n\) 个顶点的简单图度序列,当且仅当 \(\sum_{i=1}^{n}\ d_i\) 为偶数且对每个 \(k\in [1,n]\) 满足

\[\sum_{i=1}^{k}\ d_i ≤ k(k-1)+\sum_{i=k+1}^{n}\ min(d_i,k)\]

对这个公式的理解 伪证 : \(1\) ~ \(k\) 这 \(k\) 个点的度数和最大为——所有点之间两两连线产生的度数和,加上剩下每个点可连线数量的最大值,这保证了公式的必要性;而非严格单调递减则保证了公式的充分性

利用公式朴素判断一次需要 \(O(n^2)\) ,但是可以优化到 \(O(n)\) ,外层嵌套两个二分,这样可以在 \(O(n\ log\ n)\) 的时间复杂度内解决问题

上代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 500006;
int n, a[N], c[N], cc[N], ansl = -1, ansr = -1;

int pd(int x) {
    memcpy(cc, c, sizeof(cc));
    ++cc[x];
    int w = 0, t = 0;
    ll s = 0, k = 0;
    for (int i = 0; i <= n; i++) {
        int num = (i == t && (t == n || a[t] < x)) ? x : a[t++];
        s += num;
        --cc[num];
        k += n - i - (w += cc[i]) - min(num, i);
        if (s > k + (ll)i * (i + 1)) return (i == t) ? 1 : -1;
    }
    return 0;
}

bool cmp(int x, int y) {
    return x > y;
}

int main() {
    cin >> n;
    ll s = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        s += a[i];
        ++c[a[i]];
    }
    s &= 1;
    sort(a, a + n, cmp);
    int l = 0, r = (n - s) >> 1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (pd((mid << 1) + s) == -1) l = mid + 1;
        else {
            ansl = mid;
            r = mid - 1;
        }
    }
    l = ansl;
    r = (n - s) >> 1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (pd((mid << 1) + s) == 1) r = mid - 1;
        else {
            ansr = mid;
            l = mid + 1;
        }
    }
    if (ansl == -1 || ansr == -1) puts("-1");
    else for (int i = ansl; i <= ansr; i++)
            printf("%lld ", (i << 1) + s);
    return 0;
}

另外线段树也可以优化,这样时间复杂度会多一个 \(log\) ,对 \(500000\) 的数据来说有些吃力,就不码了

CF1091E New Year and the Acquaintance Estimation的更多相关文章

  1. Codeforces 1091E New Year and the Acquaintance Estimation Erdős–Gallai定理

    题目链接:E - New Year and the Acquaintance Estimation 题解参考: Havel–Hakimi algorithm 和 Erdős–Gallai theore ...

  2. Codeforces 1091E New Year and the Acquaintance Estimation [图论]

    洛谷 Codeforces 思路 有一个定理:Erdős–Gallai定理. 然后观察样例,可以猜到答案必定是奇偶性相同的一段区间,那么二分左右端点即可. 定理和这个猜测暂时都懒得学/证,留坑. #i ...

  3. Good Bye 2018 (A~F, H)

    目录 Codeforces 1091 A.New Year and the Christmas Ornament B.New Year and the Treasure Geolocation C.N ...

  4. Codeforces 1091 Good Bye 2018

    占个坑先,希望不要掉的太惨了吧,不要掉到上一次之前的rating upt:flag竟然没到,开心. A - New Year and the Christmas Ornament 好像没什么可说的. ...

  5. Codeforces Good Bye 2018

    咕bye 2018,因为我这场又咕咕咕了 无谓地感慨一句:时间过得真快啊(有毒 A.New Year and the Christmas Ornament 分类讨论后等差数列求和 又在凑字数了 #in ...

  6. Good Bye 2018 Solution

    A. New Year and the Christmas Ornament 签到. #include <bits/stdc++.h> using namespace std; int a ...

  7. 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)

    最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...

  8. Noise Contrastive Estimation

    Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...

  9. 手势估计- Hand Pose Estimation

    http://blog.csdn.net/myarrow/article/details/51933651 1. 目前进展 1.1 相关资料      1)HANDS CVPR 2016      2 ...

随机推荐

  1. yum工具的使用

    yum工具的使用 ---------- yum list|head -n 10会有一个报错:由于管道被破坏而退出-----------搜索名字为vim的rpm包yum search vim使用grep ...

  2. NLTK的安装

    一.NLTK:Natural Language Toolkit(自然语言工具包) 下载:http://www.nltk.org pip install nltk 二.使用 import nltk nl ...

  3. 设计模式---接口隔离模式之门面模式(Façade)

    前提:接口隔离模式 在组建构建过程中,某些接口之间直接的依赖常常会带来很多问题.甚至根本无法实现.采用添加一层间接接口(稳定的),来隔离本来相互紧密关联的接口是一种常见的解决方案. 典型模式: 门面模 ...

  4. 1.Eureka

    分布式系统中,当B的数量越来越多的时候,A只需要从注册中心获取B注册的服务,而不需要直接从B中获取服务,答案显而易见. application.yml: eureka: client: service ...

  5. MyEclipse项目中,让修改后的Servlet文件立即运行生效方法

    运行的时候用Debug模式发布如图 java 自动生成get set方法的快捷键是什么? Re:在myeclipse中按住shift+alt+s选择 generate getters and sett ...

  6. VS Code +node npm 调试 js

    打开vsCode的调试控制台里面的终端 然后输入下面代码 npm install express-generator -g 1 创建一个命名为 myapp 的应用. express myapp 你就可 ...

  7. SQL结构化查询语句

    SQL结构化查询语句 SQL定义了查询所有关系型数据库的规则. 1.通用语法 SQL语句可以单行或者多行书写,以分号结尾 可以使用空格和缩进增强可读性 不区分大小写,但是关键字建议大写 3种注释 注释 ...

  8. mysql驱动参数变化

    在java平台使用的mysql jdbc驱动为:mysql-connector-java. 在项目中添加如下依赖: <dependency> <groupId>mysql< ...

  9. Python数据挖掘和机器学习

    -----------------------------2017.8.9--------------------------------- 先占个坑 在接下来的一个半月里(即从现在到十一) 我将结合 ...

  10. ueditor 百度编辑器 粘贴的table表格样式边线

    粘贴html的table表格会有间隔大,黑色边线,可以在: ueditor.all.js 里 找到以下处,修改里面的样式即可 me.ready(function () { utils.cssRule( ...