链接:https://ac.nowcoder.com/acm/contest/887/C
来源:牛客网

Governing sand

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

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

题解:按照高度从低到高进行操作,对于之前操作的树,建一个权值线段树维护下在每个C下的数量和花费,然后对于当前高度计算当前高度下的树的数量m,然后线段树查询下m-1个权值最大的树的权值和。因为这个C只有200,暴力也能过,如果C很大就只能线段树了,如果有1e9那还需要离散化

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct node {int l, r;ll num, sum;
}tree[210 * 4];
struct node1 {int h, c, p;bool operator <(const node1 &b) const {return h < b.h;}
}a[N];
int n;
void build(int l, int r, int cur) {tree[cur].l = l;tree[cur].r = r;tree[cur].num = tree[cur].sum = 0;if(l == r) return;int mid = (r + l) >> 1;build(l, mid , cur << 1);build(mid + 1, r, cur << 1 | 1);
}
ll query(int cur, ll k) {if(k == 0) return 0;if(tree[cur].num <= k) {return tree[cur].sum;}if(tree[cur].l == tree[cur].r) {return min(k, tree[cur].num) * tree[cur].l;}ll res = 0;if(tree[cur << 1 | 1].num >= k) res = query(cur << 1 | 1, k);else res = tree[cur << 1 | 1].sum + query(cur << 1, k - tree[cur << 1 | 1].num);return res;
}
void update(int pos, int num, int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].num += num;tree[cur].sum = 1LL * tree[cur].l * tree[cur].num;return;}if(pos <= tree[cur << 1].r) update(pos, num, cur << 1);else update(pos, num, cur << 1 | 1);tree[cur].num = tree[cur << 1].num + tree[cur << 1 | 1].num;tree[cur].sum = tree[cur << 1].sum + tree[cur << 1 | 1].sum;
}
int main() {ll ans = 0, cnt, num;ll sum = 0;while(~scanf("%d", &n)) {sum = 0;for(int i = 1; i <= n; i++)  scanf("%d %d %d", &a[i].h, &a[i].c, &a[i].p), sum += 1LL * a[i].c * a[i].p;sort(a + 1, a + 1 + n);build(1, 200, 1);ans = sum; for(int i = 1, j; i <= n; ) {cnt = 0;num = 0;j = i;while(j <= n && a[j].h == a[i].h) {cnt += 1LL * a[j].c * a[j].p;num += a[j].p;j++;}cnt += query(1, num - 1);ans = min(ans, sum - cnt);j = i;while(j <= n && a[j].h == a[i].h) {//  cout << j << endl;update(a[j].c, a[j].p, 1);j++;}i = j;}printf("%lld\n", ans);}return 0;
}

2019 牛客多校 C Governing sand 线段树相关推荐

  1. 牛客多校6 - Josephus Transform(线段树求k-约瑟夫环+置换群的幂)

    题目链接:点击查看 题目大意:给出一个长度为 n 的排列,初始时为 1 , 2 , 3 ... n - 1 , n,现在有 m 次操作,每次操作表示为 ( k , x ) ,即进行 x 次 k-约瑟夫 ...

  2. 2021牛客多校4 - Tree Xor(线段树+异或区间拆分)

    题目链接:点击查看 题目大意:给出一棵 nnn 个点组成的树,每个点权的取值范围是 [li,ri][l_i,r_i][li​,ri​],每条边权代表的是两点的异或值,现在问这棵树有多少种有效赋值 题目 ...

  3. 牛客多校8 - All-Star Game(线段树分治+并查集按秩合并的撤销操作)

    题目链接:点击查看 题目大意:有 n 个球员和 m 个球迷,一个球员可能是多个球迷的粉丝,需要选择最少的球员进行比赛,使得所有的球迷都愿意观看(对于每个球迷来说,都有至少一个其喜欢的球员入选比赛) 对 ...

  4. 2019牛客多校第一场

    2019牛客多校第一场 题号 题目 知识点 A Monotonic Matrix B Symmetric Matrix C Fluorescent 2 D Two Graphs E Removal F ...

  5. 2019牛客多校训练第十场F Popping Balloons

    2019牛客多校训练第十场F Popping Balloons 题意:二维平面内给你若干个点,然后你可以在x轴和y轴分别射三枪(每一枪的间隔是R),问最多能射掉多少气球. 题解:贪心.这个应该只能算作 ...

  6. 2019牛客多校第九场AThe power of Fibonacci(广义BM)

    2019牛客多校第九场AThe power of Fibonacci(广义BM) 题目大意 求斐波那契数列m次方的前n项和 解题思路 显然,斐波那契的m次方前缀和依然是线性递推,因此考虑用exBM求解 ...

  7. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  8. 2019牛客多校第七场 C Governing sand

    因为当时时间不怎么够就没写... 其实就是一个模拟题而已下面注释很清楚 链接:https://ac.nowcoder.com/acm/contest/887/C 来源:牛客网 时间限制:C/C++ 3 ...

  9. Knapsack Cryptosystem(2019牛客多校折半查询)

    链接:https://ac.nowcoder.com/acm/contest/889/D 来源:牛客网 Amy asks Mr. B problem D. Please help Mr. B to s ...

最新文章

  1. ThinkPHP学习笔记之Model操作
  2. 7.1 pdo 宝塔面板php_宝塔面板配置阿里云服务器步骤和教程
  3. python为mysql设置id自增长_python mysql自增字段AUTO_INCREMENT值的修改方式
  4. java编码native2ascii下载_native2ascii.exe
  5. 如何制作和使用自签名证书
  6. 关于tcp/udp网络调试助手错误提示
  7. 创业者该如何给员工画饼、圆饼?
  8. 51单片机步进电机c语言程序,51单片机的步进电机c语言驱动程序
  9. AJAX和CGI 技术的应用
  10. python计算梯形面积_Python代码分享:面积计算器3.0代码
  11. 用flutter写一个抖音是什么体验?
  12. 数据结构之一元多项式
  13. MySQL45讲学习笔记(二)
  14. eve-ng 2.0.3-112懒人版安装、GNS3 2.2.32安装包、思科ASA8.42 9.42 路由器C3600 C7200、IOU镜像、思科IPS入侵防御系统
  15. 专题-参数方程与极坐标
  16. 传奇单机版批量修改爆率. 把所有物品爆率都改成1/10, 需要的话可以自己改更高....
  17. UI设计学习:Logo
  18. Hadoop的数据压缩
  19. 安卓古筝软件_新手如何自学乐器|零基础自学小提琴/吉他/电子琴/尤克里里/竖笛/电子鼓软件超详细测评+推荐...
  20. ul li列表样式css,列表ul li 专用样式

热门文章

  1. Java实现FTP批量大文件上传下载
  2. 人生成功的六匹马(转自喷嚏网的一篇品书)
  3. iOS入门(二十)字典
  4. 【0153】深入分析postgres接收libpq诸如insert、delete、update、select . . . 等命令的实现原理(1)
  5. mysql和sql server连接不上_php-通过不可靠的网络连接在MySQL和SQL Server...
  6. android网络图片搜索——基于百度图片搜索引擎
  7. API网关之Kong网关简介
  8. ERP系统个性化定制生产计划,简化排产流程
  9. c语言 任意自然数的分解加法,第三章 1. 代数系,自然数,整数,有理数,实数,复数...
  10. 计算机体系结构知识总结