[CF1223G/1240E]Wooden Raft 题解
前言
上午一场模拟赛(发布前很久,因为这题题解太长了),发现T3特别珂怕,打开题解,发现一行字:
不要再问了,再问就是CF 1240E
当场去世.jpg。
在下文中,我们记 \(A\) 为 \(a\) 数组中的最大值,在代码中就是 "_max" 。
题意简述
题目链接
给出一组 \(n\) 块木板以及它们的长度 \(a_i\),现在要切割出两块木板使之长度为 \(x\) ,切割 \(x\) 块木板使之长度为 \(y\)。
求 \(x \times y\) 的最大值。

题解
基本思想
我们有一个非常美妙的使用二分的 \(\Theta(Aln_Alog_A)\) 的做法,
但是我们今天要说的是一个比它优秀的 \(\Theta(Aln_A)\) 的算法。
为什么说是 \(ln_A\) ,因为 \(\sum_{i=1}^{n} n / i = ln_A\) (调和级数)。
对于几乎所有的做法,都有两个显然的思想,
枚举 \(y\) ,然后计算 \(x\);
贪心地把长度为 \(l\) 的木板分解为 \(l = tx + ky + \delta, t \in [0, 2]\) 其中 \(\delta\) 越小越好。
有了这个思想以后我们还需要按照 \(l / y\) 的值对木板分块(即块的区间 \([ ky, (k+1)y )\) )。

接下来我们可以来进行一波分类讨论。
两个 \(x\) 在同一块木板中被切割出

我们记 \(p_1, p_2\) 为两个点,且 \(p_1\) 距离它所在块的右端点比 \(p_2\) 更远。
可以证明, \(p_1\) 的决策一定劣于 \(p_2\) ,因为在抛去 \(ky\) 的情况下,
选择 \(p_2\) 所得的 \(x\) 显然更大。
那么我们从右往左扫描,设当前的块为 \(c\) 。
在式子 \(l = x + ky + \delta\),若当前的块为 \(c\) ,当前块往右的所有块(包括自己)里所在块右端点最远的点为 \(p\) 。
则定义\(x = (c \times y + p \% y) / 2\) ,剩下的部分给 \(ky\) 。
记 \(p\) 在 \(c\) 中的映射为 \(p'\),即 \(p' \in c\) 且 \(p' \equiv p \mod(y)\)(下文中的 \(p_1',p_2'\) 同理)。

具体如上图所示。
两个 \(x\) 分别在两块木板中被切割出
根据上面的结论,我们仍然从右往左扫描,设当前的块为 \(c\)。然后我们记,
当前块往右的所有块(包括自己)里所在块右端点最远的木板为 \(p_1\) 。
当前块往右的所有块(包括自己)里所在块右端点次远的木板为 \(p_2\) 。
那么我们总结出并讨论如下四种情况:
情况1、2:
\(\quad \quad\)
我们在情况一内分类讨论,发现只有两种情况可能最优,
第一种是把 \(p_2\) 木板分解完全(即 \(=x + ky\),\(k\) 为 \(c\) 到 \(p_2\) 所在块跨越的块数),第二种是把 \(p_1\) 木板分解完全。
但是注意到如果 \(p_1\) 木板分解完全,那么 \(p_2\) 木板长度显然不够了,于是我们牺牲 \(p_2\) 木板裁出的一个 \(y\) ,来保证能够切除一个 \(x\) 。
再来考虑情况二,我们发现情况二的不同之处是, \(p_2\) 木板不能够牺牲一个 \(y\) 来保证切除 \(x\) 了(长度不足),
那么处理方法很简单,直接舍弃掉完全分解 \(p_1\) 方案(滑稽),只考虑完全分解 \(p_2\)。
您觉得讨论结束了?还有两种哦 QwQ 。
情况3:

我们发现情况3与情况1相同(显然易见还是将 \(p_1\) 或 \(p_2\) 分解完全)。
情况4

我们发现情况4与情况2相同(显然易见还是不能牺牲 \(p_2\) 的一个 \(y\))。
然后我们注意到计算的时候,\(x\) 不一定小于 \(y\)的个数,所以要取一个 \(min\) 。
代码
note: 我 CodeForces 账号是 \(lukelin\) 哦(所以不要说我的代码是copy的)。
注释是英文的,因为怕出玄学错误。
/*
author: lukelin
note: I'm sorry that my English is poor.
*/
#include <cstdio>
// input data
int a[500005];
// make length -> pos & the prefix
int cnt[1000005], prfc[1000005];
int l1[1000005], l2[1000005];
#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a>b)?a:b)
inline void swap(int &a, int &b){
int tmp = a; a = b, b = tmp;
}
long long ans;
inline void flush(int x, int cnty, int y){
if (min(cnty, x) <= 1) return ;
ans = max(ans, 1ll * min(cnty, x) * y);
}
int main(){
int n, _max = 0; scanf("%d", &n);
for (int i = 1; i <= n; ++i){
scanf("%d", &a[i]), ++cnt[a[i]];
if (_max < a[i]) _max = a[i];
}
int prfc_lim = _max << 1; //the max id we will use in prfc
for (int i = 1; i <= prfc_lim; ++i) prfc[i] = prfc[i - 1] + cnt[i];
l1[0] = l2[0] = -1;
for (int i = 1; i <= prfc_lim; ++i){
//l1 - the longest wood which less equal than i
//l2 - the second longest wood which less equal than i
if (cnt[i] >= 2) l1[i] = l2[i] = i;
else if (cnt[i] == 1) l2[i] = l1[i - 1], l1[i] = i;
else l1[i] = l1[i - 1], l2[i] = l2[i - 1];
}
for (int y = 2; y <= _max; ++y){
int ycnt = 0;
for (int c = 1; c * y <= _max; ++c){
//prefix[block c's end] - prefix[block c's begin - 1]
ycnt += (prfc[c * y + y - 1] - prfc[c * y - 1]) * c;
}
// first the situation that both x in one wood
int p = -1;
for (int c = _max / y + 1; ~c; --c){
// the start position and the end position of current block(c)
int blk_sp = c * y, blk_ep = c * y + y - 1;
if (l1[blk_ep] >= blk_sp && (p == -1 || p % y < l1[blk_ep] % y))
p = l1[blk_ep];
if (~p)
flush((c * y + p % y) >> 1, ycnt - c, y);
}
// second the situation that each x in one different wood
int p1 = -1, p2 = -1;
for(int c = _max / y + 1; ~c; --c){
// the start position and the end position of current block(c)
int blk_sp = c * y, blk_ep = c * y + y - 1;
if(~p2){
//situation 1 - now both p1 and p2 is behind c
flush(c * y + p1 % y, ycnt - c * 2 - 1, y);
flush(c * y + p2 % y, ycnt - c * 2, y);
}
if (~p1 && l1[blk_ep] >= blk_sp && l1[blk_ep] % y > p2 % y){
if (l1[blk_ep] % y >= p1 % y){
//situation 3 - p1 is in c, p2 is behind c
flush(c * y + p1 % y, ycnt - c * 2, y),
flush(l1[blk_ep], ycnt - c * 2 - 1, y);
}
else{
//situation 4 - p1 is behind c, p2 is in c
flush(l1[blk_ep], ycnt - c * 2, y);
}
}
//update
if (l1[blk_ep] >= blk_sp && p2 % y < l1[blk_ep] % y)
p2 = l1[blk_ep];
if (p1 % y < p2 % y) swap(p1, p2);
if (l2[blk_ep] >= blk_sp && p2 % y < l2[blk_ep] % y)
p2 = l2[blk_ep];
if (p1 % y < p2 % y) swap(p1, p2);
if (~p2){
//situation 2 - p1, p2 is both behind c
//* if (p1, p2) isn't both in c, it's useless but won't cause WA
flush(c * y + p2 % y, ycnt - c * 2, y);
}
}
}
printf("%I64d", ans);
return 0;
}
[CF1223G/1240E]Wooden Raft 题解的更多相关文章
- HDU 1051 Wooden Sticks 贪心题解
本题一看就知道是最长不减序列了,一想就以为是使用dp攻克了. 只是那是个错误的思路. 我就动了半天没动出来.然后看了看别人是能够使用dp的,只是那个比較难证明其正确性,而其速度也不快.故此并非非常好的 ...
- UVALive 7276 Wooden Signs (DP)
Wooden Signs 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/E Description http://7xjob4. ...
- HDU1051 Wooden Sticks 【贪婪】
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Wooden Sticks -HZNU寒假集训
Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...
- HDU 1005 Wooden Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1051 Problem Description There is a pile of n wooden stick ...
- HDU-1051/POJ-1065 Wooden sticks 木棍子(动态规划 LIS 线型动归)
嘤嘤嘤,实习半年多的小蒟蒻的第一篇博客(题解) 英文的: There is a pile of n wooden sticks. The length and weight of each stick ...
- 分布式系统理论进阶 - Raft、Zab
引言 <分布式系统理论进阶 - Paxos>介绍了一致性协议Paxos,今天我们来学习另外两个常见的一致性协议——Raft和Zab.通过与Paxos对比,了解Raft和Zab的核心思想.加 ...
- 分布式一致性算法--Raft
前面一篇文章讲了Paxos协议,这篇文章讲它的姊妹篇Raft协议,相对于Paxos协议,Raft协议更为简单,也更容易工程实现.有关Raft协议和工程实现可以参考这个链接https://raft.gi ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
随机推荐
- C++学习 之 类的声明及成员的访问(笔记)
1.类的声明 简单来说,属性以及对属性的操作的整合叫做类.要声明类可使用关键字class,并在它的后面定义类名,然后紧接着是属于该类的代码块{}.类的声明类似于函数声明,类的声明本身并不改变程序 的行 ...
- Luogu P4878 [USACO05DEC]布局
题目 差分约束模板. 注意判负环需要建一个超级源点到每个点连一条\(0\)的边.因为\(1\)不一定能到达所有的点. #include<bits/stdc++.h> #define pi ...
- c++ 【递归算法】梵塔问题
一道递归水题,2话不说,直接放代码: #include<iostream> using namespace std; int k; void move(int m,char a,char ...
- EXKMP模版
这道题目折腾了我好一会啊,出于尊重我要先放我们师兄的博客 1178: [视频]EXKMP模版:最长共同前缀长度 时间限制: 1 Sec 内存限制: 128 MB提交: 180 解决: 123[提交 ...
- thinkphp5.1 关于加载静态资源路径问题
和thinkphp5.0不一样,thinkphp5.1的 thinkphp5.0的 直接在config.php文件中加入代码: <?phpreturn [ 'view_replace_str' ...
- python之成像库pillow
目录 python之成像库pillow 官方文档 图像模块(Image.Image) Image模块的功能 Image.new(mode,size,color): Image.open(file,mo ...
- C数据结构排序算法——直接插入排序法用法总结(转http://blog.csdn.net/lg1259156776/)
声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 排序相关的的基本概念 排序:将一组杂乱无章的数据按一定的规律顺次排列起来. 数据表( data list): ...
- Vue2.X 通过 ajax 获取 API 数据(非 axios)
不多废话,笔记如下 1. javascript: let vm = new Vue({ el: '#card-text', data: { info: '' }, beforeCreate: func ...
- Charles学习(一)之macOS Charles 4.x版本的安装、激活、使用以及软件功能了解
前言 Charles是mac上一款比较好用的抓包工具,那么我们什么情况下需要用到抓包工具呢?比如我想查看一个接口请求的参数.返回值,还有移动设备上的http/https请求. Charles是一个HT ...
- 一款完美代替微信小程序原生客服消息的工具:
一.设置:无需开发,多种回复(自动+人工) 自动回复形式有3种: 打开客服消息(用户只要和客服互动过一次,再次点击进入,会收到设置好的自动回复) 关键词回复(用户在小程序中回复某个关键词内容时,会 ...