Codeforces 1091E New Year and the Acquaintance Estimation Erdős–Gallai定理
题目链接:E - New Year and the Acquaintance Estimation
题解参考:
Havel–Hakimi algorithm 和 Erdős–Gallai theorem
按照后面那个定理说的,枚举$k∈[1,n]$,对于每一个$k$,计算出向等式两边加入$a_{n+1}$的合法范围,最后所有范围求交即可
最后按照前面那个定理说的,枚举最终区间的时候,对于合法真正的$a_{n+1}$进行输出即可
比赛的时候没看见后面那个定理,推了半天
-----------------------
正常人写法:
#include <bits/stdc++.h>
#define ll long long
#define rep(ii,a,b) for(ll ii=a;ii<=b;++ii)
using namespace std;
int n;
int main() {ios::sync_with_stdio(false);cin.tie(0);cin>>n;vector<ll>deg(n),sum(n+1);for(auto&i:deg) cin>>i;sort(deg.begin(),deg.end());rep(i,0,n-1) sum[i+1]=sum[i]+deg[i];int pos=0;ll lower=-1,upper=n+2;rep(k,1,n){while(pos<n&°[pos]<k) pos++;ll uper=min(n-k,(ll)pos);ll eql=sum[n]-sum[n-k];ll eqr=k*(k-1)+sum[uper]+k*(n-k-uper);ll amin=eql-eqr;ll amax=eqr-eql+deg[n-k]+min(deg[n-k],(ll)k);lower=max(amin,lower);upper=min(amax,upper);}if(upper-lower<0) cout<<-1;else rep(i,lower,upper) if((sum[n]+i)%2==0) cout<<i<<' ';
}
弱智写法:
#include <bits/stdc++.h>
#define ll long long
#define rep(ii,a,b) for(ll ii=a;ii<=b;++ii)
using namespace std;
int n;
class segtree{
#define nd node[now]
#define ndl node[now<<1]
#define ndr node[now<<1|1]public:struct segnode {int l,r,mx,tag;int mid(){return (r+l)>>1;}int len(){return r-l+1;}void update(int x){mx+=x,tag+=x;}};vector<segnode> node;segtree(int n) {node.resize(n<<2|3);maketree(1,n);}void pushup(int now){nd.mx=max(ndl.mx,ndr.mx);}void pushdown(int now){if(nd.tag){ndl.update(nd.tag);ndr.update(nd.tag);nd.tag=0;}}void maketree(int s,int t,int now=1){nd={s,t,0,0};if(s==t)return ;maketree(s,(s+t)>>1,now<<1); maketree(((s+t)>>1)+1,t,now<<1|1);}void update(int s,int t,int x,int now=1){if(s>nd.r||t<nd.l) return ;if(s<=nd.l&&t>=nd.r){nd.update(x);return ;}pushdown(now);update(s,t,x,now<<1); update(s,t,x,now<<1|1);pushup(now);}int query_lowerbound(int now=1){if(nd.mx<n) return -1;if(nd.len()==1) return nd.l;pushdown(now);if(ndl.mx>=n) return query_lowerbound(now<<1);else return query_lowerbound(now<<1|1);}int query_upperbound(int now=1){if(nd.mx<n) return -1;if(nd.len()==1) return nd.l;pushdown(now);if(ndr.mx>=n)return query_upperbound(now<<1|1);else return query_upperbound(now<<1);}
#undef nd
#undef ndl
#undef ndr
};
int main() {ios::sync_with_stdio(false);cin.tie(0);cin>>n;segtree tree(n);vector<ll>deg(n),sum(n+1);for(auto&i:deg) cin>>i;sort(deg.begin(),deg.end());rep(i,0,n-1) sum[i+1]=sum[i]+deg[i];int pos=0;rep(k,1,n){while(pos<n&°[pos]<k) pos++;ll uper=min(n-k,(ll)pos);ll eql=sum[n]-sum[n-k];ll eqr=k*(k-1)+sum[uper]+k*(n-k-uper);ll amin=eql-eqr;ll amax=eqr-eql+deg[n-k]+min(deg[n-k],(ll)k);if(amin<=min(k,deg[n-k])) tree.update(max(amin,0ll),deg[n-k],1);if(amax>deg[n-k]) tree.update(deg[n-k]+1,min(amax,(ll)n),1);}int lower=tree.query_lowerbound(),upper=tree.query_upperbound();if(lower==-1) cout<<-1;else rep(i,lower,upper)if((sum[n]+i)%2==0) cout<<i<<' ';
}
转载于:https://www.cnblogs.com/nervendnig/p/10222501.html
Codeforces 1091E New Year and the Acquaintance Estimation Erdős–Gallai定理相关推荐
- Codeforces 1091E
Codeforces 1091E 题意:给定n个点的度数,请你添加第n+1个点,输出所有可能的第n+1个点的度数 做法:1. 查看链接知道了下面这个定理 A sequence of non-negat ...
- Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理
B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...
- 阿里云nginx服务器多站点的配置
Fighting! 欲戴皇冠,必承其重! 目录视图 摘要视图 订阅 [活动]Python创意编程活动开始啦!!! CSDN日报20170426 --<四无年轻人如何逆袭> [C ...
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)
题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...
- 【codeforces 812C】Sagheer and Nubian Market
[题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...
- CodeForces 获得数据
针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...
- codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...
题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...
- CodeForces 595A
题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...
最新文章
- ewebeditor 5.2 列目录漏洞
- vue-router学习地址
- C++之编码问题(Unicode,ASCII,本地默认)
- 最近QQ联系我解决问题的郁闷
- 实验2 递归和分治法(二分查找)
- [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第34篇]描述攻击离散对数问题的baby-step/Giant-step方法
- js中比较时间字串大小
- 类图的使用 c# 1613918428
- Mac下命令行发邮件【搭配php(shell_exec...)等脚本语言,轻松发邮件,告别各种依赖库】...
- 数据库锁机制和CAS概念
- 基于Python的指数基金量化投资 - 指数的净资产收益率计算
- C# UDP Socket ReceiveFrom 远程主机强迫关闭了一个现有的连接。
- WPF之NPOIE导出xcel
- 算法笔记 A+B 输入输出练习VIII
- vc调用浏览器打开指定网址页面
- linux运维工程师前景怎样 就业薪资待遇高吗
- 反映个把月才退了个零头 ——消费者投诉旅游平台退款难
- 2022道路运输安全员考试题库多选预测分享(2)
- Codeforces Round #655 (Div. 2)
- 部分dcm文件使用jpeg2000压缩会出错