Cardboard Box

贪了个半天贪不对, 我发现我根本就不会贪心。

我们先按b排序, 然后枚举选两颗心的b的最大值, 在这个之前的肯定都要选一个, 因为前面的要是一个都没选的话,

你可以把当前选两颗心的替换成前面选两颗心, 然后用平衡树或者线段树维护一下前k大和就好啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 3e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); #define lson l, mid, rt->ls
#define rson mid + 1, r, rt->rs
struct Node {
Node() {
ls = rs = NULL;
sum = cnt = ;
}
Node *ls, *rs;
LL sum;
int cnt;
}; inline void pull(Node* rt) {
rt->cnt = rt->ls->cnt + rt->rs->cnt;
rt->sum = rt->ls->sum + rt->rs->sum;
if(!rt->ls->cnt) delete rt->ls, rt->ls = NULL;
if(!rt->rs->cnt) delete rt->rs, rt->rs = NULL;
}
inline void push(Node* rt) {
if(!rt->ls) rt->ls = new Node();
if(!rt->rs) rt->rs = new Node();
}
void update(int p, int c, int l, int r, Node* rt) {
if(l == r) {
rt->cnt += c;
rt->sum += 1LL * p * c;
return;
}
push(rt);
int mid = l + r >> ;
if(p <= mid) update(p, c, lson);
else update(p, c, rson);
pull(rt);
}
LL query(int k, int l, int r, Node* rt) {
if(!k) return ;
if(rt->cnt <= k) return rt->sum;
if(l == r) return 1LL * k * l;
push(rt);
int mid = l + r >> ;
if(rt->ls->cnt >= k) return query(k, lson);
else return query(rt->ls->cnt, lson) + query(k - rt->ls->cnt, rson);
} int n, w, a[N], b[N], id[N], fin[N];
LL ans = INF; bool cmpa(int x, int y) {
return a[x] < a[y];
}
bool cmpb(int x, int y) {
return b[x] < b[y];
}
int main() {
Node* Rt = new Node();
scanf("%d%d", &n, &w);
for(int i = ; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
for(int i = ; i <= n; i++) id[i] = i;
int where = -;
sort(id + , id + + n, cmpa);
if(w <= n) {
LL ret = ;
for(int i = ; i <= w; i++) ret += a[id[i]];
ans = min(ans, ret);
where = ;
}
sort(id + , id + + n, cmpb);
for(int i = ; i <= n; i++) update(a[id[i]], , , inf, Rt);
LL prefix = ;
LL ret = ;
for(int i = ; i <= n; i++) {
int x = id[i];
update(a[x], -, , inf, Rt);
ret = prefix + b[x];
int need = w - (i + );
if(Rt->cnt >= need) {
ret += query(need, , inf, Rt);
if(ret < ans) {
ans = ret;
where = i;
}
}
prefix += a[x];
update(b[x] - a[x], , , inf, Rt);
}
if(!where) {
sort(id + , id + + n, cmpa);
for(int i = ; i <= w; i++) fin[id[i]] = ;
} else {
priority_queue<PII, vector<PII>, greater<PII> > que;
fin[id[where]] = ;
w -= where + ;
for(int i = ; i < where; i++) que.push(mk(b[id[i]] - a[id[i]], i)), fin[id[i]] = ;
for(int i = where + ; i <= n; i++) que.push(mk(a[id[i]], i));
while(w--) {
int who = que.top().se;
que.pop();
if(who < where) fin[id[who]] = ;
else fin[id[who]] = ;
}
}
printf("%lld\n", ans);
for(int i = ; i <= n; i++) printf("%d", fin[i]);
puts("");
return ;
} /*
*/

Codeforces 436E Cardboard Box (看题解)的更多相关文章

  1. Codeforces 436E - Cardboard Box(贪心/反悔贪心/数据结构)

    题面传送门 题意: 有 \(n\) 个关卡,第 \(i\) 个关卡玩到 \(1\) 颗星需要花 \(a_i\) 的时间,玩到 \(2\) 颗星需要 \(b_i\) 的时间.(\(a_i<b_i\ ...

  2. Codeforces 269C Flawed Flow (看题解)

    我好菜啊啊啊.. 循环以下操作 1.从队列中取出一个顶点, 把哪些没有用过的边全部用当前方向. 2.看有没有点的入度和 == 出度和, 如果有将当前的点加入队列. 现在有一个问题就是, 有没有可能队列 ...

  3. Codeforces 1045C Hyperspace Highways (看题解) 圆方树

    学了一下圆方树, 好神奇的东西呀. #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...

  4. Codeforces 1137D Cooperative Game (看题解)

    Cooperative Game 智商题, 感觉不太能推出来, 虽然看看证明过程是对的. #include<bits/stdc++.h> #define LL long long #def ...

  5. Codeforces 875F Royal Questions (看题解)

    我还以为是什么板子题呢... 我们把儿子当做点, 公主当做边, 然后就是求边权值最大基环树森林. #include<bits/stdc++.h> #define LL long long ...

  6. Codeforces 983C Elevator dp (看题解)

    Elevator 怎么今天写啥题都不会写啊, 我是傻了吗.. 把电梯里面四个人的目标点当作状态, 然后暴力转移. #include<bits/stdc++.h> #define LL lo ...

  7. Codeforces 924D Contact ATC (看题解)

    Contact ATC 我跑去列方程, 然后就gg了... 我们计每个飞机最早到达时间为L[ i ], 最晚到达时间为R[ i ], 对于面对面飞行的一对飞机, 只要他们的时间有交集则必定满足条件. ...

  8. Codeforces 830C Bamboo Partition (看题解)

    Bamboo Partition 列公式, 整除分块, 想不到, 好菜啊. #include<bits/stdc++.h> #define LL long long #define fi ...

  9. 题解-CF436E Cardboard Box

    题面 CF436E Cardboard Box \(n\) 个关卡,对每个关卡可以花 \(a_i\) 时间得到 \(1\) 颗星,或花 \(b_i\) 时间得到 \(2\) 颗星,或不玩.问获得 \( ...

随机推荐

  1. Qt5.10.1 在windows下vs2017静态编译

    1.在计算机上安装python库和perl库(因为后续的静态编译需要用到这两种语言),可以在命令行敲击“python”和“perl -v”检查是否安装成功. 2.修改msvc-desktop.conf ...

  2. centos中释放缓存的方法

    释放缓存区内存的方法 a)清理pagecache(页面缓存) # > /proc/sys/vm/drop_caches 或者 # sysctl - b)清理dentries(目录缓存)和inod ...

  3. 缓存系列之四:redis持久化与redis主从复制

    一:redis 虽然是一个内存级别的缓存程序,即redis 是使用内存进行数据的缓存的,但是其可以将内存的数据按照一定的策略保存到硬盘上,从而实现数据持久保存的目的,redis支持两种不同方式的数据持 ...

  4. hue报错StructuredException: timed out (code THRIFTSOCKET): None的处理

    通过hue的web界面进行hive的sql查询,无法显示结果并报错timeout 报错如下:[28/Jul/2017 11:23:29 +0800] decorators ERROR error ru ...

  5. JGroups

    JGroups Developer(s) Bela Ban Stable release 4.0.10.Final / February 1, 2018 Written in Java Operati ...

  6. CSS 三角形与圆形

    1. 概述 1.1 说明 通过边框(border)的宽度与边框圆角(border-radius)来设置所需的三角形与圆形. 1.2 边框 宽高都为0时,边框设置的不同结果也不同,如下: 1.四个边框都 ...

  7. Go语言环境安装&搭建(Linux)

    Linux的东西果然不记不行啊~ 下载&安装 下载 我们先找到linux版的下载链接 https://golang.org/dl/ 打开网址找到Linux对应的链接右键复制下载地址 然后连接服 ...

  8. log4net使用的两种方式

    1.首先添加log4net.dll引用(可以使用  管理NuGet程序包添加引用,也可以下载下来手动去添加引用) 2.在app.config文件中配置 3.log4net使用的2终方式 log4net ...

  9. ORA-00845 MEMORY_TARGET not supported on this system 的解决

    本文来源:宁静致远 的<ORA-00845 MEMORY_TARGET not supported on this system 的解决> oracle11g数据库在执行dbca或者调整s ...

  10. Confluence 6 为搜索引擎隐藏外部链接

    为搜索引擎隐藏外部链接能够避免向你的站点添加垃圾信息.如果你启用了这个选项的话,任何插入到页面中的 URLs 和评论将会赋予 'nofollow' 属性,这个属性将会禁止搜索引擎进行索引.  快捷链接 ...