Codeforces 1091 (Good Bye 2018)

1091 E

题意

一张无向图,给出 \(n-1\) 个顶点的度数,求第 \(n\) 个顶点所有可能的度数。不存在输出 \(-1\)
(graph realization problem)[https://en.wikipedia.org/wiki/Graph_realization_problem] → (Erdős–Gallai theorem)[https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93Gallai_theorem]
已知:当且仅当序列 \(d\) 满足 \(\sum_{i}d_i\) 为偶数,且\[\sum_{i=1}^{k}d_i\le k(k-1)+\sum_{i=k+1}^{n}\min (d_i,k)\]
对于所有 \(k\in [1,n]\) 恒成立时, \(d\) 为一张无向图的度数序列。\((1≤n≤5⋅10^5)\)

Examples

Input
3
3 3 3
Output
3
Input
4
1 1 1 1
Output
0 2 4
Input
2
0 2
Output
-1
Input
35
21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22
Output
13 15 17 19 21

显然,第 \(n\) 个顶点所有可能的度数为一段连续的奇数或偶数区间。所以我们二分这个区间的左、右端点。
对于一个二分出的值 \(x\) ,枚举所有 \(k\) ,代入上式检验是否恒成立即可。需要预处理后缀和。

1091 F

题意

有三种地形,三种行动。能量初始值为\(0\),不能为负。
|地形名称|行动名称|速度|能量变化|
|:-:|:-:|:-:|:-:|
|\(G\)|走、飞|\(5\text{s/m},1\text{s/m}\)|\(+1\)|
|\(W\)|游、飞|\(3\text{s/m},1\text{s/m}\)|\(+1\)|
|\(L\)|飞|\(1\text{s/m}\)|\(-1\)|
给出 \(n\) 段地形及每段地形长度 \(l_i\) ,求经过所有地形的最短时间。
注:可在某一地形上来回移动。\((1≤n≤10^5,1≤l_i≤10^{12})\)

Examples

Input
1
10
G
Output
30
Input
2
10 10
WL
Output
40
Input
2
1 2
WL
Output
8
Input
3
10 10 10
GLW
Output
80

贪心。
先从前往后模拟全部是飞行的情况,能量不够时,再考虑用游泳或走路代替飞行。优先考虑游泳。如果还不够,则在某个可以游泳或走路的地形上来回折返,游泳优先。

猜你喜欢

转载自www.cnblogs.com/BlogOfchc1234567890/p/10322964.html