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. web@h,c小总结

    问题0:元素内联元素,行内元素,行内块元素.        内联: 宽高M,P都有效        行内元素:无宽高,内容撑开,M,P左右有效        行内块元素:可设宽高,内容撑开,M,P都有 ...

  2. css效果文字多了就...

    开发中经常会遇见这样的问题,一段文字或者一段标题过长了,就让超出长度的部分益...替换.具体怎么做的呢?直接上代码: <style> *{ margin: 0; padding: 0; } ...

  3. .NET基础之构造函数

    1.构造函数: 分为实例构造函数.静态构造函数.私有构造函数. 使用new表达式创建某个类的对象时, 1.1实例构造函数: (1)构造函数的名字与类名相同: (2)使用new表达式创建类的对象或者结构 ...

  4. python 初识函数

    1.什么是函数 函数就是对一个代码块 或者功能的封装,什么时候用,什么时候执行 语法: def  函数名():            函数体(代码块) 函数的调用 函数名()          #所有 ...

  5. Jmeter下载安装配置及使用(windows)

    1 前言 仅作为记录使用. 2 步骤 2.1 下载地址:http://jmeter.apache.org/download_jmeter.cgi 2.2 选择binary版本即可:apache-jme ...

  6. maven项目部署到Tomcat

    1.安装Tomcat,地址:http://tomcat.apache.org,我安装的版本为9.0.0.M22,安装的目录为C:\apache-tomcat,设置环境变量CATALINA_HOME为C ...

  7. pip的常用命令

    前言 pip作为Python的御用包管理工具有着强大的功能,但是许多命令需要我们使用的时候借助搜索引擎查找(尤其是我), 于是我想将我使用到的命令整合下来,以后不用麻烦去找了,也希望能给你带来帮助.文 ...

  8. python-序列化模块

    本节内容 前言 json模块 pickle模块 shelve模块 总结 一.前言 1. 现实需求 每种编程语言都有各自的数据类型,其中面向对象的编程语言还允许开发者自定义数据类型(如:自定义类),Py ...

  9. Confluence 6 "Duplicate Key" 相关问题解决

    如果你遇到了下面的错误信息,例如: could not insert: [bucket.user.propertyset.BucketPropertySetItem#bucket.user.prope ...

  10. Confluence 6 恢复一个站点有关使用站点导出为备份的说明

    推荐使用生产备份策略.我们推荐你针对你的生产环境中使用的 Confluence 参考 Production Backup Strategy 页面中的内容进行备份和恢复(这个需要你备份你的数据库和 ho ...