Solution -「LOCAL」Drainage System
\(\mathcal{Description}\)
合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个。每次可以挑 \(x\in[L,R]\) 个果子合并成一个,代价为所挑果子权值之和。求合并所有果子的最少代价。\(T\) 组数据。
\(T\le10\),\(n,a_i\le10^5\),\(2\le L\le R\le\sum_{i=1}^na_i\)。
\(\mathcal{Solution}\)
把合并考虑成一棵树,树叉在 \([L,R]\) 内,可以发现总代价为每个点的深度 \(\times\) 权值之和。以此为背景证明几个结论(当然很容易直接猜到最终结论)。
Lemma 1:最优解的非叶结点最少。否则,一定可以删除最深的非叶结点,并把它的儿子们丢给其它非叶结点,使“深度 \(\times\) 权值之和”变小。
Lemma 2:记 \(u\) 的深度为 \(d(u)\),当树满足 Lemma 1 时,若存在非叶结点 \(d(u)<d(v)\),则 \(u\) 满叉。很显然,只要 \(v\) 存在且 \(u\) 不满,就可以把 \(v\) 的儿子接到 \(u\) 下使答案更优。由此有推论:存在最优解,其合并数量构成的序列字典序最小(先最小(\(L\)),再最大 \(R\))。
考虑到实际问题,我们可以断言答案的合并数量为:\(\{L,L,\cdots,L,x,R,R,\cdots,R\}\) 且其中 \(R\) 尽可能多。
不过这个题你甚至不能带堆的 \(\log\) qwq!
由输入方式,显然一堆果子可以表示成 \((\text{权值},\text{个数})\) 二元组,初始时有 \(n\) 个二元组(有些个数是 \(0\),用于占位),它们已按权值升序排列了。考虑从队首取出若干个小果子合并成大果子,显然和出的大果子的权值是升序的,那么很多情况下大果子可以直接插在队尾。不过可能有大果子的权值属于 \([1,n]\) 的情况,我们直接把对应果子的个数 \(+1\) 即可。
复杂度 \(\mathcal O(Tn\log\sum_{i=1}^na_i)\)。(Tiw 说的 qwq。
\(\mathcal{Code}\)
#include <queue>
#include <cstdio>
#include <iostream>
typedef long long LL;
typedef std::pair<LL, LL> pll;
inline char fgc () {
static char buf[1 << 17], *p = buf, *q = buf;
return p == q && ( q = buf + fread ( p = buf, 1, 1 << 17, stdin ), p == q ) ? EOF : *p ++;
}
inline LL rint () {
LL x = 0; char s = fgc ();
for ( ; s < '0' || '9' < s; s = fgc () );
for ( ; '0' <= s && s <= '9'; s = fgc () ) x = x * 10 + ( s ^ '0' );
return x;
}
const int MAXN = 1e5;
int n, a[MAXN + 5];
LL L, R, S, ans;
std::queue<pll> plan;
struct GreadyQueue {
std::deque<pll> deq;
std::deque<pll>::iterator ptr;
GreadyQueue (): ptr ( deq.end () ) {}
inline void clear () { deq.clear (), ptr = deq.end (); }
inline pll& front () { return deq.front (); }
inline void popF () {
bool eff = ptr == deq.begin ();
deq.pop_front ();
if ( eff ) ptr = deq.begin ();
}
inline void add ( const pll x ) {
ans += x.first * x.second;
for ( ; ptr != deq.end () && ptr->first < x.first; ++ ptr );
if ( ptr == deq.end () ) deq.push_back ( x ), -- ( ptr = deq.end () );
else ptr->second += x.second;
}
} Q;
inline void clear () {
ans = S = 0, Q.clear ();
for ( ; !plan.empty (); plan.pop () );
}
inline bool calcPlan () {
LL tR = ( S - 1 ) / ( R - 1 ) + !!( ( S - 1 ) % ( R - 1 ) ), tL = 0;
if ( tR * ( L - 1 ) > S - 1 ) return false;
LL def = tR * ( R - 1 ) - ( S - 1 );
if ( L ^ R ) {
tR -= tL = def / ( R - L ), def -= tL * ( R - L );
if ( tL ) plan.push ( pll ( L, tL ) );
if ( def ) plan.push ( pll ( R - def, 1 ) ), -- tR;
}
if ( tR ) plan.push ( pll ( R, tR ) );
return true;
}
inline void followPlan () {
for ( ; !plan.empty (); plan.pop () ) {
pll stp ( plan.front () ), cur ( 0, 0 );
while ( stp.second ) {
if ( cur.second ) {
if ( cur.second + Q.front ().second >= stp.first ) {
cur.first += Q.front ().first * ( stp.first - cur.second );
Q.front ().second -= stp.first - cur.second;
Q.add ( pll ( cur.first, 1 ) ), cur = pll ( 0, 0 );
-- stp.second;
} else {
cur.first += Q.front ().second * Q.front ().first;
cur.second += Q.front ().second, Q.popF ();
continue;
}
}
LL cnt = std::min ( stp.second, Q.front ().second / stp.first );
if ( cnt ) Q.add ( pll ( Q.front ().first * stp.first, cnt ) );
stp.second -= cnt, Q.front ().second -= stp.first * cnt;
if ( stp.second ) {
cur = pll ( Q.front ().second * Q.front ().first, Q.front ().second );
Q.popF ();
}
}
}
}
int main () {
freopen ( "river.in", "r", stdin );
freopen ( "river.out", "w", stdout );
for ( int T = rint (); T --; ) {
clear ();
n = rint (), L = rint (), R = rint ();
for ( int i = 1; i <= n; ++ i ) {
Q.deq.push_back ( pll ( i, a[i] = rint () ) );
S += a[i];
}
if ( !calcPlan () ) { puts ( "-1" ); continue; }
Q.ptr = Q.deq.begin (), followPlan ();
printf ( "%lld\n", ans );
}
return 0;
}
\(\mathcal{Details}\)
用 std::deque
是因为只有它有迭代器啦 owo。
涉及容器元素变化(特别是 pop
,erase
之类)的时候,特别注意迭代器的操作嗷!
Solution -「LOCAL」Drainage System的更多相关文章
- Solution -「LOCAL」二进制的世界
\(\mathcal{Description}\) OurOJ. 给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...
- Solution -「LOCAL」大括号树
\(\mathcal{Description}\) OurTeam & OurOJ. 给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ( 或 ).将从 \(u\) 到 \(v\) ...
- Solution -「LOCAL」过河
\(\mathcal{Description}\) 一段坐标轴 \([0,L]\),从 \(0\) 出发,每次可以 \(+a\) 或 \(-b\),但不能越出 \([0,L]\).求可达的整点数. ...
- Solution -「LOCAL」Burning Flowers
灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\) 给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...
- Solution -「LOCAL」画画图
\(\mathcal{Description}\) OurTeam. 给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和.树形生成方式为随机取不连通两点连边直到全 ...
- Solution -「LOCAL」ZB 平衡树
\(\mathcal{Description}\) OurOJ. 维护一列二元组 \((a,b)\),给定初始 \(n\) 个元素,接下来 \(m\) 次操作: 在某个位置插入一个二元组: 翻 ...
- Solution -「LOCAL」舟游
\(\mathcal{Description}\) \(n\) 中卡牌,每种三张.对于一次 \(m\) 连抽,前 \(m-1\) 次抽到第 \(i\) 种的概率是 \(p_i\),第 \(m\) ...
- Solution -「LOCAL」充电
\(\mathcal{Description}\) 给定 \(n,m,p\),求序列 \(\{a_n\}\) 的数量,满足 \((\forall i\in[1,n])(a_i\in[1,m])\l ...
- Solution -「LOCAL」「cov. 牛客多校 2020 第五场 C」Easy
\(\mathcal{Description}\) Link.(完全一致) 给定 \(n,m,k\),对于两个长度为 \(k\) 的满足 \(\left(\sum_{i=0}^ka_i=n\r ...
随机推荐
- Ribbon原理与应用
一.定义 Ribbon是请求的负载均衡器,它为我们提供了几种负载均衡算法:轮询.随机等. 二.配置 spring: cloud: loadbalancer: retry: enabled: true ...
- sqlmap之--os-shell命令执行原理
最近也是在看sqlmap,感觉--os-shell这个命令确实很厉害,但我并不知道它的原理,所以来研究一下 环境 环境就是我本地搭的一个有sql注入漏洞的一个小demo 演示 这是我们的demo环境 ...
- SpringBoot整合Elasticsearch+ik分词器+kibana
话不多说直接开整 首先是版本对应,SpringBoot和ES之间的版本必须要按照官方给的对照表进行安装,最新版本对照表如下: (官网链接:https://docs.spring.io/spring-d ...
- [STM32F10x] 利用定时器测量脉冲宽度
硬件:STM32F103C8T6 平台: ARM-MDk V5.11 前面一篇文章讲过如何利用定时器测量信号的频率(见[STM32F10x] 利用定时器测量频率),使用的是定时器的捕获/比较单元(Ca ...
- 【记录一个问题】thanos receiver在更换tsdb文件后,内存并未显著下降
在16:14,切换了TSDB 在16::15分切换了TSDB,并且上游未写入数据 容器的内存并未在这个时刻明显下降 疑惑: 如果thanos receiver的内存占用不与time series数量正 ...
- 【记录一个问题】cv::cuda::BufferPool发生assert错误
cv::cuda::setBufferPoolUsage(true); const int width = 512; const int height = 848; const int channel ...
- Cesium和Kaarta用高分辨率激光雷达可视化室内和地下环境
Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ Cesium使急救人员和军事操作人员更容易快速评估和了解密集和不 ...
- Serverless计算
云服务的演化历程 整个it系统服务的搭建,随着时间有多个层级的演化.从最早的内部部署(On-premises) 到基于云的Iaas,Paas,Saas,Baas, Faas.服务的构建对开发者越来友好 ...
- ES6之async与await
· async - await 是 Promise 和 Generator 的语法糖,目的只是为了让我们书写代码时更加流畅,增强代码的可读性. · async - await 是建立在Promise机 ...
- 如何在pyqt中实现丝滑滚动字幕
滚动字幕的视觉效果 网上有很多博客介绍了滚动字幕的实现方法,懂得都懂,大部是 Ctrl C + Ctrl V,效果还很差,最后还是得靠自己.主要思路就是通过定时器定时刷新+绘制两段完整的字符串来达到 ...