题目链接: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&&deg[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&&deg[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<<' ';
}

  

Codeforces 1091E New Year and the Acquaintance Estimation Erdős–Gallai定理的更多相关文章

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

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

  2. CF1091E New Year and the Acquaintance Estimation

    题目地址:CF1091E New Year and the Acquaintance Estimation 首先,易知 \(ans\) 的奇偶性与所有给出的数的和的奇偶性相同 其次,易证 \(ans\ ...

  3. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...

  4. 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 ...

  5. codeforces 696C PLEASE 概率dp+公式递推+费马小定理

    题意:有3个杯子,排放一行,刚开始钥匙在中间的杯子,每次操作,将左右两边任意一个杯子进行交换,问n次操作后钥匙在中间杯子的概率 分析:考虑动态规划做法,dp[i]代表i次操作后的,钥匙在中间的概率,由 ...

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

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

  7. Codeforces 1091 Good Bye 2018

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

  8. Codeforces Good Bye 2018

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

  9. Good Bye 2018 Solution

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

随机推荐

  1. jQuery使用(二):DOM样式操作和属性操作

    DOM取值与赋值 .html() .text() .size() 1.html()方法类似原生DOM的属性innerHTML,不传入参数的时候默认为取指定元素内的HTML内容,包含前后空白文本结构,以 ...

  2. python--numpy、pandas

    numpy 与 pandas 都是用来对数据进行处理的模块, 前者以array 为主体,后者以 DataFrame 为主体(让我想起了Spark的DataFrame 或RDD) 有说 pandas 是 ...

  3. Python操作MySQL-day11

    写在前面 上课第11天,打卡: 包子有肉,不在褶上: 前言 存储和数据库是整个网站架构的瓶颈 关系型数据库(MySQL | Oracle) 类似于excel表格 用结构化查询语言SQL进行存取的 非关 ...

  4. jqgrid again

    之前有提及过jqgrid这个很不错的jquery grid, 非常适合企业MIS系统使用. 本文以一个显示学生成绩结果的页面, 来说明它的一些用法, 手写代码, 运行可能会报错, 不过思路是经过验证的 ...

  5. 使用JAVA数组实现顺序栈

    1,首先总结一下线性表(分为顺序表和链接表,[即顺序存储结构和链式存储结构的区别])和栈(顺序栈和链接栈)还有队列(顺序队列和链接队列)的JAVA类库中的实现: java.util.ArrayList ...

  6. Mac 开发使用中的小技巧收集

    1. mac 下ssh连接到 linux 服务器管理,同putty,无需第三方 Mac 下打开终端,输入: ssh 登录用户名@ip地址 如: ssh root@142.138.1.89 如有询问是否 ...

  7. C++ vector和list的主要用法区别

    vector可以直接通过下标访问,list不可以 1.vector ; i < userTypes.size(); i++) { str = userTypes[i].typeName; } 2 ...

  8. 嫁给程序员的好处,你get到了吗?

    首先,我们要知道,什么是程序员?程序员是做什么的? "程序员(英文Programmer)是从事程序开发.维护的专业人员.一般将程序员分为程序设计人员和程序编码人员,但两者的界限并不非常清楚, ...

  9. Android手机特殊软件配置

    1. 安装360一键root http://root.360.cn/ 2.安装SSHdroid 用于启用手机的ssh 地址:https://www.apk20.com/apk/77332/v/7346 ...

  10. ubuntu没有/usr/include/sys目录

    实际上不是没有sys目录,只是系统给换路径了 32位系统:/usr/incude/i386-linux-gnu/sys 64位系统:/usr/include/x86_64-linux-gnu/sys/ ...