题目链接:https://ac.nowcoder.com/acm/contest/887/C

题目描述

The Wow village is often hit by wind and sand,the sandstorm seriously hindered the economic development of the Wow village.

There is a forest in front of the Wowo village, this forest can prevent the invasion of wind and sand. But there is a rule that the number of tallest trees in the forest should be more than half of all trees, so that it can prevent the invasion of wind and sand. Cutting down a tree need to cost a certain amount of money. Different kinds of trees cost different amounts of money. Wow village is also poor.
There are n kinds of trees. The number of i-th kind of trees is PiP_iPi​, the height of i-th kind of trees is HiH_iHi​, the cost of cutting down one i-th kind of trees is CiC_iCi​.
(Note: "cutting down a tree" means removing the tree from the forest, you can not cut the tree into another height.)

输入描述:

The problem is multiple inputs (no more than 30 groups).
For each test case.
The first line contines one positive integers n(1≤n≤105)n (1 \leq n \leq 10^5)n(1≤n≤105),the kinds of trees.
Then followed n lines with each line three integers Hi(1≤Hi≤109)H_i (1 \leq H_i \leq 10^9)Hi​(1≤Hi​≤109)-the height of each tree, Ci(1≤Ci≤200)C_i (1 \leq C_i \leq 200)Ci​(1≤Ci​≤200)-the cost of cutting down each tree, and Pi(1≤Pi≤109)P_i(1 \leq P_i\leq 10^9)Pi​(1≤Pi​≤109)-the number of the tree.

输出描述:

For each test case, you should output the minimum cost.
示例1

输入

2
5 1 1
1 10 1
2
5 1 2
3 2 3

输出

1
2 对于最高的树我们有两种操作
1.留下最高的树,依次砍掉剩下的树中花费最小的树,使其满足题意
2.砍掉最高的树,维护剩下的树
求出操作1和2的花费,取最小就是答案 将n种树按高度从小到大排序,遍历n种树,对于第i高度的树,我们将把第i高的树全部砍掉的花费加上令第i-1高的树作为最高树所需的花费,与令第i高的树为最高所需的花费做比较,选取最小的花费。
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100005
struct node{
ll h,p,c;
bool operator <(const node &w)const{
return h<w.h;
}
}a[maxn];
int n;
ll num[];//存花费为i的树的数量
int main()
{
while(cin>>n)
{
ll cnt=;
for(int i=;i<=n;i++)
{
cin>>a[i].h>>a[i].c>>a[i].p;
cnt+=a[i].c*a[i].p;
}
for(int i=;i<=;i++)
num[i]=;
ll ans=cnt,tot=;
sort(a+,a++n);
int pos=;
for(int i=;i<=n;i=pos)
{
pos=i;
while(a[i].h==a[pos].h&&pos<=n)pos++;
ll sum=,cost=;//sum为目前最高树的数量
for(int j=i;j<pos;j++)
{
cnt-=a[j].c*a[j].p;
sum+=a[j].p;
}
tot+=sum;//小于等于目前最高树的数量
sum=tot-sum*+;//还需砍掉多少颗树
for(int j=;sum>;j++)
{
if(num[j]<=sum)
{
cost+=j*num[j];
sum-=num[j];
}
else
{
cost+=j*sum;
sum=;
}
}
ans=min(ans,cost+cnt);
for(int j=i;j<pos;j++)
{
num[a[j].c]+=a[j].p;
}
}
cout<<ans<<endl;
}
return ;
}

Governing sand 贪心的更多相关文章

  1. 2019牛客训练赛第七场 C Governing sand 权值线段树+贪心

    Governing sand 题意 森林里有m种树木,每种树木有一定高度,并且砍掉他要消耗一定的代价,问消耗最少多少代价可以使得森林中最高的树木大于所有树的一半 分析 复杂度分析:n 1e5种树木,并 ...

  2. Governing sand(主席树/贪心)(2019牛客暑期多校训练营(第七场))

    示例:输入:25 1 11 10 125 1 23 2 3输出:12 题意:n种树,第i种树有P[i]颗,砍掉每颗树的代价是C[i], 高度是H[i].需要用最小的花费砍掉一些树,让最高的树超过一半. ...

  3. 2019牛客多校C Governing sand——桶&&思维题

    题意 有 $n$ 种树,每种树都有高度 $H_i$,费用 $C_i$,数量 $P_i$,现要砍掉一些树,使得剩下的树中最高的树的数量超过一般,求最小的费用.($1 \leq n \leq 10^5, ...

  4. 2019牛客暑期多校训练营(第七场)-C Governing sand

    题目链接:https://ac.nowcoder.com/acm/contest/887/C 题意:有n种树,给出每种数的高度.移除的花费和数量,求最小花费是多少使得剩下树中最高的树的数量占一半以上. ...

  5. 2019牛客暑期多校训练营(第七场) - C - Governing sand - 平衡树

    5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 感觉该出14才对,取前k小写成了取前k大. 5 1 5 4 2 5 3 3 5 2 4 5 1 6 5 5 suf=55 res=0 a ...

  6. 牛客多校第七场 C Governing sand 线段树

    题意: 有一个树林,树林中不同种类的树有不同的数量,高度,砍伐它们的价格.现在要求砍掉一些树,使得高度最高的树占剩下的树的总数的一半以上,求最小花费. 题解: 用线段树维护不同种类树的信息,叶子节点从 ...

  7. 2019牛客多校第七场C-Governing sand(线段树+枚举)

    Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...

  8. 2019nc#7

    题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A String 点击查看 进入讨论 566/3539  通过 B Irreducible Polynomial 点击查看 规律 730/229 ...

  9. ZJUT11 多校赛补题记录

    牛客第一场 (通过)Integration (https://ac.nowcoder.com/acm/contest/881/B) (未补)Euclidean Distance (https://ac ...

随机推荐

  1. js 替换指定位置的字符串

    不多bb,直接代码 //str:原始字符串,index,开始位置,changeStr,改变后的字 changeStr(str,index,changeStr){ return str.substr(0 ...

  2. CF161BDiscounts

    CF161B 题目大意;要购买\(n\)件物品,有\(A\)\(B\)两种类型,要求分成\(k\)组,其中如果其中一组含有\(A\)类物品,那么这一组最便宜的一件物品就会半价 怎么分组最小化代价? 我 ...

  3. Vue通讯

    vue组件通讯 #props传递数据 父->子 //父组件传递数据 <template> <Children :data="msg"></Chi ...

  4. Java_地铁购票系统

    定义了两个类,在Subway类中定义三个私有数据变量,线路号,经过站点,换乘站.以及4个方法分别实现从txt文件中导入线路信息:输出线路信息:查询两个站点经过站点数,并输出经过站点以及在某站换乘几号线 ...

  5. [梁山好汉说IT] 熵的概念 & 决策树ID3如何选择子树

    [梁山好汉说IT] 熵的概念 & 决策树ID3如何选择子树 0x00 摘要 记录对概念的理解,用梁山好汉做例子来检验是否理解正确. 0x01 IT概念 1. 事物的信息和信息熵 1.1 事物的 ...

  6. JMM&Thread

    1.概述 高效并发通过JAVA线程之间提高并发协调实现,在实现过程中需考虑硬件的效率和一致性,但在运算的过程中需要考虑处理器与内存的交互,所以基于高速缓存的存储交互解决的处理器与内存的方案,在对多处理 ...

  7. DOCKER学习_001:Docker简介

    一 Docker简介 1.1 docker由来 Docker的英文翻译是“码头工人”,即搬运工,它搬运的东西就是我们常说的集装箱Container,Container里面装的是任意类型的App.我们的 ...

  8. 017 Ceph的集群管理_3

    一.验证OSD 1.1 osd状态 运行状态有:up,in,out,down 正常状态的OSD为up且in 当OSD故障时,守护进程offline,在5分钟内,集群仍会将其标记为up和in,这是为了防 ...

  9. 【题解】NOIP2017 提高组 简要题解

    [题解]NOIP2017 提高组 简要题解 小凯的疑惑(数论) 不讲 时间复杂度 大力模拟 奶酪 并查集模板题 宝藏 最优解一定存在一种构造方法是按照深度一步步生成所有的联通性. 枚举一个根,随后设\ ...

  10. 【题解】幼儿园篮球题(范德蒙德卷积+斯特林+NTT)

    [题解]幼儿园篮球题(NTT+范德蒙德卷积+斯特林数) 题目就是要我们求一个式子(听说叫做超几何分布?好牛逼的名字啊) \[ \sum_{i=1}^{S}\dfrac 1 {N \choose n_i ...