题目链接: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. codeforce 382 div2 E —— 树状dp

    题意:给一棵n个结点的无根树染色,求使每个结点距离为k的范围内至少有一个被染色的结点的总染色方法数目 分析:首先我们定义: 对于结点v, 如果存在一个黑色结点u距离v不超过k,则结点v被“控制” 首先 ...

  2. 原 在windows上创建文件名以“.”开头的文件

    在实际项目开发中,我们会经常遇到以”.”开头的配置文件.例如我们如果需要通过配置文件给npm更换源就需要更改.npmrc文件,然而还有很多友友还不清楚如何创建一个类似的文件,今天就和大家分享下这个小技 ...

  3. redis cluster和hash slot

    redis cluster介绍 从redis3.0.0开始,官方支持了redis cluster的集群模式,结束了redis没有集群的时代. redis cluster 支撑 N 个 redis ma ...

  4. P1052 国王放置问题

    题目描述 在n*m的棋盘上放置k个国王,要求k个国王互相不攻击,有多少种不同的放置方法.假设国王放置在第(x,y)格,国王的攻击的区域是:(x-1,y-1), (x-1,y),(x-1,y+1),(x ...

  5. P1022 绵羊排序

    题目描述 聪聪想要给他家农场里的 \(n\) 只绵羊按照品质从高到低进行排序. 农场里的 \(n\) 只绵羊编号从 \(1\) 到 \(n\) ,第 \(i\) 只绵羊的体重为 \(w_i\) ,高度 ...

  6. 2019-8-31-AutoHotKey-用打码的快捷键

    title author date CreateTime categories AutoHotKey 用打码的快捷键 lindexi 2019-08-31 16:55:58 +0800 2019-06 ...

  7. H3C生成树的不足

  8. 2018.11.25 齐鲁工业大学ACM-ICPC迎新赛正式赛题解

    整理人:周翔 A 约数个数(难) 解法1:苗学林  解法2:刘少瑞   解法3:刘凯  解法4:董海峥 B Alice And Bob(易) 解法1:周翔  解法2:苗学林  解法3:刘少瑞 C 黑白 ...

  9. Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具

    Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...

  10. linux PCI 寻址

    每个 PCI 外设有一个总线号, 一个设备号, 一个功能号标识. PCI 规范允许单个系统占 用多达 256 个总线, 但是因为 256 个总线对许多大系统是不够的, Linux 现在支持 PCI 域 ...