【贪心】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\)本书,如果此时再买书,则必须先扔掉已拥有的一本书. ...
随机推荐
- Java GUI 布局管理器
容器可设置布局管理器,管理容器中组件的布局: container.setLayout(new XxxLayout()); Java有6种布局管理器,AWT提供了5种: FlowLayout Borde ...
- hihocoder1133 二分·二分查找之k小数
思路: 类似于快排的分治算法. 实现: #include <iostream> #include <cstdio> #include <algorithm> #in ...
- iOS 获取真机上系统动态库文件
iOS 获取真机上所有系统库文件 系统动态库文件存放真机地址(/System/Library/Caches/com.apple.dyld/dyld_shared_cache_arm64) 在Mac\i ...
- 迅为iMX6UL Cortex-A7架构单核ARM开发板接口介绍-支持定制
支持商业级和工业级核心板 1. POWER 电源接口电源输入为 5V/2A+,给核心板提供 5V 电源,给底板供电.原理图部分如下图所示. 电源接口位置如下图所示. 2. SWITCH 电源开关轻触电 ...
- DELETE - 删除一个表中的行
SYNOPSIS DELETE FROM [ ONLY ] table [ WHERE condition ] DESCRIPTION 描述 DELETE 从指明的表里删除满足 WHERE 子句的行. ...
- uva 1451 数形结合
思路:枚举点t,寻找满足条件的点t': 计sum[i]为前i项合,平均值即为sum[t]-sum[t'-1]/t-t'+1 设(Pi=(i,Si),表示点在s中的位置,那么就可以画出坐标图,问题就转化 ...
- nginx的编译安装
下载源码 wget http://nginx.org/download/nginx-1.15.9.tar.gz 安装开发包组 yum groupinstall "Development To ...
- Qt setWindow setViewPort
painter.setWindow(-50, -50, 100, 100); //表示x,y坐标不变,可视的窗口移动到(-50,-50)的位置.同时在x,y方向产生factorx= (window.w ...
- java使用数据库连接池
连接池的实现方式是首先使用JNDI(JavaTM Naming and Directory Interface) 将数据源对象注册为一个命名服务,然后使用JNDI提供的服务接口,按照名称检索对应的数据 ...
- 717. 1-bit and 2-bit Characters@python
We have two special characters. The first character can be represented by one bit 0. The second char ...