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 (贪心)的更多相关文章

  1. Mashmokh and ACM CodeForces - 414D (贪心)

    大意: 给定n结点树, 有k桶水, p块钱, 初始可以任选不超过k个点(不能选根结点), 在每个点放一桶水, 然后开始游戏. 游戏每一轮开始时, 可以任选若干个节点关闭, 花费为关闭结点储存水的数量和 ...

  2. codeforces 414D Mashmokh and Water Tanks

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

  3. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

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

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

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

  7. C - Ordering Pizza CodeForces - 867C 贪心 经典

    C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...

  8. Codeforces 570C 贪心

    题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...

  9. Codeforces 732e [贪心][stl乱搞]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...

随机推荐

  1. C# string 数组 每个元素 加上单引号,每一个都被包含在单引号内

    在拼接SQL的时候经常会遇到此类问题,尤其是in查询的时候,内容是一段 单引号的 字符的时候 strWhere += " a.EC101_WRYBH  IN (" + string ...

  2. 求平均排序MATLAB code

    A0=R(:,1:2:end); for i=1:17 A1=A0(i,:); p=sort(unique(A1)); for j=1:length(p) Rank0(A1==p(j))=j; end ...

  3. asp.net 错误跳转

    每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...

  4. Octopus系列之关于多选属性如何在OO中表示呢?

    在电子商务系统中 关于产品属性的问题 会设计如下几个表 产品信息        Product 选项信息表     Option        存储 Size  Color.... 选项值信息表  O ...

  5. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  6. 理解squid的正向和反向代理

    1.相同点: 访问的走向都是:客户端 -> 代理服务器 ->真实服务器 ->代理服务器->客户端 2.不同点:正向代理语义上更侧重于,让代理服务器去帮忙请求某个网址.让代理服务 ...

  7. Facebook技术架构

    Facebook MySQL,Multifeed (a custom distributed system which takes the tens of thousands of updates f ...

  8. C#重启系统代码

    C#重启窗体代码 System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location ...

  9. Google十大惊人产品

    国外资讯网站BusinessInsider刊文细数了谷歌惊世骇俗的十大产品,范围从无人驾驶汽车到太空电梯再到高空风力发电,每一项都令人无限神往,充满未来感. 以下是谷歌十大惊人产品: 众所周知,谷歌并 ...

  10. NSString asscii格式(2进制) 转 utf8格式——解决iOS自己处理http socket数据,遇到Transfer-Encoding: chunked时

    因为需要实现自己的http客户端,就要自己模拟http 的socket通讯: 上行不难,自己处理好http即可. 但下行时,服务器端的动态语言返回数据有可能会是这种格式: http头 16进制表示的数 ...