E - Aquarium decoration

枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ;
const int Mod = 1e9 + ; int n, m, k, num, cnt, state[N], d[N], a[N], b[N], c[N];
LL suma[N], sumb[N], sumc[N]; struct node {
node* ch[];
int key, fix, sz, cnt;
LL sum;
void update() {
sz = ch[]->sz + ch[]->sz + cnt;
sum = ch[]->sum + ch[]->sum + 1ll*cnt*key;
}
}; typedef node* P_node; struct Treap {
node base[N], nil;
P_node root, null, len;
Treap() {
root = null = &nil;
null->key = null->fix = 1e9;
null->sz = null->cnt = ;
null->ch[] = null->ch[] = null;
len = base;
}
P_node newnode(int tkey) {
len->key = tkey;
len->fix = rand();
len->ch[] = len->ch[] = null;
len->sz = len->cnt = ;
len->sum = tkey;
return len++;
}
void rot(P_node &p, int d) {
P_node k = p->ch[d ^ ];
p->ch[d ^ ] = k->ch[d];
k->ch[d] = p;
p->update();
k->update();
p = k;
}
void _Insert(P_node &p, int tkey) {
if(p == null) {
p = newnode(tkey);
} else if(p->key == tkey) {
p->cnt++;
} else {
int d = tkey > p->key;
_Insert(p->ch[d], tkey);
if(p->ch[d]->fix > p->fix) {
rot(p, d ^ );
}
}
p->update();
} void _Delete(P_node &p, int tkey) {
if(p == null) return;
if(p->key == tkey) {
if(p->cnt > ) p->cnt--;
else if(p->ch[] == null) p = p->ch[];
else if(p->ch[] == null) p = p->ch[];
else {
int d = p->ch[]->fix > p->ch[]->fix;
rot(p, d);
_Delete(p->ch[d], tkey);
}
} else {
_Delete(p->ch[tkey > p->key], tkey);
}
p->update();
}
int _Kth(P_node p, int k) {
if(p == null || k < || k > p->sz) return ;
if(k < p->ch[]->sz + ) return _Kth(p->ch[], k);
if(k > p->ch[]->sz + p->cnt) return _Kth(p->ch[], k - p->ch[]->sz - p->cnt);
return p->key;
}
int _Rank(P_node p, int tkey, int res) {
if(p == null) return -;
if(p->key == tkey) return p->ch[]->sz + res + ;
if(tkey < p->key) return _Rank(p->ch[], tkey, res);
return _Rank(p->ch[], tkey, res + p->ch[]->sz + p->cnt);
}
int _Pred(P_node p, int tkey){
if(p == null) return -1e9;
if(tkey <= p->key) return _Pred(p->ch[], tkey);
return max(p->key, _Pred(p->ch[], tkey));
}
int _Succ(P_node p, int tkey){
if(p == null) return 1e9;
if(tkey >= p->key) return _Succ(p->ch[], tkey);
return min(p->key, _Succ(p->ch[], tkey));
}
LL _Query(P_node p, int res) {
if(!res) return ;
if(p->ch[]->sz >= res) return _Query(p->ch[], res);
else if(p->ch[]->sz + p->cnt < res) {
return p->ch[]->sum + 1ll*p->key*p->cnt + _Query(p->ch[], res - p->ch[]->sz - p->cnt);
} else {
return p->ch[]->sum + 1ll*p->key*(res - p->ch[]->sz);
}
}
void Insert(int tkey){ _Insert(root,tkey); }
void Delete(int tkey){ _Delete(root,tkey); }
int Kth(int k){ return _Kth(root,k); }
int Rank(int tkey){ return _Rank(root,tkey,); }
int Pred(int tkey){ return _Pred(root,tkey); }
int Succ(int tkey){ return _Succ(root,tkey); }
LL Query(int res){ return _Query(root,res); }
}tp; int main() {
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= n; i++) scanf("%d", &d[i]);
scanf("%d", &num);
for(int i = ; i <= num; i++) {
int x; scanf("%d", &x);
state[x] |= ;
}
scanf("%d", &num);
for(int i = ; i <= num; i++) {
int x; scanf("%d", &x);
state[x] |= ;
}
for(int i = ; i <= n; i++) {
if(state[i] == ) tp.Insert(d[i]), cnt++;
else if(state[i] == ) a[++a[]] = d[i];
else if(state[i] == ) b[++b[]] = d[i];
else c[++c[]] = d[i];
}
sort(a + , a + + a[]);
sort(b + , b + + b[]);
sort(c + , c + + c[]);
for(int i = ; i <= a[]; i++)
suma[i] = suma[i-] + a[i];
for(int i = ; i <= b[]; i++)
sumb[i] = sumb[i-] + b[i];
for(int i = ; i <= c[]; i++)
sumc[i] = sumc[i-] + c[i]; LL ans = INF;
for(int i = ; i <= c[]; i++) tp.Insert(c[i]), cnt++;
for(int i = ; i <= c[]; i++) {
if(i) tp.Delete(c[i]), cnt--;
int res1 = max(, k - i);
if(a[] < res1 || b[] < res1) continue;
while(a[] > res1) {
tp.Insert(a[a[]]);
a[]--; cnt++;
}
while(b[] > res1) {
tp.Insert(b[b[]]);
b[]--; cnt++;
}
if(i + * res1 > m) continue;
int res2 = m - i - * res1;
ans = min(ans, sumc[i] + suma[a[]] + sumb[b[]] + tp.Query(res2));
}
printf("%lld\n", ans == INF ? - : ans);
return ;
} /*
*/

Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树的更多相关文章

  1. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)

    A. Carrot Cakes time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  2. C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)

    题目链接:http://codeforces.com/contest/799/problem/C 题目: 题意: 给你n种喷泉的价格和漂亮值,这n种喷泉题目指定用钻石或现金支付(分别用D和C表示),C ...

  3. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】

    题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...

  4. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) 一夜回到小学生

    我从来没想过自己可以被支配的这么惨,大神讲这个场不容易掉分的啊 A. Carrot Cakes time limit per test 1 second memory limit per test 2 ...

  5. 【动态规划】【滚动数组】【搜索】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion

    显然将扩张按从大到小排序之后,只有不超过前34个有效. d[i][j]表示使用前i个扩张,当length为j时,所能得到的最大的width是多少. 然后用二重循环更新即可, d[i][j*A[i]]= ...

  6. 【预处理】【分类讨论】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

    分几种情况讨论: (1)仅用C或D买两个 ①买两个代价相同的(实际不同)(排个序) ②买两个代价不同的(因为买两个代价相同的情况已经考虑过了,所以此时对于同一个代价,只需要保存美丽度最高的喷泉即可)( ...

  7. 树状数组 Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

    C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion

    D. Field expansion time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #413, rated, Div. 1 + Div. 2 C. Fountains(贪心 or 树状数组)

    http://codeforces.com/contest/799/problem/C 题意: 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100  ...

随机推荐

  1. 使用前端组件化思想修改todolist

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. nginx配置详情(总结)

    Nginx简介 Nginx是一款开源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务 Nginx工作原理 Nginx由内核和模块组成,完成工作是通过查找配置文件 ...

  3. hdu1286 找新朋友

    找新朋友 http://acm.hdu.edu.cn/showproblem.php?pid=1286 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. Java并发编程原理与实战十五:手动实现一个可重入锁

     package com.roocon.thread.ta1; public class Sequence { private MyLock lock = new MyLock(); private ...

  5. 通过xshell/securecrt连接linux上传/下载文件

    通过ssh等客户端连接远程linux总会有上传下载的需求,这里分别用Ubuntu和centos展示安装lrzsz软件的命令,使用命令是一致的,这里简单写 1.安装: centos:(注:参数-y中的意 ...

  6. 20155302 2016-2017-2 《Java程序设计》第七周学习总结

    20155302 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 Lambda表达式的优点:更加紧凑的代码.修改方法的能力.更好地支持多核处理 "L ...

  7. 修复TortoiseGit文件夹和文件图标不显示问题的多种解决办法以及重启之后TortoiseGit图标注册表又不见了的解决办法

    一 首先进行第一种尝试 打开 regedit.exe ,准备修改注册表 找到 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\ ...

  8. 读sru代码

    1. def read_corpus(path, eos="</s>"): data = [ ] with open(path) as fin: for line in ...

  9. 一个简单的java jdbc案例

    有些时候,配置一个spring+mybatis框架,然后写xml,dao ,service显得特别繁琐. 如果我们只是想查一下数据库,不考虑连接复用也不考虑动态sql,可以用原生的jdbc来实现,方便 ...

  10. Python基础之多线程事件Event

    import threading,time class Boss(threading.Thread): def run(self): print("BOSS:伙计们今晚上加班到22:00&q ...