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
ans=55
suf=40 res=0
ans=40
suf=30 res=15
ans=40
suf=25 res=25
ans=40
suf=0 res=30
ans=30
30
第四次询问为什么是25?明明要砍完前面所有的9棵树的。擦,curnum加的怎么是c。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2005;
int ch[MAXN][2];
int val[MAXN], dat[MAXN];
ll siz[MAXN], cnt[MAXN];
ll sum[MAXN];
int tot, root;
inline void Init() {
tot = 0;
root = 0;
}
inline int NewNode(int v, ll num) {
ch[++tot][0] = ch[tot][1] = 0;
val[tot] = v, dat[tot] = rand();
siz[tot] = cnt[tot] = num;
sum[tot] = 1ll * val[tot] * cnt[tot];
return tot;
}
inline void PushUp(int id) {
siz[id] = siz[ch[id][0]] + siz[ch[id][1]] + cnt[id];
sum[id] = sum[ch[id][0]] + sum[ch[id][1]] + 1ll * val[id] * cnt[id];
}
inline void Rotate(int &id, int d) {
int temp = ch[id][d ^ 1];
ch[id][d ^ 1] = ch[temp][d];
ch[temp][d] = id;
id = temp;
PushUp(ch[id][d]), PushUp(id);
}
inline void Insert(int &id, int v, ll num) {
if(!id)
id = NewNode(v, num);
else {
if(v == val[id])
cnt[id] += num;
else {
int d = v < val[id] ? 0 : 1;
Insert(ch[id][d], v, num);
if(dat[id] < dat[ch[id][d]])
Rotate(id, d ^ 1);
}
PushUp(id);
}
}
ll GetSum(int id, ll k) {
if(!id||k<=0)
return 0;
else {
if(k >= siz[ch[id][0]] + cnt[id])
return GetSum(ch[id][1], k - siz[ch[id][0]] - cnt[id]) + sum[ch[id][0]] + 1ll * val[id] * cnt[id];
else if(k >= siz[ch[id][0]])
return sum[ch[id][0]] + 1ll * val[id] * (k - siz[ch[id][0]]);
else
return GetSum(ch[id][0], k);
}
}
int n;
struct Tree {
int h, c, p;
bool operator<(const Tree& t)const {
return h < t.h;
}
} tr[100005];
const ll INF = 1e18;
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
while(~scanf("%d", &n)) {
Init();
for(int i = 1; i <= n; ++i)
scanf("%d%d%d", &tr[i].h, &tr[i].c, &tr[i].p);
sort(tr + 1, tr + 1 + n);
ll sufcost = 0;
for(int i = 1; i <= n; ++i)
sufcost += 1ll * tr[i].c * tr[i].p;
ll ans = INF;
for(int i = 1, nxt; i <= n; i = nxt) {
ll curnum = 0;
for(nxt = i + 1; nxt <= n && tr[nxt].h == tr[i].h; ++nxt);
for(int j = i; j < nxt; ++j) {
curnum += tr[j].p;
sufcost -= 1ll * tr[j].c * tr[j].p;
}
ll res=GetSum(root, siz[root] - curnum + 1);
ans = min(ans, sufcost + res);
for(int j = i; j < nxt; ++j)
Insert(root, tr[j].c, tr[j].p);
}
printf("%lld\n", ans);
}
}
2019牛客暑期多校训练营(第七场) - C - Governing sand - 平衡树的更多相关文章
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第二场)J-Subarray(思维)
>传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
- 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)
题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...
随机推荐
- 7.docker私有registry
一.Docker Registry分类 Registry用于保存docker镜像,包括镜像的层次结构和元数据.都是基于https或者http工作的. 用户可自建Registry,也可使用官方的Dock ...
- 进行移动端rem适配
(function (designWidth, maxWidth) { var doc = document, win = window; var docEl = doc.documentElemen ...
- 自己动手实现一个网络模型 python
转自:https://www.jianshu.com/p/4b30e1dd2252 common_funcs.py import numpy as np import matplotlib.pyplo ...
- Python常用框架
序言 所谓专家,就是在一个很小的领域里把所有错误都犯过了的人--尼尔斯·玻尔 Django Flask Tornado 适合后端微服务 资料 flask
- spring util包 StringUtils工具类中的isEmpty() 方法解析
今天在公司看到同事写的代码,无意发现在判断字符串类型时,使用的是StringUtils工具类中的isEmpty()去判断如下所示 @RequestMapping(value = "/pub/ ...
- 大数据笔记(十二)——使用MRUnit进行单元测试
package demo.wc; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.io.IntW ...
- Linux 用户必须知道的 14 个常用 Linux 终端快捷键
简介:以下是一些每个 Linux 用户必须使用的键盘快捷键. 使用命令行时,这些 Linux 快捷键将提升你的工作效率和效率. 你知道什么把专业用户和普通用户分开的吗?掌握键盘快捷键. 好的!这虽不是 ...
- sqlite时间类型
SQLite分页显示:Select * From news order by id desc Limit 10 Offset 10这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么 ...
- 非web工程,打jar放shell执行
作为6年经验的程序员,一直在搞web服务应用开发,今天领导被吐槽了,只会web方面的东东,最基本的打包啥啥都不会.. 一般开发工程都是web项目,突然要求开发非web,不用tomcat装(浪费端口号) ...
- 004-windows(64位)下使用curl命令
一.下载工具包:http://curl.haxx.se/download.html 二.使用 使用方式一:在curl.exe目录中使用 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的 ...