gym102201E_Eat Economically
题意
给\(2n\)个物品,分别有\(a,b\)属性,对于\(i=1...n\),选择\(i\)个\(a\)属性和\(i\)个\(b\)属性,且每个物品只能作为一种属性的贡献,求最小的值。
分析
- 看了题解补了两天... 应该叫做可反悔的贪心,或者其实就是网络流?不过因为是特殊的图,所以可以用优先队列来优化。
- 维护四个优先队列,分别是未使用的\(a\)属性,未使用的\(b\)属性,已使用的\(a\)属性转化为\(b\)属性的花费,已使用的\(b\)属性转化为\(a\)属性的花费。
- 对一般情况,每次取出最小\(a\)属性,取出最小\(b\)属性转化为\(a\)属性的花费,以及最小的\(b\)属性,判断哪种策略更优,对\(b\)属性同理。
- 很多细节需要注意
- 相同属性值,选择后反悔成另一种属性的花费更小的优先(无需绝对值)。比如(3,2)比(3,4)优先。
- 两种策略的花费相同时,选择直接取出的策略,因为另一种策略需要从另一种最小堆中取出一个元素,使得堆顶元素变大。
- 在前两个最小堆中取元素时需要判断取出元素是否已访问过。
- 进行反悔的元素需要将花费取反然后放到另一个反悔队列里面去。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+50;
struct node{
int id;
ll val,sec;
bool operator <(const node& rhs)const{
if(val!=rhs.val){
return val>rhs.val;
}else{
return sec-val>rhs.sec-rhs.val;
}
}
};
priority_queue<node> lc,dn,ltd,dtl;
int n,vis[N];
ll l[N],d[N];
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=1;i<=2*n;i++){
scanf("%lld%lld",&l[i],&d[i]);
lc.push(node{i,l[i],d[i]});
dn.push(node{i,d[i],l[i]});
}
ll ans=0;
for(int i=1;i<=n;i++){
while(!lc.empty() && vis[lc.top().id]){
lc.pop();
}
if(lc.empty()){
auto t=dtl.top();
dtl.pop();
ans+=t.val;
auto g=dn.top();
dn.pop();
ans+=g.val;
ltd.push(node{t.id,d[t.id]-l[t.id]});
dtl.push(node{g.id,l[g.id]-d[g.id],0});
}else{
auto a=lc.top();
while(vis[a.id]){
lc.pop();
a=lc.top();
}
while(!dn.empty() && vis[dn.top().id]){
dn.pop();
}
if(dtl.empty() || dn.empty()){
ans+=a.val;
lc.pop();
vis[a.id]=1;
ltd.push(node{a.id,d[a.id]-l[a.id],0});
}else{
auto b=dtl.top();
auto c=dn.top();
if(a.val<=b.val+c.val){
ans+=a.val;
lc.pop();
vis[a.id]=1;
ltd.push(node{a.id,d[a.id]-l[a.id],0});
}else{
ans+=b.val+c.val;
dtl.pop();
dn.pop();
vis[c.id]=1;
ltd.push(node{b.id,d[b.id]-l[b.id],0});
dtl.push(node{c.id,l[c.id]-d[c.id],0});
}
}
}
while(!dn.empty() && vis[dn.top().id]){
dn.pop();
}
if(dn.empty()){
auto t=ltd.top();
ltd.pop();
ans+=t.val;
auto g=lc.top();
lc.pop();
ans+=g.val;
dtl.push(node{t.id,l[t.id]-d[t.id],0});
ltd.push(node{g.id,d[g.id]-l[g.id],0});
}else{
auto a=dn.top();
while(vis[a.id]){
dn.pop();
a=dn.top();
}
while(!lc.empty() && vis[lc.top().id]){
lc.pop();
}
if(ltd.empty() || lc.empty()){
ans+=a.val;
dn.pop();
vis[a.id]=1;
dtl.push(node{a.id,l[a.id]-d[a.id],0});
}else{
auto b=ltd.top();
auto c=lc.top();
if(a.val<=b.val+c.val){
ans+=a.val;
dn.pop();
vis[a.id]=1;
dtl.push(node{a.id,l[a.id]-d[a.id],0});
}else{
ans+=b.val+c.val;
ltd.pop();
lc.pop();
vis[c.id]=1;
dtl.push(node{b.id,l[b.id]-d[b.id],0});
ltd.push(node{c.id,d[c.id]-l[c.id],0});
}
}
}
printf("%lld\n",ans);
}
return 0;
}
gym102201E_Eat Economically的更多相关文章
- Why Countries Succeed and Fail Economically
Countries Succeed and Fail Economically(第一部分)" title="Why Countries Succeed and Fail Econo ...
- Doherty Threshold
Prior to the publication of the IBM technical paper behind what commonly known today as the Doherty ...
- Manifesto of the Communist Party
A spectre is haunting Europe – the spectre of communism. All the powers of old Europe have entered i ...
- Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)
ABSTRACT Recent technological advancement have led to a deluge of data from distinctive domains (e.g ...
- 摘要评注The Cathedral & The Bazaar
2013年暑期买到这本书,距离其第一版已经有14年之久,而最早发布在互联网上的文章更是早在1997年.在我阅读的时候,很多事迹已经沉积为历史,很多预言已经成为现实.而这本书的意义却丝毫没有因此淡化,反 ...
- English substitute
英语写作中替换掉用到发腻的↓常用词↓,吐血整理2小时~~ 动词替换: 1.Improve 提高: Promote: 促进AC之间的贸易 promote the trade between ...
- How to Write Doc Comments for the Javadoc Tool
http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...
- 越狱Season 1-Episode 6: Riots, Drills and the Devil: Part 1
Season 1, Episode 6: Riots, Drills and the Devil: Part 1 - Diamond: Just a few more rides. 就再多玩几次吧 O ...
- Regionals 2013 :: North America - Southeast USA
Regionals 2013 :: North America - Southeast USA It Takes a Village As a Sociologist, you are studyin ...
随机推荐
- cf 1102 B
题意:求字符串中任意相邻两位是否可以可以由前一个加上任意个x或y屏蔽十位与后一位相等,如果可以需要添加的最少数字是多少,x值为0-9,y值也为0-9,求出任意x,y对应情形下字符串需要添加的最少数字, ...
- UVA10831题解
Gerg's Cake Gerg is having a party, and he has invited his friends. p of them have arrived already, ...
- ionic 页面动画 ngAnimate
git 地址: https://github.com/Augus/ngAnimate/ 使用方法: 下载后, 1.引入ngAnimate.js(依赖jquery); 2.引入 ng-animate.c ...
- Spring 核心技术(7)
接上篇:Spring 核心技术(6) version 5.1.8.RELEASE 1.6 定制 Bean 的特性 Spring Framework 提供了许多可用于自定义 bean 特性的接口.本节将 ...
- 01 Python 基础数据类型
基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余 + - * / ** %...2.bool 布尔值 判断真假以及作为条件变量3.str 字符串 存 ...
- python接口自动化测试之requests库详解
前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...
- CF995B Suit and Tie 贪心 第十三
Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CSS特效集锦:视觉魔法的碰撞与融合(二)
引言 长久以来,我认识到.CSS,是存在极限的.正如曾经替你扛下一切的那个男人,也总有他眼含热泪地拼上一切,却也无法帮你做到的事情,他只能困窘地让你看到他的无能为力,怅然若失. 然后和曾经他成长的时代 ...
- 林大妈的JavaScript进阶知识(三):HTML5 History API
HTML5中新增了History API,它用于管理浏览器路由跳转的一个url栈.History是window对象的一部分,它也是一个对象,因此称它是BOM(类似DOM,Browser Object ...
- Go语言标准库之template
html/template包实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.它提供了和text/template包相同的接口,Go语言中输出HTML的场景都应使用text/templa ...