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 ...
随机推荐
- 读书分享全网学习资源大合集,推荐Python3标准库等五本书「02」
0.前言 在此之前,我已经为准备学习python的小白同学们准备了轻量级但超无敌的python开发利器之visio studio code使用入门系列.详见 1.PYTHON开发利器之VS Code使 ...
- Flutter学习笔记(25)--ListView实现上拉刷新下拉加载
如需转载,请注明出处:Flutter学习笔记(25)--ListView实现上拉刷新下拉加载 前面我们有写过ListView的使用:Flutter学习笔记(12)--列表组件,当列表的数据非常多时,需 ...
- unity_小功能实现(客户端相互通信功能)
服务器端:在VS中新建项目,用于服务器的搭建 using System;using System.Collections.Generic; using System.Net.Sockets;u ...
- 09_if条件语句的使用
1.条件语句是用来判断给定的条件是否满足(表达式值是否为0),并根据判断的结果(真或假)决定执行的语句2.go语言和C语言的条件语句在语法格式上大体相似,但是还是有略微的不同,具体的细节如下 例子:p ...
- UVA P12101 【Prime Path】
题库 :UVA 题号 :12101 题目 :Prime Path link :https://www.luogu.org/problemnew/show/UVA12101
- Java连载26-方法(语法结构)
一.方法 1.返回值类型如果不是void,表示这个方法执行结束之后必须返回一个具体的数值,当方法执行结束的时候没有返回任何数值,编译器会报错,怎么返回值呢?并且要求“值”的数据类型必须和“方法的返回值 ...
- CF EDU 1101D GCD Counting 树形DP + 质因子分解
CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...
- SDU暑期集训排位(8)
A. A Giveaway 签到 B. Game of XOR 做法 dp[G][L][R]表示在倒数第G代,左边的数是L,右边的数是R,下面共有多少个0和1 区间和转换成两次前缀和和一次单点查询 利 ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- poj 2828--Buy Tickets(线段树)
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...