Mashmokh and ACM CodeForces - 414D (贪心)
大意: 给定n结点树, 有k桶水, p块钱, 初始可以任选不超过k个点(不能选根结点), 在每个点放一桶水, 然后开始游戏. 游戏每一轮开始时, 可以任选若干个节点关闭, 花费为关闭结点储存水的数量和, 然后未关闭的非根结点上的水会流入父结点, 然后再开始新的一轮. 当所有非根结点无水后游戏结束, 假设第$i$轮流入根节点的水为$w_i$, 游戏共进行了$l$轮, 求$max(w_1,w_2,...,w_l)$
可以发现最优时一定是一段深度相邻的水, 所以双指针维护一段连续的区间就行了.
考虑每个区间的花费怎样计算, 一个显然的贪心策略是: 每次都关闭深度最低的点, 直到这段区间的水深度相同为止.
假设现在维护的区间为$[l,r]$, 若添加一个点$r+1$, 若$dep[r+1]=dep[r]$则花费不增加, 否则需要多等待它一回合, 花费增加$r-l+1$. 若删除左端点$l$, 花费减少量即为左端点的关闭次数$dep[r]-dep[l]$
双指针具体实现的话, 因为有0贡献点的存在, 按我以前的写法会少更新ans, 解决方法是在移动右端点前更新一下ans
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#define pb push_back
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll; const int N = 1e5+10, INF = 0x3f3f3f3f;
int n, k, p, cnt;
vector<int> g[N];
int dep[N]; void dfs(int x, int fa, int d) {
if (x!=1) dep[++cnt] = d;
for (int y:g[x]) if (y!=fa) dfs(y,x,d+1);
} int main() {
scanf("%d%d%d", &n, &k, &p);
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs(1,0,0);
sort(dep+1,dep+1+cnt);
int now = 1, w = 0, ans = 0;
REP(i,1,cnt) {
while (w<=p&&now<=cnt) {
ans = max(ans, now-i);
if (dep[now]!=dep[now-1]) w += now-i;
++now;
}
if (w<=p) ans = max(ans, now-i);
w -= dep[now-1]-dep[i];
}
printf("%d\n", min(ans, k));
}
Mashmokh and ACM CodeForces - 414D (贪心)的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- codeforces 414D Mashmokh and Water Tanks
codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...
- codeforces 414B B. Mashmokh and ACM(dp)
题目链接: B. Mashmokh and ACM time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #240 (Div. 1) B. Mashmokh and ACM DP
B. Mashmokh and ACM ...
- B. Mashmokh and ACM(dp)
http://codeforces.com/problemset/problem/414/B B. Mashmokh and ACM time limit per test 1 second memo ...
- CodeForces 414D (贪心)
problem Mashmokh and Water Tanks 题目大意 给你一棵树,k升水,p块钱,进行一次游戏. 在游戏进行前,可以在任意个节点上放置1升水(总数不超过k) 游戏进行若干轮,每轮 ...
- Codeforces 414B Mashmokh and ACM
http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...
- codeforces D.Mashmokh and ACM
题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长 ...
- CodeForces 415D Mashmokh and ACM
$dp$. 记$dp[i][j]$表示已经放了$i$个数字,并且第$i$个数字放了$j$的方案数.那么$dp[i][j] = \sum\limits_{k|j}^{} {dp[i - 1][k]}$ ...
随机推荐
- linux常用命令:tr 命令
tr 命令实现字符转换功能,其功能类似于 sed 命令,但是,tr 命令比 sed 命令简单.也就是说,tr 命令能实现的功能,sed 命令都可以实现.尽管如此,tr 命令依然是 Linux 系统下处 ...
- 深度学习的集成方法——Ensemble Methods for Deep Learning Neural Networks
本文主要参考Ensemble Methods for Deep Learning Neural Networks一文. 1. 前言 神经网络具有很高的方差,不易复现出结果,而且模型的结果对初始化参数异 ...
- Linux服务器配置---ssh配置
Ssh配置 通过配置文件,我们可以有效的管理ssh 1.空闲时间关闭连接 1)修改配置文件“/etc/ssh/sshd_config”,设置clientAliveInterval和client ...
- 查看.mobileprovision 详细信息
.mobileprovision 用诸如sublime 等工具是无法打开. 可以通过shell命令查看: security cms -D -i 某某.mobileprovision
- c++中对应java ShutdownHook的退出处理器
最近学习cpp(至于为什么,可参考http://www.cnblogs.com/zhjh256/p/6321972.html),c++标准库第二版5.8.2节的时候,发现c++有一个对应java Sh ...
- HTML5 Chart.js 框架
HTML5 Chart.js 框架 版权声明:未经博主授权,内容严禁转载 ! Chart.js 概述: chart.js 是一个简单的.面向对象.为设计者开发者准备的图表绘制工具. 点击进入官方网址 ...
- 2018-2019-1 20189218《Linux内核原理与分析》第六周作业
向menuOS中增加命令 修改menu目录下的test.c文件,增加自己的函数定义,并在修改main()函数,按照前面的menuconfig的写法写好自己的menuconfig. 我选择的是acces ...
- hdu 3336 Count the string -KMP&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- cogs 444. [HAOI2010]软件安装
★★☆ 输入文件:install.in 输出文件:install.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述]现在我们的手头有N个软件,对于一个软件i,它要 ...
- arm linux下编译库System.Net.Primitives.dll和System.Xml.XmlSerializer.dll
1.环境: /home/jello # uname -aLinux 3.10.0 #2 SMP Mon Mar 6 17:52:09 CST 2017 armv7l GNU/Linux 2.获取mo ...