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上取整. 有无限个变 ...
随机推荐
- express+nodecoffee写passport登录验证实例(一)
项目中要用到passport登录验证,环境如标题样:express框架,coffee模版引擎,node后台 一:建项目 直接用express命令建,虽然默认模版为jade,可以手动换成coffee哦. ...
- 镜像渐变-radio-gradient
2013年11月15日13:08:37 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"&g ...
- oracle知识点
创建序列 create sequence a_seq --创建序列名字为 a_seqminvalue 1 -- 最小值为 1maxvalue 99999 --- 最大值为 99999 start wi ...
- Zookeeper注册节点的掉线自动重新注册及测试方法
转载:http://www.codelast.com/ 在一套分布式的online services系统中,各service通常不会放在一台服务器上,而是通过Zookeeper这样的东西,将自己的se ...
- vim多行注释和取消多行注释
多行注释: 1. 进入命令行模式,按ctrl + v进入 visual block模式(可视快模式),然后按j, 或者k选中多行,把需要注释的行标记起来 2. 按大写字母i,再插入注释符,例如// 3 ...
- PMP 项目管理
1.什么是项目管理 项目管理就是把各种知识,技能,工具,技术应用于项目活动,来满足项目的需求.这个是从技术方面来说的.其实在项目管理的技术 层面背后,还有理念层面的内容.学习项目管理,除了学习技术 ...
- 转: 详解css中的display属性
在一般的CSS布局制作时候,我们常常会用到display对应值有block.none.inline这三个值.下面我们来分别来认识和学习什么时候用什么值.这里通过CSS display知识加实例讲解方法 ...
- FOJ 2105 Digits Count
题意:对一串数字进行抑或某数,和某数,或某数,统计某区间和的操作. 思路:因为化成二进制就4位可以建4颗线段树,每颗代表一位二进制. and 如果该为是1 直接无视,是0则成段赋值为0: or 如 ...
- 仿淘宝js图片切换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 近期C++编译问题汇总
编译c++ 代码中遇到几个问题,汇总一下: 1.编译openssl 遇到问题如图 , 原因:不支持汇编编译,在perl编译指令中加入: no-asm , 如:perl Configure VC-WI ...