题目链接: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. Kafka技术内幕 读书笔记之(四) 新消费者——心跳任务

    消费者拉取数据是在拉取器中完成的,发送心跳是在消费者的协调者上完成的,但并不是说拉取器和消费者的协调者就没有关联关系 . “消费者的协调者”的作用是确保客户端的消费者和服务端的协调者之间的正常通信,如 ...

  2. linux unknown host 问题【转】

    如果某台Linux(CentOS)服务器ping域名, 如下提示: # ping www.sina.comping: unknown host www.sina.com 确认网络没问题的情况下, 可以 ...

  3. 解析ArcGis的标注(二)——认识ArcGis标注标签

    该“标注”系列博文的标注引擎使用“标准标注引擎(standard label engine)”,这个概念如不知道,可不理会,ArcGis默认标注引擎就是它. 本篇的标注语句解析使用VBScript,注 ...

  4. linux_网易云音乐安装

    使用命令安装一些基本包$ sudo apt install devscripts equivs git

  5. 回顾一下C++ 编写DLL

    项目模版使用Win32工程创建的dll项目 一.原始代码 使用depends查看导出函数 二.不同编译方式区别 C方式编译(extern "C"): __stdcall调用约定:输 ...

  6. tcp_listen函数

    #include <netdb.h> #include <unistd.h> #include <stddef.h> #include <strings.h& ...

  7. AD软件使用心得

    1.在更新原理图之前一定要标记所有器件,否则无法生成PCB器件. 2.学会用sch list网表来批量修改器件名称 3.布线的面

  8. 利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据

    登录界面 <body><form action="login1.php" method="post"><div>用户名:&l ...

  9. [C++]PAT乙级1002.写出这个数(20/20)

    /* 1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100. ...

  10. form表单post提交的数据格式

    1.浏览器行为:Form表单提交 action:url 地址,服务器接收表单数据的地址 method:提交服务器的http方法,一般为post和get name:最好好吃name属性的唯一性 enct ...