题目链接: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. 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(二)

    1.Linux“线程” 进程与线程之间是有区别的,不过linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身只有进程的概念,而其所谓的“线程” ...

  2. python (大文件下载及进度条展示) 验证客户端链接的合法性,socketserver

    ##########总结########### 文件校验加进度条显示 ####server import os import json import socket import struct impo ...

  3. eclipse将javaSE项目导出成可执行jar包

    将第三方包和项目打包到一块 step1:选中要导出的项目,右键选择Export step2:选择java/Runable JAR file step3:选择main主程序,选择第三方包打包的形式,推荐 ...

  4. Elasticsearch集群节点配置详解

    注意:如果是在局域网中运行elasticsearch集群也是很简单的,只要cluster.name设置一致,并且机器在同一网段下,启动的es会自动发现对方,组成集群. 2.elasticsearch- ...

  5. react使用proxy代理配置

    proxy,默认为NULL,类型为URL,一个为了发送http请求的代理 在package.json文件中使用proxy配置可以解决跨域问题 使用注意事项: create-react-app脚手架低于 ...

  6. [Android] Android最简单ScrollView和ListView滚动冲突解决方案

    [Question]问题描述: 单独的ListView列表能自动垂直滚动,但当将ListView嵌套在ScrollView后,会和ScrollView的滚动滑块冲突,造成ListView滑块显示不完整 ...

  7. 错误记录:vue跟vue编译器版本不一致

    错误如下: error in ./src/Utils.vue Module build failed: Error: Vue packages version mismatch: - vue@ - v ...

  8. jquery判断对象是否存在

    if($("#abc").length >0) { ... } if($("#abc").html() != "") { ... }

  9. 关于nexus的学习

    1.首先下载 解压版: 2.将nexus的bin目录路径  写入  环境变量path中: 3.修改 nexus配置文件wrapper.conf  15行  ,指定java.exe文件位置(例如:wra ...

  10. PHP面向对象的三大特征操作——封装、继承、多态(下)

    <?php 继承(单继承)特点:一个子类只有一个父类,一个父类可以有多个子类.//父类(基类)class Ren{    public $name;    public function say ...