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 ...
随机推荐
- CENTOS服务器基础教程-U盘系统盘制作
什么都要用到一点点,会一点点,现在的USB3.0基本上服务器都已经支持.小编给大家介绍基础篇:如何使用U盘制作系统安装盘 工具/原料 U盘 UltraISO工具 方法/步骤 准备一个U ...
- MySQL数据库笔记六:数据定义语言及数据库的备份和修复
1. MySQL中的函数 <1>加密函数 password(str) 该函数可以对字符串str进行加密,一般情况下,此函数给用户密码加密. select PASSWORD('tlxy666 ...
- window下 局域网内使用mysql,mysql 开启远程访问权限
一.window 10 开启3306端口 1.按win键选择设置 2.搜索防火墙 3.选择高级设置 3.右键入站规则->新建规则 4.按照提示,规则类型选择端口,应用于tcp,特定本地端口输入3 ...
- Bootstrap如何禁止响应式布局
Bootstrap 会自动帮你针对不同的屏幕尺寸调整你的页面,使其在各个尺寸的屏幕上表现良好.下面我们列出了如何禁用这一特性,就像这个非响应式布局实例页面一样. 禁止响应式布局有如下几步: 移除 此 ...
- Nginx 实用配置
1 防盗链 相关配置: valid_referers location ~* \.(gif|jpg|png)$ { # 只允许 192.168.0.1 请求资源 valid_referers none ...
- 第8章 浏览器对象模型BOM 8.2 location对象
location 是最有用的 BOM对象之一,它提供了与当前窗口中加载的文档有关的信息,还提供了一些导航功能.事实上, location 对象是很特别的一个对象,因为它既是 window 对象的属性, ...
- CodeForces 758 C Unfair Poll
Unfair Poll 题意:一共有n排同学每排同学有m个人, 老师问问题有一个顺序, 先从第一排开始问,问完第一排的所有同学之后,再问第2排的,对于所有排的访问顺序为 1,2,3……n-1,n,n- ...
- CF937B Vile Grasshoppers
Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- briup_jdbc自建工具类终极版
总结:此次构建工具类,难点在于查询,所需要的功能是 不管是 oracle还是mysql 都可以连接,并且 提供所需要的实体类,都可以将查询内容封装到实体类中去 遇到的难点 连接时,是从prppert ...
- Python字符串中删除特定字符
分析 在Python中,字符串是不可变的.所以无法直接删除字符串之间的特定字符. 所以想对字符串中字符进行操作的时候,需要将字符串转变为列表,列表是可变的,这样就可以实现对字符串中特定字符的操作. 1 ...