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

【题意】

有一个图书馆,刚开始没有书,最多可容纳k本书;有n天,每天会有人借一本书,当天归还;如果图书馆有这个本就直接借到,否则图书馆的人会购买这本书,每本书的价格都是1;如果现在图书馆的书已达上限还需购买,必须舍弃已有的一本书,以后再有人借这本书要重新购买。

问图书馆的人最少要花多少钱购书?

【思路】

关键是替换原则,每次都替换下一次出现最晚的,因为它占用图书馆的时间最长。不是替换后面需要数量最少的!比如

10 2

1 2 4 5 1 1 1 1 2 3

4是替换2而不是1

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm> using namespace std;
typedef long long ll;
const int maxn=;
const int inf=0x3f3f3f3f;
int n,m;
int vis[maxn];
int num[maxn];
int nxt[maxn];
int a[maxn];
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(nxt,,sizeof(nxt));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
int ans=;
int cnt=;
for(int i=;i<n;i++)
{
num[a[i]]--;
if(!vis[a[i]])
{
ans++;
if(cnt<m)
{
cnt++;
}
else
{
int index=;
//替换
for(int k=;k<i;k++)
{
if(vis[a[k]])
{
//如果有一个以后都不出现
if(num[a[k]]==)
{
index=a[k];
break;
}
}
}
if(index==)
{
memset(nxt,,sizeof(nxt));
int mmax=-inf;
for(int k=i+;k<n;k++)
{
if(vis[a[k]]&&!nxt[a[k]])
{
index=a[k];
nxt[a[k]]=;
}
}
}
vis[index]=;
}
vis[a[i]]=;
}
}
printf("%d\n",ans);
}
return ;
}

【贪心】codeforces A. Heidi and Library (easy)的更多相关文章

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

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

  2. 【贪心】codeforces B. Heidi and Library (medium)

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

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

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

  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. 外文翻译 《How we decide》多巴胺的预言 第三节

    这是第二章的最后一节. 书的导言 本章第一节 本章第二节 本节阅读感言:自我批评是自我提升的妙方. 多巴胺是我们感情的源泉.多巴胺相关的神经系统在不断的记录着我们主观意识没有注意到的一个个模式,将它们 ...

  2. AJPFX关于Java内部类及其实例化

    public class Outer {    private int size;    public class Inner {        private int counter = 10;  ...

  3. 洛谷P4013 数字梯形问题(费用流)

    题意 $N$行的矩阵,第一行有$M$个元素,第$i$行有$M + i - 1$个元素 问在三个规则下怎么取使得权值最大 Sol 我只会第一问qwq.. 因为有数量的限制,考虑拆点建图,把每个点拆为$a ...

  4. AngularJS日期格式化

    本地化日期格式化:({{ today | date:'medium' }})    Mar 28, 2016 6:42:25 PM({{ today | date:'short' }})   3/28 ...

  5. 微信小程序开发系列五:微信小程序中如何响应用户输入事件

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  6. leetcode_919. Complete Binary Tree Inserter_完全二叉树插入

    https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...

  7. Android(java)学习笔记179:多媒体之加载大图片到内存(Bitmap API)

    1. Bitmap (API使用) android里面的bitmap中,一个像素点需要4个byte去表示,这是因为android表示颜色是" argb ":其中 a 表示是透明度, ...

  8. python django项目断点调试

  9. Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use ...报错

    错误截图 Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* t ...

  10. ios之sqllite3简单使用

    SQLite3是嵌入在iOS中的关系型数据库,对于存储大规模的数据很有效.SQLite3使得不必将每个对象都加到内存中. 基本操作: (1)打开或者创建数据库 sqlite3 *database; i ...