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)的更多相关文章

  1. 【贪心】codeforces A. Heidi and Library (easy)

    http://codeforces.com/contest/802/problem/A [题意] 有一个图书馆,刚开始没有书,最多可容纳k本书:有n天,每天会有人借一本书,当天归还:如果图书馆有这个本 ...

  2. 【网络流】codeforces C. Heidi and Library (hard)

    http://codeforces.com/contest/802/problem/C

  3. 贪心算法 Heidi and Library (easy)

    A. Heidi and Library (easy) time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. 题解-CF802C Heidi and Library (hard)

    题面 CF802C Heidi and Library (hard) 有一个大小为 \(k\) 的空书架.有 \(n\) 天和 \(n\) 种书,每天要求书架中有书 \(a_i\).每天可以多次买书, ...

  5. 【CF802C】Heidi and Library(网络流)

    [CF802C]Heidi and Library(网络流) 题面 CF 洛谷 题解 前面两个Easy和Medium都是什么鬼玩意啊.... 不难发现如果这天的要求就是第\(a_i\)种书的话,那么\ ...

  6. C. Heidi and Library (神奇的网络流)

    C. Heidi and Library 题意 有 n 种分别具有价格 b 的书 a ,图书馆里最多同时存放 k 本书,已知接下来 n 天每天都有一个人来看某一本书,如果图书馆里没有则需要购买,问最少 ...

  7. CF802C Heidi and Library hard 费用流 区间k覆盖问题

    LINK:Heidi and Library 先说一下简单版本的 就是权值都为1. 一直无脑加书 然后发现会引起冲突,可以发现此时需要扔掉一本书. 扔掉的话 可以考虑扔掉哪一本是最优的 可以发现扔掉n ...

  8. 【CF802C】Heidi and Library (hard) 费用流

    [CF802C]Heidi and Library (hard) 题意:有n个人依次来借书,第i人来的时候要求书店里必须有种类为ai的书,种类为i的书要花费ci块钱购入.而书店的容量只有k,多余的书只 ...

  9. Codeforces 802 ABC. Heidi and Library

    题目大意 你需要保证第\(i\)天时有第\(a_i\)种书.你可以在任何一天买书,买第\(i\)种书的代价为\(c_i\). 你最多同时拥有\(k\)本书,如果此时再买书,则必须先扔掉已拥有的一本书. ...

随机推荐

  1. PowerShell和Bash的介绍

    PowerShell是运行在windows平台的脚本,而Bash是运行在linux平台的脚本 现在bash能做的事情,PowerShell也能做,PowerShell的强大之处是它可以管理window ...

  2. JavaScript中函数是不能重载原因

    以前有一次写JS插件的时候,由于后台写习惯了,妄想在JS中写重载函数,可惜不能成功,原因花了一点时间记了下来 首先要理解重载的含义:函数返回值不同或者形式参数个数不同但函数名相同的函数 JavasSc ...

  3. 在线编译器Coding Ground

    http://www.tutorialspoint.com/codingground.htm Free Online IDE and Terminal - Edit, Compile, Execute ...

  4. java (给出年月日,计算该日是该年的第n天 )

    package com.ywx.testdemo01; import java.util.Scanner; /** * 题目:给出年月日,计算该日是该年的第n天 * @author yangwenxu ...

  5. IOS存储目录documents你在哪里啊

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

  6. 在Apache服务器中禁用option

    在apache禁止 http OPTIONS方法. apache disable http OPTIONS method 2013-04-17 09:27 4050人阅读 评论(1) 收藏 举报  分 ...

  7. 洛谷 P2153 [SDOI2009]晨跑

    题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街 ...

  8. (转)Spring的bean管理(注解方式)

    http://blog.csdn.net/yerenyuan_pku/article/details/69663779 Spring的bean管理(注解方式) 注解:代码中的特殊标记,注解可以使用在类 ...

  9. 应用程序员眼中的数据库管理系统:API+数据库语言

    应用程序员眼中的数据库管理系统:API+数据库语言 sqlite3_open_v2 https://www.cnblogs.com/cchust/p/5121559.html

  10. JavaScript-条件循环

    JavaScript-条件循环 条件判断语句 if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码 if (表达式){    当条件为 true 时执行的代码} if...else 语 ...