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 ...
随机推荐
- JAVA 获取时间段内的每一天
public class day { public static void main(String[] args) { // TODO Auto-generated method stub Strin ...
- HDU 6134
题意略. 思路: 我们先不考虑[(i , j) == 1],在此情况下,其实这个值是sum( [ (i , j) == 1,2,3,....,n ] ) 这些情况.我们要求的仅仅是其中的第一部分而已. ...
- 数据的查找和提取[2]——xpath解析库的使用
xpath解析库的使用 在上一节,我们介绍了正则表达式的使用,但是当我们提取数据的限制条件增多的时候,正则表达式会变的十分的复杂,出一丁点错就提取不出来东西了.但python已经为我们提供了许多用于解 ...
- 自制轮子 canvas tree
这是树的效果 这个树拥有的功能有 1.小地图显示 2.小地图点击快速定位 3.点击加减显示根节点 4.基本的数显示 5.节点拖拽 6.点击头像跳转到相关页面 7.使用直角线和直线选择 8.画布自适应 ...
- c++并查集配合STL MAP的实现(洛谷P2814题解)
不会并查集的话请将此文与我以前写的并查集一同食用. 原题来自洛谷 原题 文字稿在此: 题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. ...
- ccpc网赛 hdu6703 array(权值线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6703 大意:给一个n个元素的数组,其中所有元素都是不重复的[1,n]. 两种操作: 将pos位置元素+1e7 查 ...
- bzoj 2001 CITY 城市建设 cdq分治
题目传送门 题解: 对整个修改的区间进行分治.对于当前修改区间来说,我们对整幅图中将要修改的边权都先改成-inf,跑一遍最小生成树,然后对于一条树边并且他的权值不为-inf,那么这条边一定就是树边了. ...
- 基于注解的读取excel的工具包
easyexcel-wraper easyexcel-wraper是什么? 一个方便读取excel内容,且可以使用注解进行内容验证的包装工具 easyexcel-wraper有哪些功能? 在easye ...
- 【Offer】[3-2] 【不修改数组找出重复的数字】
题目描述 思路分析 Java代码 代码链接 题目描述 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的. 请找出数组中任意一个重复的数字,但不能修改输入的数组. ...
- 洛谷 P1059【明明的随机数】 题解
事实上,完全可以先将输入进来带有重复的元素们保存进一个数组并对该数组进行排序,再将该数组的各个元素逐个判断是否与前一元素相同(重复与否的判断),将不重复的元素转移至另一个数组,与此同时进行对不重复元素 ...