Governing sand

题目传送门

解题思路

枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面需要砍掉的个数。

所以,可以利用权值线段树,按照高度从小到大的枚举顺序将各个种类的树放入,维护每个节点的树的棵数以及花费,按照枚举的顺序求出各个高度作为最大高度的最小花费,其中最小的就是答案。需要注意一下的是,当你把一种高度作为最大高度,你要砍的树不应该包含它,所以先求花费,再插入。

代码如下

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int N = 100005; struct T{
int l, r;
ll c;
ll sum;
}tree[N<<2]; struct R{
ll h, c, p;
bool operator<(const R& a)const{
return h < a.h;
}
}a[N]; void build(int k, int l, int r)
{
tree[k].l = l, tree[k].r = r;
tree[k].c = tree[k].sum = 0;
if(tree[k].l == tree[k].r)
return;
int mid = (tree[k].l + tree[k].r) / 2;
build(2*k, l, mid);
build(2*k+1, mid + 1, r);
} void insert(int k, int x, ll c)
{
if(tree[k].l == tree[k].r){
tree[k].c += c;
tree[k].sum += tree[k].l * c;
return;
}
int mid = (tree[k].l + tree[k].r) / 2;
if(x <= mid)
insert(2*k, x, c);
else
insert(2*k+1, x, c);
tree[k].c = tree[2*k].c + tree[2*k+1].c;
tree[k].sum = tree[2*k].sum + tree[2*k+1].sum;
} ll query(int k, ll x)
{
if(tree[k].l == tree[k].r)
return tree[k].l * x;
if(tree[2*k].c >= x)
return query(2*k, x);
else {
x -= tree[2*k].c;
return query(2*k+1, x) + tree[2*k].sum;
}
} int main()
{
int n;
while(scanf("%d", &n) != EOF){
ll sum = 0, num = 0;
for(int i = 1; i <= n; i ++){
scanf("%lld%lld%lld", &a[i].h, &a[i].c, &a[i].p);
sum += a[i].c * a[i].p;
}
build(1, 1, 200);
ll ans = 2000000000000000000LL;
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i ++){
sum -= a[i].p * a[i].c;
num += a[i].p;
ll ct = a[i].p;
int j = i;
while(j != n && a[j].h == a[j + 1].h){
j ++;
sum -= a[j].p * a[j].c;
num += a[j].p;
ct += a[j].p;
}
ll cut = num - 2 * ct + 1;
ll cur = sum;
if(cut > 0)
cur += query(1, cut);
ans = min(ans, cur);
insert(1, a[i].c, a[i].p);
while(i != n && a[i].h == a[i + 1].h){
i ++;
insert(1, a[i].c, a[i].p);
}
}
printf("%lld\n", ans);
}
return 0;
}

2019牛客多校第七场C-Governing sand(线段树+枚举)的更多相关文章

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

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

  2. 2019牛客多校第四场C-sequence(单调栈+线段树)

    sequence 题目传送门 解题思路 用单调栈求出每个a[i]作为最小值的最大范围.对于每个a[i],我们都要乘以一个以a[i]为区间内最小值的对应的b的区间和s,如果a[i] > 0,则s要 ...

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

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

  4. 牛客多校第四场sequence C (线段树+单调栈)

    牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...

  5. 2019牛客多校第七场E Find the median 权值线段树+离散化

    Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...

  6. 2019牛客多校第七场H Pair 数位DP

    题意:给你一个3个数A, B, C问有多少对pair(i, j),1 <= i <= A, 1 <= j <= B, i AND j > C或 i XOR j < ...

  7. 2019牛客多校第七场E Find the median 离散化+线段树维护区间段

    Find the median 题意 刚开始集合为空,有n次操作,每次操作往集合里面插入[L[i],R[i]]的值,问每次操作后中位数是多少 分析 由于n比较大,并且数可以达到1e9,我们无法通过权值 ...

  8. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

  9. 2019年牛客多校第四场 B题xor(线段树+线性基交)

    题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...

随机推荐

  1. mysqldump导出数据出现问题

    利用mysqldump导出数据时提示warning,A partial dump from a server that has GTIDsubt@ubt-All-Series:~$  mysqldum ...

  2. spring boot 尚桂谷学习笔记10 数据访问02 mybatis

    数据访问 mybatis 创建一个 springboot 工程,模块选择 sql 中 mysql(数据驱动), jdbc(自动配置数据源), mybatis Web模块中选择 web pom 引入: ...

  3. C++怎样通过嵌入汇编写一个函数

    参考:http://msdn.microsoft.com/en-us/library/h5w10wxs.aspx 普通的函数,Compiler会自动生成prologue和epilogue,但是通过在函 ...

  4. USACO 6.3 章节 你对搜索和剪枝一无所知QAQ

    emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...

  5. 使用matplotlib画出log的图像

    以下内容是学习笔记,若有侵权,立即删除! import math import matplotlib.pyplot as plt import numpy as np if __name__ == ' ...

  6. PAT甲级【2019年9月考题】——A1162 MergingLinkedLists【25】

    7-2 Merging Linked Lists (25 分) Given two singly linked lists L 1 =a 1 →a 2 →...→a n−1 →a n  L1=a1→a ...

  7. redux请求数据流程

    redux请求数据流程 store里面的index.js文件 import {createStore,combineReducers,applyMiddleware} from "redux ...

  8. grep与egrep命令

    greo -E 等同于 egrep grep常用两种方式 1.   grep -c python a.txt 2.   cat a.txt | grep -c python a.txt文件如下 i l ...

  9. jq实现两个input输入同时不为空时,改变确认框背景颜色

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  10. Springboot2.x整合Redis(一)

    备注: springboto整合redis依赖于spring-boot-starter-data-redis这个jar 一,项目环境和依赖 1.POM.xml配置 <parent> < ...