题目链接: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. STM32学习笔记:【004】USART串口通信

    版本:STM32F429 Hal库v1.10 串口通信能够实现两块电路之间不同的通信,在开发中作为打印调试也是一门利器(printf重定向). 补充一点小知识: 1. weak修饰符修饰的函数,说明这 ...

  2. 2018牛客网暑期ACM多校训练营(第二场)G Transform(二分)

    题意 在一个数轴上有n个集装箱,第 i 个集装箱的位置为x[i],且在集装箱内装有a[i]件货物,现在将这些集装箱内的货物进行移动(将一件货物从第 i 个集装箱移动到第 j 个集装箱的花费就为2*ab ...

  3. 2016vijos 1-2 股神小L(堆)

    维护前i天的最优解,那么在后面可能会对前面几天的买卖情况进行调整 如果前面买入,买入的这个在后面一定不会卖出 如果前面卖出,卖出的这个可能会在后面变成买入,因为买这个,卖后面的会获得更多的收益 用一个 ...

  4. bzoj千题计划308:bzoj4589: Hard Nim(倍增FWT+生成函数)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4589 n*m*m 做法 dp[i][j] 前i堆石子,异或和为j的方案数 第一重循环可以矩阵快速幂 ...

  5. ArcMap登录和ArcGIS online都是灰色,无法连接网络的解决方法

    此问题可能由多种原因造成,表现为系统托盘中的 ArcGIS Online 地球图标断开连接(显示红色×). 解决方法: 方法一: 在系统托盘中的 ArcGIS Online 地球图标右键单击此图标 & ...

  6. Python实现代理模式

    from abc import ABCMeta, abstractmethod NOT_IMPLEMENTED = "You should implement this." cla ...

  7. 利用C#访问注册表获取软件的安装路径

    文章地址:https://blog.csdn.net/yl2isoft/article/details/17332139

  8. GCC编译器原理(二)------编译原理一:ELF文件(1)

    二.ELF 文件介绍 2.1 可执行文件格式综述 相对于其它文件类型,可执行文件可能是一个操作系统中最重要的文件类型,因为它们是完成操作的真正执行者.可执行文件的大小.运行速度.资源占用情况以及可扩展 ...

  9. phpcms中set_config和get_sysinfo函数

    /** * 设置config文件 * @param $config 配属信息 * @param $filename 要配置的文件名称 */ function set_config($config, $ ...

  10. 内容分享-迅为IMX6开发板编译问题及解决方法

    [经验分享]IMX6开发板编译问题及解决方法本文转自迅为IMX6开发板售后讨论群,分享给大家~ 物理主机 win10 64 位专业版.虚拟机 VM12 Pro.开发环境采用迅为提供的开发环境: Ubu ...