题目链接: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. tf.train.match_filenames_once()

    文件匹配之用 官方解释: 调用样例: https://bbs.csdn.net/topics/392271556 返回值样例:

  2. 几种常见排序算法的基本介绍,性能分析,和c语言实现

    本文介绍6种常见的排序算法,以及他们的原理,性能分析和c语言实现: 为了能够条理清楚,本文所有的算法和解释全部按照升序排序进行 首先准备一个元素无序的数组arr[],数组的长度为length,一个交换 ...

  3. H3C STP配置示例

  4. 深浅Copy的理解

    在python里对对象进行拷贝有三个,即赋值,深拷贝,浅拷贝. 而对象又分为两种,一种是不可变对象(字符串.元组.数字 ),一种是可变对象(列表.字典). 而深浅拷贝有三种情况:拷贝的是不可变对象.拷 ...

  5. CF140CNew Year Snowmen

    CF140C 题目大意:堆雪人,需要三个大小不同的雪球,现有\(n\)个给定大小的雪球,问最多堆多少个雪人 一个很明显的思路是把每种雪球出现的个数记录下来,然后直接扔到大根堆里面,每次选择剩下出现次数 ...

  6. ZOJ Problem Set - 1090——The Circumference of the Circle

      ZOJ Problem Set - 1090 The Circumference of the Circle Time Limit: 2 Seconds      Memory Limit: 65 ...

  7. python 多线程两种实现方式,Python多线程下的_strptime问题,

    python 多线程两种实现方式 原创 Linux操作系统 作者:杨奇龙 时间:2014-06-08 20:24:26  44021  0 目前python 提供了几种多线程实现方式 thread,t ...

  8. 查看虚拟机里的Centos7的IP(设置centos网卡)

    这里之所以是查看下IP ,是我们后面要建一个Centos远程工具Xshell 连接Centos的时候,需要IP地址,所以我们这里先 学会查看虚拟机里的Centos7的IP地址 首先我们登录操作系统 用 ...

  9. vagrant在windows下的安装和配置(二)

    在(一)中安装和配置好后 框框中的信息是登录vagrant up后的系统用的 我这里登录用的是xshell-----下载一个xshell然后安装 打开xshell 按确定之后生成一个新的会话,然后登录 ...

  10. HBase写过程详解

    1首次读写流程图 2 首次写基本流程 (1)客户端发起PUT请求,Zookeeper返回hbase:meta所在的region server (2)去(1)返回的server上,根据rowkey去hb ...