JZOJ 5355. 【NOIP2017提高A组模拟9.9】保命
题目
为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄做一些防火措施,保障自己、猫、奶牛的生命安全。
农夫约的农庄里有N+1 座建筑,排成了一排,编号为0~N。对于0 <=i < N,建筑i 有w[i]头奶牛居住,与建筑i+1 距离为d[i]。建筑N 已装有消防栓,现在,农夫约决定再给k 个建筑安装消防栓,以减小安全隐患。
当火灾来临时,所有奶牛会从所在建筑开始,向大编号方向逃生,直到遇上第一个消防栓(如果本来就在消防栓处,就不用跑了)。农夫约定义了一个隐患值val:所有奶牛逃生距离之和。
农夫约希望让隐患值尽可能小,需要你给他设计一个好方案。
分析
设 \(f_{i,l}\) 表示做到第 \(i\) 个,已经装了 \(l\) 个安全栓时最小答案
转移时枚举一个 \(j\),为了能快速转移,我们需要一些辅助数组
设 \(g_i\) 表示 \(i\) 之前的所有奶牛到 \(i\) 的总逃生距离
\(s_i,d_i\) 分别表示题中 \(w,d\) 的前缀和
那么 \(f_{i,j} = \min f_{j,l-1}+g_i-g_j-(d_{i-1} - d_{j-1})\times s_j\)
对于 \(j=0\) 是为了不越界特殊处理
由于它是 \(O(n^2k)\) 的,我们还需斜率优化
\(Code\)
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 1e6 + 5 , K = 25;
int n , k , pre[N][K] , q[N];
LL f[N][3] , d[N] , dd[N] , s[N] , g[N];
void print(int p , int k)
{
if (p) print(pre[p][k] , k - 1);
else return;
printf("%d " , p - 1);
}
inline double slope(int x , int y , int l)
{
LL X1 , X2;
if (x == 0) X1 = f[x][l & 1 ^ 1];
else X1 = f[x][l & 1 ^ 1] - g[x] + s[x] * d[x - 1];
if (y == 0) X2 = f[y][l & 1 ^ 1];
else X2 = f[y][l & 1 ^ 1] - g[y] + s[y] * d[y - 1];
return 1.0 * (X1 - X2) / (s[x] - s[y]);
}
inline LL read()
{
LL res = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') res = (res << 3) + (res << 1) + ch - '0' , ch = getchar();
return res;
}
int main()
{
freopen("life.in" , "r" , stdin);
freopen("life.out" , "w" , stdout);
n = read() , k = read();
for(register int i = 1; i <= n; i++) s[i] = read() , d[i] = dd[i] = read();
for(register int i = 1; i <= n + 1; i++)
s[i] += s[i - 1] , d[i] += d[i - 1] , g[i] = g[i - 1] + s[i - 1] * dd[i - 1];
for(register int i = 0; i <= n + 1; i++)
for(register int j = 0; j <= 1; j++) f[i][j] = 1e17;
f[0][0] = 0;
int h , t;
for(register int l = 1; l <= k + 1; l++)
{
q[h = t = 1] = 0;
for(register int i = 1; i <= n + 1; i++)
{
while (h < t && slope(q[h] , q[h + 1] , l) < d[i - 1]) h++;
if (q[h] == 0) f[i][l & 1] = f[q[h]][l & 1 ^ 1] + g[i];
else f[i][l & 1] = f[q[h]][l & 1 ^ 1] + g[i] - g[q[h]] - (d[i - 1] - d[q[h] - 1]) * s[q[h]];
pre[i][l] = q[h];
while (h <= t && slope(q[t - 1] , q[t] , l) > slope(q[t] , i , l)) t--;
q[++t] = i;
}
}
printf("%lld\n" , f[n + 1][(k + 1) & 1]);
print(pre[n + 1][k + 1] , k);
}
JZOJ 5355. 【NOIP2017提高A组模拟9.9】保命的更多相关文章
- JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠
JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...
- [JZOJ 100026] [NOIP2017提高A组模拟7.7] 图 解题报告 (倍增)
题目链接: http://172.16.0.132/senior/#main/show/100026 题目: 有一个$n$个点$n$条边的有向图,每条边为$<i,f(i),w(i)>$,意 ...
- 【NOIP2017提高A组模拟9.7】JZOJ 计数题
[NOIP2017提高A组模拟9.7]JZOJ 计数题 题目 Description Input Output Sample Input 5 2 2 3 4 5 Sample Output 8 6 D ...
- JZOJ 100029. 【NOIP2017提高A组模拟7.8】陪审团
100029. [NOIP2017提高A组模拟7.8]陪审团 Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Got ...
- JZOJ 5328. 【NOIP2017提高A组模拟8.22】世界线
5328. [NOIP2017提高A组模拟8.22]世界线 (File IO): input:worldline.in output:worldline.out Time Limits: 1500 m ...
- JZOJ 5329. 【NOIP2017提高A组模拟8.22】时间机器
5329. [NOIP2017提高A组模拟8.22]时间机器 (File IO): input:machine.in output:machine.out Time Limits: 2000 ms M ...
- JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)
5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...
- JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)
5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...
- JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)
5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...
- 【NOIP2017提高A组模拟9.17】信仰是为了虚无之人
[NOIP2017提高A组模拟9.17]信仰是为了虚无之人 Description Input Output Sample Input 3 3 0 1 1 7 1 1 6 1 3 2 Sample O ...
随机推荐
- Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts.
$git push origin master 报错: Warning: Permanently added the RSA host key for IP address '192.30.253.1 ...
- 【每日一题】【链表or双指针循环条件】2022年2月26日-NC96 判断一个链表是否为回文结构
描述给定一个链表,请判断该链表是否为回文结构.回文是指该字符串正序逆序完全一致. 思路: public boolean isPail (ListNode head) { ListNode node = ...
- 单点登录系统使用Spring Security的权限功能
背景 在配置中心增加权限功能 目前配置中心已经包含了单点登录功能,可以通过统一页面进行登录,登录完会将用户写入用户表 RBAC的用户.角色.权限表CRUD.授权等都已经完成 希望不用用户再次登录,就可 ...
- Django框架:1、手撸web框架、Django框架简介、安装与使用和小白必会三板斧
Django框架 目录 Django框架 一.Django推导流程 1.纯手撸web框架 2.基于wsgire模块 3.代码封装优化 4.动静态网页 5.jinja2模块 6.前端.后端.数据库三者联 ...
- 体验 Gitea Actions
即将推出的 Gitea Actions 致力于打造一个 CI/CD 工具的标准协议,第三方 CI 系统可以基于actions 协议与 Gitea 平台集成,提供一站式管理方案.Gitea Action ...
- formly-form 动态表单
动态表单库 https://github.com/ngx-formly/ngx-formly 安装 ng add @ngx-formly/schematics --ui-theme=ng-zorro- ...
- JavaScript 深拷贝的循环引用问题
如果说道实现深拷贝最简单的方法,我们第一个想到的就是 JSON.stringify() 方法,因为JSON.stringify()后返回的是字符串,所以我们会再使用JSON.parse()转换为对象, ...
- 将git仓库从submodule转换为subtree
三个脚本 Alexander Mikhailian cat .gitmodules |while read i do if [[ $i == \[submodule* ]]; then mpath=$ ...
- 使用Git提交代码
目录 1.提交前准备工作 2.代码提交步骤 3.从git上面拉代码 4.Git变更集 5.参考资料 1.提交前准备工作 首先去git官网下载git工具(Git GUI Here.Git Bash He ...
- Kali Pi 安装 RTL8812AU驱动
今天,我们来实操安装一下昨天的RTL8812的无线网卡驱动. 说明 我们今天使用的网卡是磊科的NW392无线网卡,其主要核心为NW392. 一张32G内存卡 树莓派为树莓派4B 4G-RAM 系统为 ...