codeforces 414D Mashmokh and Water Tanks
codeforces 414D Mashmokh and Water Tanks
题意
题解
\(a_i\):第 \(i\) 层的结点个数。
\(b_i\):第 \(i\) 层初始有水的结点个数。
如果不允许关闭水塔,最后的答案就是 \(max\{a_i\}\)。
现在允许关闭部分水塔,我们可以把一些连续层数的水汇聚到同一层。假设我们汇聚 \([l, r]\) 范围的水,总花费是 \(\Sigma_{i=l}^r\{b_i*(r-i)\}\)。
双指针实现即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(x) (int)x.size()
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N=101010;
int n,k,p,ma;
int a[N];
vi g[N];
void dfs(int u,int fa,int d) {
++a[d];
ma=max(d, ma);
rep(i,0,sz(g[u])) {
int v=g[u][i];
if(v==fa) continue;
dfs(v, u, d+1);
}
}
int main() {
while(~scanf("%d%d%d",&n,&k,&p)) {
///init
rep(i,0,n+1) g[i].clear();
ma=0;
memset(a,0,sizeof(a));
///read
rep(i,1,n) {
int u,v;scanf("%d%d",&u,&v);
g[u].pb(v);
g[v].pb(u);
}
///solve
dfs(1,1,0);
ll c=0, v=0, f=a[1], ans=0;
for(int l=1, r=1;r<=ma;++r) {
c+=v;
v+=a[r];
while(v>k) {
ll sub=min(f, v-k);
c-=sub*(r-l);
v-=sub;
f-=sub;
if(f==0) {
++l;
f=a[l];
}
}
while(c>p) {
ll sub=min(f, (c-p)/(r-l)+((c-p)%(r-l)>0));
c-=sub*(r-l);
v-=sub;
f-=sub;
if(f==0) {
++l;
f=a[l];
}
}
ans=max(ans, v);
}
printf("%lld\n",ans);
}
return 0;
}
/*
10 2 1
1 2
1 3
3 4
3 5
2 6
6 8
6 7
9 8
8 10
5 1000 1000
1 2
1 3
3 4
3 5
*/
codeforces 414D Mashmokh and Water Tanks的更多相关文章
- CF414D Mashmokh and Water Tanks
CF414D Mashmokh and Water Tanks 洛谷评测传送门 题目描述 Mashmokh is playing a new game. In the beginning he has ...
- CodeForces 414D (贪心)
problem Mashmokh and Water Tanks 题目大意 给你一棵树,k升水,p块钱,进行一次游戏. 在游戏进行前,可以在任意个节点上放置1升水(总数不超过k) 游戏进行若干轮,每轮 ...
- Mashmokh and ACM CodeForces - 414D (贪心)
大意: 给定n结点树, 有k桶水, p块钱, 初始可以任选不超过k个点(不能选根结点), 在每个点放一桶水, 然后开始游戏. 游戏每一轮开始时, 可以任选若干个节点关闭, 花费为关闭结点储存水的数量和 ...
- Codeforces 877 C. Slava and tanks
http://codeforces.com/problemset/problem/877/C C. Slava and tanks time limit per test 2 seconds me ...
- Codeforces 414B Mashmokh and ACM
http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...
- 【codeforces 175D】 Plane of Tanks: Duel
http://codeforces.com/problemset/problem/175/D (题目链接) 题意 A,B两人玩坦克大战,坦克有生命值,射击间隔,伤害范围,未命中的概率.问A赢的概率是多 ...
- Codeforces 414C Mashmokh and Reverse Operation
题意:给你2^n个数,每次操作将其分成2^k份,对于每一份内部的数进行翻转,每次操作完后输出操作后的2^n个数的逆序数. 解法:2^n个数,可以联想到建立一棵二叉树的东西,比如 2,1,4,3就可以 ...
- codeforces D.Mashmokh and ACM
题意:给你n和k,然后找出b1, b2, ..., bl(1 ≤ b1 ≤ b2 ≤ ... ≤ bl ≤ n),并且对所有的bi+1%bi==0,问有多少这样的序列? 思路:dp[i][j] 表示长 ...
- codeforces C. Mashmokh and Numbers
题意:给你n和k,然后让你找出n个数使得gcd(a1,a2)+gcd(a3,a4)+......的和等于k: 思路:如果n为奇数,让前n-3个数的相邻两个数都为1,n-2和n-1两个数gcd为k-an ...
随机推荐
- UVM系统验证基础知识0(Questasim搭建第一个UVM环境)
版权声明:本文为Times_poem原创文章,转载请告知原博主.特别声明:本文在原文基础上做了简单修改以适应文中举例在questasim下的运行,敬请原博主谅解. 需求说明:UVM系统验证 内容 ...
- [shell进阶]——shell多线程
关于shell的多线程 1. 多线程并发执行任务,而不用一台台的串行执行,能更快更高效 2. Shell并没有多线程的概念,所以: * 一般使用wait.read等命令技巧性地模拟多线程实 * 使用命 ...
- 问题集录--jquery将json转excel保持
代码如下: <html> <head> <meta http-equiv="content-type" content="text/html ...
- Jquery 搜索框自动提示
为文本框增加自动提示下拉功能,比如输入 1,则从后台数据库查询出包含1 的字段,在文本框增加下拉列表供用户选择 ajax 返回数据为搜索查询字段的json集合 <script src=" ...
- [转]The NTLM Authentication Protocol and Security Support Provider
本文转自:http://davenport.sourceforge.net/ntlm.html#ntlmHttpAuthentication The NTLM Authentication Proto ...
- C# 实现将listview中已经显示的数据导出到Access 数据库
private void button1_Click(object sender, EventArgs e) { OleDbConnection dbconn = new OleDbConnectio ...
- 八、curator recipes之选举主节点LeaderSelector
简介 前面我们看到LeaderLatch对于选举的实现:https://www.cnblogs.com/lay2017/p/10264300.html 节点在加入选举以后,除非程序结束或者close( ...
- 小菜庄园 Spring------图片的上传和下载
1.图片的上传前台三个条件 文件上传页面的3个要求: < 1.表单提交为post.mothod="post" < 2.表单的 ...
- 高并发第二弹:并发概念及内存模型(JMM)
高并发第二弹:并发概念及内存模型(JMM) 感谢 : 深入Java内存模型 http://www.importnew.com/10589.html, cpu缓存一致性 https://www.cnbl ...
- C# 后台处理图片的几种方式
第一种: 将上传图片直接保存到本地 var supportedTypes = new[] { "jpg", "jpeg", "png", & ...