【贪心】codeforces B. Heidi and Library (medium)
http://codeforces.com/contest/802/problem/B
【题意】
有一个图书馆,刚开始没有书,最多可容纳k本书;有n天,每天会有人借一本书,当天归还;如果图书馆有这个本就直接借到,否则图书馆的人会购买这本书,每本书的价格都是1;如果现在图书馆的书已达上限还需购买,必须舍弃已有的一本书,以后再有人借这本书要重新购买。
问图书馆的人最少要花多少钱购书?
数据范围变成了4000 00.
【思路】
关键是替换原则,每次都替换下一次出现最晚的,因为它占用图书馆的时间最长。
【官方题解】
Here we need to be much more ecient. There are many data structures that one can use. One idea is to
store a queue of remaining requests for each possible book (initializing it at the beginning and removing
requests from the front as they arrive) and an std::set-like structure (based on a balanced binary search
tree; it is also possible to use a priority queue based on a heap) which stores books that are currently at the
library, sorted by their next-request time.
这里我用优先级队列找出每次应该替换哪一个,用队列记录每个图书馆中的元素出现位置的情况,不断更新pq和q。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=4e5+;
int a[maxn];
int b[maxn];
map<int,int> mp;
struct Node
{
int id;
int num;
Node(int iid,int nnum):id(iid),num(nnum){}
};
//根据id排序,优先级队列先pop的是排在后面的,也就是下一次出现最晚的
bool operator<(Node a,Node b)
{
return a.id<b.id;
}
//每个数都有一个出现位置的队列
queue<int> q[maxn];
priority_queue<Node> pq;
int vis[maxn];
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int ans=;
int cnt=;
int i;
for(i=;i<=n;i++)
{
if(cnt<m)
{
if(!vis[a[i]])
{
b[++cnt]=a[i];
vis[a[i]]=;
ans++;
}
}
else
{
break;
}
}
if(cnt<m)
{
printf("%d\n",ans);
continue;
}
//q[a[k]] 为a[k]从小到大出现位置的队列 ,要从i开始算,以前的都不用记
for(int k=i;k<=n;k++)
{
q[a[k]].push(k);
}
for(int k=;k<=cnt;k++)
{
if(!q[b[k]].empty())
{
pq.push(Node(q[b[k]].front(),b[k]));
}
else
{
//maxn代表后面不出现
pq.push(Node(maxn,b[k]));
}
//b[1,cnt]中记录的始终是当前图书馆的书,每个元素都不同,是一个一一映射
mp[b[k]]=k;
}
for(;i<=n;i++)
{
if(!q[a[i]].empty())
{
q[a[i]].pop();
}
if(!vis[a[i]])
{
ans++;
Node node=pq.top();
pq.pop();
//根据node.num映射到对应b中的位置
int id=mp[node.num];
vis[b[id]]=;
b[id]=a[i];
vis[a[i]]=;
//更新后的映射
mp[a[i]]=id;
}
//要放在后面,否则会影响前面的pop
if(!q[a[i]].empty())
{
pq.push(Node(q[a[i]].front(),a[i]));
}
else
{
pq.push(Node(maxn,a[i]));
}
}
printf("%d\n",ans);
}
return ;
}
【贪心】codeforces B. Heidi and Library (medium)的更多相关文章
- 【贪心】codeforces A. Heidi and Library (easy)
http://codeforces.com/contest/802/problem/A [题意] 有一个图书馆,刚开始没有书,最多可容纳k本书:有n天,每天会有人借一本书,当天归还:如果图书馆有这个本 ...
- 【网络流】codeforces C. Heidi and Library (hard)
http://codeforces.com/contest/802/problem/C
- 贪心算法 Heidi and Library (easy)
A. Heidi and Library (easy) time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 题解-CF802C Heidi and Library (hard)
题面 CF802C Heidi and Library (hard) 有一个大小为 \(k\) 的空书架.有 \(n\) 天和 \(n\) 种书,每天要求书架中有书 \(a_i\).每天可以多次买书, ...
- 【CF802C】Heidi and Library(网络流)
[CF802C]Heidi and Library(网络流) 题面 CF 洛谷 题解 前面两个Easy和Medium都是什么鬼玩意啊.... 不难发现如果这天的要求就是第\(a_i\)种书的话,那么\ ...
- C. Heidi and Library (神奇的网络流)
C. Heidi and Library 题意 有 n 种分别具有价格 b 的书 a ,图书馆里最多同时存放 k 本书,已知接下来 n 天每天都有一个人来看某一本书,如果图书馆里没有则需要购买,问最少 ...
- CF802C Heidi and Library hard 费用流 区间k覆盖问题
LINK:Heidi and Library 先说一下简单版本的 就是权值都为1. 一直无脑加书 然后发现会引起冲突,可以发现此时需要扔掉一本书. 扔掉的话 可以考虑扔掉哪一本是最优的 可以发现扔掉n ...
- 【CF802C】Heidi and Library (hard) 费用流
[CF802C]Heidi and Library (hard) 题意:有n个人依次来借书,第i人来的时候要求书店里必须有种类为ai的书,种类为i的书要花费ci块钱购入.而书店的容量只有k,多余的书只 ...
- Codeforces 802 ABC. Heidi and Library
题目大意 你需要保证第\(i\)天时有第\(a_i\)种书.你可以在任何一天买书,买第\(i\)种书的代价为\(c_i\). 你最多同时拥有\(k\)本书,如果此时再买书,则必须先扔掉已拥有的一本书. ...
随机推荐
- ios学习笔记 UITableView(纯代码) (二)
头文件 --------------------------------------------- #import <UIKit/UIKit.h> /** UITableViewDataS ...
- QQ面板拖拽(慕课网DOM事件探秘)(下)
2.鼠标事件坐标获取 function fnDown(event) { var event = event || window.event; var oDrag = document.getEleme ...
- Android Platform Version 和 API Level对照
Platform Version API Level VERSION_CODE Notes Android 5.1 22 LOLLIPOP_MR1 Platform Highlights Androi ...
- MongoDB部署、使用、监控及调优
MongoDB部署 系统环境:CentOS7 下载地址:http://mirrors.163.com/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD ...
- 激活eclipse自动提示功能
eclipse设置: Window->Preferences->Java->Editor->Content Assist
- SQL Server 查询锁表和接锁表
SQL Server 查询锁表 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) as tableNa ...
- docker上配置nginx负载均衡
采用ubuntu系统,docker安装自行百度 1.安装tomcat docker run -d -p : tomcat docker run -d -p : tomcat 安装两个实例,端口分别为8 ...
- SQLServer · 最佳实践 · SQL Server 2012 使用OFFSET分页遇到的问题
1. 背景 最近有一个客户遇到一个奇怪的问题,以前使用ROW_NUMBER来分页结果是正确的,但是替换为SQL SERVER 2012的OFFSET...FETCH NEXT来分页出现了问题,因此,这 ...
- 打开centos直接进入文本模式命令行
2.打开/etc/inittab 文件 #vim /etc/inittab3.在默认的 run level 设置中,可以看到第一行书写如:id:5:initdefault:(默认的 run level ...
- 1.1 NLP基础技能,字符串的处理
#!/usr/bin/env python # coding: utf-8 # # 字符串操作 # ### 去空格和特殊字符 # In[8]: s = " hello world! &quo ...