CF1091E New Year and the Acquaintance Estimation
题目地址: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的更多相关文章
- 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 ...
- Codeforces 1091E New Year and the Acquaintance Estimation [图论]
洛谷 Codeforces 思路 有一个定理:Erdős–Gallai定理. 然后观察样例,可以猜到答案必定是奇偶性相同的一段区间,那么二分左右端点即可. 定理和这个猜测暂时都懒得学/证,留坑. #i ...
- Good Bye 2018 (A~F, H)
目录 Codeforces 1091 A.New Year and the Christmas Ornament B.New Year and the Treasure Geolocation C.N ...
- Codeforces 1091 Good Bye 2018
占个坑先,希望不要掉的太惨了吧,不要掉到上一次之前的rating upt:flag竟然没到,开心. A - New Year and the Christmas Ornament 好像没什么可说的. ...
- Codeforces Good Bye 2018
咕bye 2018,因为我这场又咕咕咕了 无谓地感慨一句:时间过得真快啊(有毒 A.New Year and the Christmas Ornament 分类讨论后等差数列求和 又在凑字数了 #in ...
- Good Bye 2018 Solution
A. New Year and the Christmas Ornament 签到. #include <bits/stdc++.h> using namespace std; int a ...
- 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...
- Noise Contrastive Estimation
Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...
- 手势估计- Hand Pose Estimation
http://blog.csdn.net/myarrow/article/details/51933651 1. 目前进展 1.1 相关资料 1)HANDS CVPR 2016 2 ...
随机推荐
- 数据库MySQL
--IN 关键字 在.....里 SELECT * FROM zhangwu WHERE money IN (66,666,700); 1.主键约束 特点非空 只用于表示當前的记录 primary k ...
- python异步编程之asyncio(百万并发)
前言:python由于GIL(全局锁)的存在,不能发挥多核的优势,其性能一直饱受诟病.然而在IO密集型的网络编程里,异步处理比同步处理能提升成百上千倍的效率,弥补了python性能方面的短板,如最 ...
- python: with的使用;
with适用于对资源进行访问的场合,不论使用过程中是否发生异常都执行必要的“清理”操作,释放资源,比如文件资源的关闭,线程锁的获取和释放等: with与上下文管理器相关: 上下文管理协议: 包含__ ...
- -bash: /tyrone/jdk/jdk1.8.0_91/bin/java: cannot execute binary file
问题描述:今天在linux环境下安装了一下JDK,安装成功后,打算输入java -version去测试一下,结果却出错了. 错误信息:-bash: /tyrone/jdk/jdk1.8.0_91/bi ...
- maven坑-Failure to transfer org.apache.maven:maven
参考网址:http://www.mkyong.com/maven/how-to-convert-maven-java-project-to-support-eclipse-ide/ https://b ...
- ASP.NET MVC 4 (十) 模型验证
模型验证是在模型绑定时检查从HTTP请求接收的数据是否合规以保证数据的有效性,在收到无效数据时给出提示帮助用户纠正错误的数据. 显式模型验证 验证数据最直接的方式就是在action方法中对接收的数据验 ...
- Kudu系列-基础
Apache Kudu 支持Insert/Update/Delete 等写操作(Kudu 随机写效率也很高, 实测对一个窄表做全字段update, 其速度达到了Insert速度的88%, 而verti ...
- C++解决case中不能定义局部变量问题
case Operation::DeviceAuthen: { std::string token = root["body"]["token"].asStri ...
- C# 异步委托(AP、APM)
Ø 前言 C# 异步委托也是属于异步编程中的一种,可以称为 Asynchronous Programming(异步编程)或者 Asynchronous Programming Model(异步编程模 ...
- 关于公众平台接口不再支持HTTP方式调用的公告
为保证数据传输安全,提高业务安全性,公众平台将不再支持HTTP方式调用.避免影响正常使用中含有HTTP方式调用的服务,请开发者尽快调整,将现有通过HTTP方式调用的切换成HTTPS调用,平台将于201 ...