题目链接: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. vue 路由跳转前确认框,刷新浏览器页面前提示确认框

    先看效果图: 1.刷新页面效果: 2.跳转路由(进入别的页面前)效果: 代码: // 路由跳转确认 beforeRouteLeave(to, from, next) { const answer = ...

  2. 【t056】智力问答(multiset做法)

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 新年联欢会上,G.Sha负责组织智力问答节目.G.Sha建立了一个很大很大的超级题库,并衡量了每道题的 ...

  3. excel转换成实体

    package com.cinc.ecmp.utils; import java.io.IOException; import java.io.InputStream; import java.lan ...

  4. ZR提高失恋测2(9.7)

    ZR提高失恋测2(9.7) 网址http://www.zhengruioi.com/contest/392 版权原因,不放题面 A 首先,我们发现对于匹配串\(s\)中所有满足\(s_i \not = ...

  5. 2018-2-13-WPF-异常-NativeWPFDLLLoader.LoadNativeWPFDLL

    title author date CreateTime categories WPF 异常 NativeWPFDLLLoader.LoadNativeWPFDLL lindexi 2018-2-13 ...

  6. NuGet 如何设置图标

    在找 NuGet 的时候可以看到有趣的库都有有趣的图标,那么如何设置一个 NuGet 的图标 在开始之前,请在nuget官方网站下载 NuGet.exe 同时设置环境变量 环境变量设置的方法就是将 N ...

  7. 【u035】奶牛的电信

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方 ...

  8. codeforces 1167B Lost Numbers

    传送门:https://codeforces.com/contest/1167/problem/B 题意: 交互题:现在你有6个数4, 8, 15, 16, 23, 42组成的某种组合,你可以询问系统 ...

  9. Java方法参数:

    一个方法不能修改一个基本数据类型的参数 一个方法可以改变一个对象参数的状态 一个方法不能实现让对象参数引用一个新的对象 案例1: 一个方法不能修改一个基本数据类型的参数 String a = &quo ...

  10. Python7_内置函数总结

    Python Built-In 函数: str(obj) :输入(对象),返回一个对象的string格式: isinstance(object,classinfo):判断一个对象是否是一个已知的类型, ...