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. FreeSWITCH voicemail

    功能描述:分机不存在时,进行语音留言. 步骤: 1.编译mod_voicemail 模块.默认是已经有编译 2.加载mod_voicemail模块: fs_cli  -->  reload mo ...

  2. UML教程

    1.前言 1.1 前言   本资料对UML1.5各种模型图的构成和功能进行说明,通过本资料的学习达到可以读懂UML模型图的目的.本资料不涉及模型图作成的要点等相关知识. 1.2 UML概述 1.2.1 ...

  3. Windows平台下,Java性能分析工具VisualVM的Tomcat8的配置

    VisualVM在JDK6版本及以上已经自带这个应用. 位置:C:\Program Files (x86)\Java\jdk1.8.0_60\bin\jvisualvm.exe   在Windows环 ...

  4. linux流量异常查看哪些程序占用的

    Linux下进程/程序网络带宽占用情况查看工具 -- NetHogs   http://www.vpser.net/manage/nethogs.html   来自.  最后略有修改 之前VPS侦探曾 ...

  5. Android中播放音乐的几种方式

    前言 前几天一直在研究RxJava2,也写了记录了几篇博客,但因为工作任务原因,需要研究音频相关的知识,暂时放下Rxjava,本文的demo中,MediaPalyer 部分使用RxJava编写一点逻辑 ...

  6. 【进阶2-2期】JavaScript深入之从作用域链理解闭包(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记   https://github.com/yygmind/blog/issues/18 红宝书(p178)上对于闭包的定义:闭包是指有权访问另外一 ...

  7. 改造 Android 官方架构组件 ViewModel

    前言 Android 官方架构组件在今年 5 月份 Google I/O 大会上被公布, 直到 11 月份一直都是测试版, 由于工作比较繁忙, 期间我只是看过类似的文章, 但没有在实际项目中使用过, ...

  8. ( linker command failed with exit code 1) 错误解决方案 项目使用的是pod

    targets -> build settings -> architectures -> build active architecture only -> debug 改成 ...

  9. flask中的wtforms使用

    一.简单介绍flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装: pip3 install wtforms 二.简单使用wtfo ...

  10. Mycat配置文件详解及全局序列号

    来详细的看看 mycat的配置文件,更多信息请查看:mycat权威指南. schema.xml: Schema.xml 作为 MyCat 中重要的配置文件之一,管理着 MyCat 的逻辑库.表.分片规 ...