大意: 给定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 (贪心)的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. codeforces 414D Mashmokh and Water Tanks

    codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...

  3. 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 ...

  4. Codeforces Round #240 (Div. 1) B. Mashmokh and ACM DP

                                                 B. Mashmokh and ACM                                     ...

  5. B. Mashmokh and ACM(dp)

    http://codeforces.com/problemset/problem/414/B B. Mashmokh and ACM time limit per test 1 second memo ...

  6. CodeForces 414D (贪心)

    problem Mashmokh and Water Tanks 题目大意 给你一棵树,k升水,p块钱,进行一次游戏. 在游戏进行前,可以在任意个节点上放置1升水(总数不超过k) 游戏进行若干轮,每轮 ...

  7. Codeforces 414B Mashmokh and ACM

    http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...

  8. codeforces D.Mashmokh and ACM

    题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长 ...

  9. CodeForces 415D Mashmokh and ACM

    $dp$. 记$dp[i][j]$表示已经放了$i$个数字,并且第$i$个数字放了$j$的方案数.那么$dp[i][j] = \sum\limits_{k|j}^{}  {dp[i - 1][k]}$ ...

随机推荐

  1. Python tricks(4) -- with statement

    简介 with是从2.5版本引入的一个语法. 这个语法本身是为了解决try..finally繁琐的释放各类资源(文件句柄, Lock等)的问题. 如果想在旧版本中使用这个功能, 直接引入future模 ...

  2. chrome谷歌浏览器用这种方式清除缓存比较方便了,必须是调试模式才行

     chrome谷歌浏览器用这种方式清除缓存比较方便了  PS:必须是调试模式才行,可以不是手机模式 ,有些低版本浏览器可能没有这个功能.   ----------------------------- ...

  3. C++提供了四个转换运算符

    const_cast <new_type> (expression) static_cast <new_type> (expression) reinterpret_cast ...

  4. Contiki源码+原理+功能+编程+移植+驱动+网络(转)

    源:Contiki源码+原理+功能+编程+移植+驱动+网络 请链接:http://www.rimelink.com/nd.jsp? id=31&_np=105_315 假设您对于用Contik ...

  5. 01: 安装zabbix server

    目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...

  6. PHP 开发 api 接口安全验证

    原文链接:http://blog.csdn.net/li741350149/article/details/62887524

  7. Java位运算实现加减乘除

    一.加法 a+b 举例实现:13+9=22 13+9不考虑进位结果为12 只考虑进位结果为10 和刚好是22. 13二进制为1101,9二进制为1001. 不考虑进位结果为0100.算式为a^b 只考 ...

  8. Java DecimalFormat 用法(数字格式化)

    我们经常要将数字进行格式化,比如取2位小数,这是最常见的.Java 提供 DecimalFormat 类,帮你用最快的速度将数字格式化为你需要的样子.下面是常用的例子: import java.tex ...

  9. Django组件(一) Django之分页器

    Django的分页器(paginator)简介 在页面显示分页数据,需要用到Django分页器组件 from django.core.paginator import Paginator Pagina ...

  10. Linux写时拷贝技术【转】

    本文转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html COW技术初窥: 在Linux程序中,fork()会产 ...