CodeForces 414D (贪心)
problem Mashmokh and Water Tanks
题目大意
给你一棵树,k升水,p块钱,进行一次游戏。
在游戏进行前,可以在任意个节点上放置1升水(总数不超过k)
游戏进行若干轮,每轮游戏开放所有节点,可以选择若干个节点关闭,代价为该节点的水数量。然后所有未关闭的节点的水流向它的父亲(不会连续移动)。最后,根节点中的水会被取走,水的数量为该轮游戏的盈利。
求盈利最大的某轮游戏的盈利。
解题分析
在放置水的选择上,应该尽量选择深度相邻的节点,即将所有节点按照深度排序后,所选择放水的节点应该是连续的一段。
考虑选择某段区间后,所需要花费的钱。
假设深度范围为 [l , r] ,某个深度的点为 a[i] ,则花费钱为sigma(a[i]*(r-i))
用两个指针进行扫描即可。
参考程序
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define V 100008
#define E 200008
#define LL long long
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x)); const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/ int n,water,coin,d;
int h[V]; struct line{
int u,v,nt;
}eg[E];
int lt[V],sum; void add(int u,int v){
eg[++sum]=(line){u,v,lt[u]}; lt[u]=sum;
} void dfs(int u,int fa,int dep){
h[u]=dep;
for (int i=lt[u];i;i=eg[i].nt){
int v=eg[i].v;
if (v!=fa) dfs(v,u,dep+);
}
} int main(){
clr(lt,); sum=;
scanf("%d %d %d",&n,&water,&coin);
for (int i=;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,,);
sort(h+,h+n+);
int i=,j=,ans=;
long long cost=;
while (i<n && j<n){
j++;
if (h[j]!=h[j-]) cost+=j-i;
while (cost>coin){
cost-=h[j]-h[i];
i++;
}
ans=max(ans,j-i+);
}
printf("%d\n",min(ans,water));
}
CodeForces 414D (贪心)的更多相关文章
- Mashmokh and ACM CodeForces - 414D (贪心)
大意: 给定n结点树, 有k桶水, p块钱, 初始可以任选不超过k个点(不能选根结点), 在每个点放一桶水, 然后开始游戏. 游戏每一轮开始时, 可以任选若干个节点关闭, 花费为关闭结点储存水的数量和 ...
- codeforces 414D Mashmokh and Water Tanks
codeforces 414D Mashmokh and Water Tanks 题意 题解 \(a_i\):第 \(i\) 层的结点个数. \(b_i\):第 \(i\) 层初始有水的结点个数. 如 ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
- Codeforces 732e [贪心][stl乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...
随机推荐
- 【新手练习】类似Path的按钮,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Thread 总结
进程:是一个正在执行的程序 每一个进程执行都有一个执行顺序.该顺序是一个执行路劲,后者叫一个控制单元. 线程:就是进程中的一个独立控制单元. 线程在控制着进程的执行 一个进程中至少有个一个线程 Jav ...
- IO流--文件处理
import java.io.*; public class io { public static void main(String[] args) { ListDemo(); File dir = ...
- JavaScrip的DOM操作(13号讲)
1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型,文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西 2.Windows对象操作 一.属性和方法 二.Window.open(& ...
- 我的Github注册使用之旅
[个人介绍] 我是来自网络工程143班的姜金金,学号是1413042066.我没什么大的爱好,闲时喜欢在有阳光的午后喝喝小茶,捧一本书慢慢品茗:也喜欢散散步,欣赏细碎事物的美好,驻足沿路美丽的风景.说 ...
- 转 Warning:MongoDB Replica Sets配置注意事项
我们知道,MongoDB不提供单机的数据安全性,取而代之的是提供了Replica Sets的高可用方案.官方文档中提到的案例是三个节点组成的Replica Sets,这样在其中任何一个节点宕机后都会自 ...
- FZU 2092 收集水晶 bfs+记忆化搜索 or 暴力
题目链接:收集水晶 一眼看过去,觉得是普通的bfs,初始位置有两个.仔细想了想...好像如果这样的话..........[不知道怎么说...T_T] dp[12][12][12][12][210] 中 ...
- hadoop集群配置实例
1)ssh配置 http://allthingshadoop.com/2010/04/20/hadoop-cluster-setup-ssh-key-authentication/ 2) 修改打开文件 ...
- 利用dispatch_once创建单例
无论是爱还是恨,你都需要单例.实际上每个iOS或Mac OS应用都至少会有UIApplication或NSApplication. 什么是单例呢?Wikipedia是如此定义的: 在软件工程中,单例 ...
- Ubuntu: an error occurred while mounting /mnt/hgfs
对于这个error,我采用的一个不完美的方法是: vi /etc/fstab .host:/projectname /mnt/hgfs vmhgfs rw,ttl=,uid=my_uid,gid=my ...