题目链接: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. ESB简介及选型(转) http://www.cnblogs.com/skyme/archive/2012/08/06/2623414.html

    什么是ESB 企业服务总线(Enterprise Service Bus,ESB)的概念是从面向服务体系架构(Service Oriented Architecture, SOA)发展而来的.SOA描 ...

  2. 跳到IE9的一个坑里了(枚举 JavaScript 属性)

    有一段Json如下 [{"SaleName_ID":"0","SaleName":"","28":& ...

  3. idea使用maven打包项目

    第一步:打开maven Projects 第二步.找到package 第三步,运行.到路径下面去找打包的文件吧. 第二种方法: 使用命令 cmd进入项目目录,例如项目在D盘项目名poject 输入: ...

  4. .Net版微信支付

    一. 案例介绍 这里模拟一个实际业务场景,进行介绍微信支付,业务功能包括:登录.注册.充值.查看充值记录. 页面图:       二. 概要设计 1.数据库设计 这里数据库包括两张表:用户表和订单表. ...

  5. 068、Calico的网络结构是什么?(2019-04-11 周四)

    参考https://www.cnblogs.com/CloudMan6/p/7520164.html     root@host1:~# docker run -itd --name bbox1 -- ...

  6. 055、创建macvlan网络 (2019-03-22 周五)

    参考https://www.cnblogs.com/CloudMan6/p/7364332.html     创建macvlan网络,需要指定使用哪块物理网卡进行通信   -o parent=ens1 ...

  7. Android资源重命名

    Android资源比如图片重命名后,Resource里看到的还是之前的名字,可以关掉AndroidStudio,从新打开就正常了

  8. java.lang.ClassCastException: com.sun.proxy.$Proxy27 cannot be cast to com.bbk.n002.service.QuestionService

    1 严重: Servlet /N002-1.0 threw load() exception 2 java.lang.ClassCastException: com.sun.proxy.$Proxy2 ...

  9. Java SE之网络编程:知识框架

  10. 怎样远程访问 MySQL

    比如我在PC上安装有 phpmyadmin, 图形界面很友好,我的MySQL 在一台Centos 7.5服务器上,很自然的想到用phpmyadmin 去登录linux上的MySQL. 但是折腾了很久也 ...