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$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...
随机推荐
- 4.Pod控制器
Controller-manager: Kube-controller-manager Cloud-controller-manager:在K8S上启用CloudProvider的时候才需要,用来配合 ...
- Python---面向对象---修学校
一.创建北京和成都两个校区: 1.创建Linux\Python两个课程 2.创建北京校区的Python 3期课程和成都校区的Linux 1期课程 3.管理员创建了北京校区的学员小张,并将其分配在了Py ...
- LTM_本地流量管理(二)
会话保持 首先要熟悉两个概念:连接connect和会话session 连接:在四层负载均衡中,连接是最小元素. l 源端口:客户端随机产生的端口. l 源地址:发起请求的源IP地址. l 目的端 ...
- iOS 指定位置切圆角不生效问题
如果是在VC中操作,需要在viewDidLayoutSubviews方法里 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; ...
- MySQL两个时间相减
SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); interval可是: SECOND 秒 SECONDS MINUTE 分钟 MINUTE ...
- 安装k8s集群(亲测)
先安装一台虚拟机,然后进行克隆,因为前面的步骤都是一样的,具体代码如下: Last login: Mon Nov 25 00:40:34 2019 from 192.168.180.1 ##安装依赖包 ...
- Actor Roles 图示
Udemy上的教程<Unreal Multiplayer Mastery - Online Game Development in C++>中对Actor Roles的总结非常直观到位,一 ...
- [ethereum源码分析](1) dubug环境搭建
前言 因为最近云小哥哥换了一份工作,新公司比较忙,所以一直没有更新新的博客.云小哥哥新的公司是做区块链的,最近在学习区块链相关的东西(也算是乘坐上了区块链这艘大船).本博客是记录我搭建ethereum ...
- python中的实例属性和类属性
在python中,类属性和实例属性的区别是什么? 我认为是作用域的不同,实例对象可以访问类属性,类对象不可以访问实例属性.(类的概念本身就是作用域的概念,你不能让一只猫会飞,猫属于猫类,这一类都不会飞 ...
- codeforces 722F - Cyclic Cipher
题目链接:http://codeforces.com/problemset/problem/722/F ------------------------------------------------ ...