【贪心】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\)本书,如果此时再买书,则必须先扔掉已拥有的一本书. ...
随机推荐
- 成为Android高手必须掌握的8项基本要求
[1] Android操作系统概述 1. Android系统架构. 2. Android利用设计理念. 3. Android 开源知识. 4. Android 参考网站与权威信息.[2] Androi ...
- android 防止bitmap 内存溢出
在android开发过程中经常会处理网络图片发送内存溢出,那么怎么解决这种问题? 思路: 下载到本地 通过网络获取和文件下载存放到手机中目录 代码: // 获取网络 public InputStrea ...
- ios https 安全证书配置
原定于2017年1月1日起所有提交到 App Store 的App必须强制开启 ATS,需要配置Https.但是现在不需要了,无固定期限的往后延期了,但是这个还是得弄明白下为好,说不定哪天突然就让弄了 ...
- Linux 合并多个txt文件到一个文件
Linux 或 类Unix 下实现合并多个文件内容到一个文件中 代码如下 cat b1.txt b2.txt b3.txt > b_all.txt 或者 cat *.txt > merge ...
- 原生ajax请求的五个步骤
//第一步,创建XMLHttpRequest对象 var xmlHttp = new XMLHttpRequest(); function CommentAll() { //第二步,注册回调函数 xm ...
- HashMap和HashTable的理解与区别
Hashtable是java一开始发布时就提供的键值映射的数据结构,而HashMap产生于JDK1.2.虽然Hashtable比HashMap出现的早一些,但是现在Hashtable基本上已经被弃用了 ...
- clone对象或数组
function clone(obj) { var o; if (typeof obj == "object") { if (obj === null) { o = null; } ...
- 【洛谷2019 OI春令营】期中考试
T68402 扫雷 题目链接:传送门 题目描述 扫雷,是一款单人的计算机游戏.游戏目标是找出所有没有地雷的方格,完成游戏:要是按了有地雷的方格,游戏失败.现在 Bob 正在玩扫雷游戏,你作为裁判要判断 ...
- Hibernate初始化环境的基本封装
public class HibernateUtils { private static SessionFactory sf; static{ sf = new Configuration().con ...
- MySQL-06 数据备份和恢复
学习目标 数据备份 数据恢复 数据库迁移 导入和导出 数据备份 系统意外崩溃或者服务器硬件损坏都有可能导致数据库丢失,因此生产环境中数据备份非常重要. MySQLdump命令备份 该命令可以将数据库备 ...