Luogu 3479 [POI2009]GAS-Fire Extinguishers
补上了这一道原题,感觉弱化版的要简单好多。
神贪心:
我们设$cov_{x, i}$表示在$x$的子树中与$x$距离为$i$的还没有被覆盖到的结点个数,设$rem_{x, i}$表示在$x$的子树中与$x$的距离为$i$的已经设立的消防站还能覆盖的点数。
对于每一个$x$,我们考虑让它去管辖与它距离为$k$的点(即$cov_{x, k}$),因为这些点是必须由$x$来管辖的(即使在$x$的父亲处设立的消防站也管辖不到),容易知道需要这样的消防站的个数为$\left \lceil \frac{cov_{x, k}}{s} \right \rceil$。
那么设立了这样的消防站之后$x$的$rem$肯定会有剩余,我们直接贪心地去用$rem$覆盖$cov$,如果想要使效果最大化,那么用$rem_{x, i}$来抵消$cov_{x, i/i - 1}$就好了,其他的让$x$的父亲来抵消,可以知道这样子答案一定不会变差。
最后看看$1$号根节点有多少还不能覆盖的(设为$cnt$),答案加上$\left \lceil \frac{cnt}{s} \right \rceil$就好。
注意$rem, cov, ans$乘起来会爆$int$。
时间复杂度$O(nk)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 1e5 + ;
const int M = ; int n, s, k, tot = , head[N];
ll ans = 0LL, rem[N][M], cov[N][M]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} void dfs(int x, int fat) {
cov[x][] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x); for(int j = ; j <= k; j++)
cov[x][j] += cov[y][j - ];
for(int j = k; j >= ; j--)
rem[x][j - ] += rem[y][j];
} ll now = (cov[x][k] + s - ) / s;
ans += now, rem[x][k] += now * s; for(int i = ; i <= k; i++) {
if(!rem[x][i]) continue;
for(int j = i; j >= && (j >= i - || x == ); j--) {
if(cov[x][j] >= rem[x][i]) {
cov[x][j] -= rem[x][i];
rem[x][i] = ;
break;
}
rem[x][i] -= cov[x][j];
cov[x][j] = ;
}
}
} int main() {
read(n), read(s), read(k);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
} dfs(, ); ll now = ;
for(int i = ; i <= k; i++)
now += cov[][i];
ans += (now + s - ) / s; printf("%lld\n", ans);
return ;
}
Luogu 3479 [POI2009]GAS-Fire Extinguishers的更多相关文章
- Luogu P3489 [POI2009]WIE-Hexer 最短路
https://www.luogu.org/problemnew/show/P3489 普通的最短路,不过我觉得这个复杂度按道理来说边数不应该是m*2^13吗,不知道是数据比较水还是实际上能证明复杂度 ...
- 解题:POI 2009 Fire Extinguishers
题面 洛谷数据非常水,建议去bzoj 我第一眼一看这不是那个POI2011的升级版吗(明明这个是2009年的,应该说那个是这个的弱化版,果然思想差不多. 因为$k$很小,可以考虑每个间隔距离来转移.我 ...
- luogu 3488 [POI2009]LYZ-Ice Skates 线段树 + 思维
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin), ...
- Luogu 3942 将军令
之前写那个(Luogu 2279) [HNOI2003]消防局的设立的时候暴力推了一个树形dp,然后就导致这个题不太会写. 贪心,先把树建出来,然后考虑按照结点深度排个序,每次取出还没有被覆盖掉的深度 ...
- BZOJ1117 [POI2009]救火站Gas 贪心
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ1117.html 题目传送门 - BZOJ1117 题意 给你一棵树,现在要建立一些消防站,有以下要求: ...
- [POI2009]救火站Gas
Description 给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站. 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上 ...
- [luogu]P1053 篝火晚会[数学][群论]
[luogu]P1053 篝火晚会 题目描述 佳佳刚进高中,在军训的时候,由于佳佳吃苦耐劳,很快得到了教官的赏识,成为了“小教官”.在军训结束的那天晚上,佳佳被命令组织同学们进行篝火晚会.一共有n个同 ...
- [luogu]P1016 旅行家的预算[贪心]
[luogu]P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能 ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- Uncaught TypeError: this.canvas.getContext is not a function
/**************************************************************************** * Uncaught TypeError: ...
- 用Java实现异构数据库的高效通用分页查询功能
不同数据库的分页查询语句有着较大区别,其中MySQL数据的limit offset语法最为简单,而SQL Server数据库和Oracle数据库的分页就比较复杂了. 网上常见的SQL Server和O ...
- golang实现模拟键盘按键
公司前段时间要我写个小项目需要可以局域网内一个ipad控制另一台pc上的键盘输入,github上找了找,居然有个robotgo库这么神级的存在,感觉go的库真是越来越多了,虽然大部分都是第三方的.ht ...
- LeetCode Maximum Average Subarray I
原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...
- Photon多人游戏开发教程
http://gad.qq.com/article/detail/26112 PUN介绍 入门 Photon Unity Networking(首字母缩写PUN)是一个Unity多人游戏插件包.它提供 ...
- WebSocket --为什么引入WebSocket协议
Browser已经支持http协议,为什么还要开发一种新的WebSocket协议呢?我们知道http协议是一种单向的网络协议,在建立连接后,它只允许Browser/UA(UserAgent)向WebS ...
- 使用Json Template在Azure China创建ARM类型的虚拟机
前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM.并且介绍了如何用Azure CLI和PowerShell创建虚拟机.本文将介绍如何采用Json的Template来创建基于ARM的VM ...
- svn-clearup 报错的处理(Cleanup failed to process the following paths...)
在使用 svn 客户端执行操作失败后,执行 Clean up 操作也报错:Cleanup failed to process the following paths... ,一直不知道是什么原因.通常 ...
- 【转】前端上传组件Plupload使用指南
http://www.cnblogs.com/2050/p/3913184.html Plupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统的 ...
- python-xlrd api
1.导入模块 import xlrd from xlrd import open_workbook 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFi ...