Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A
Description
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.
We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.
Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.
There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.
You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
4 80
1 2 2 1
2
4 1
1 2 2 1
3
4 2
1 2 3 1
3
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
题意:学弟给我是这么解释的:有n本书,书架只能放k本,多出来只能从原来的书架丢出来
比如4 80这组,都放得下,前面1,2都买过了,那么以后不需要买了,所以是2
4 1这组,买1放入书架,放不下了,丢掉1,买2放入书架,一共是2
4 2这组,买1,2放入书架,买3丢2(因为1以后要用),再丢1放最后一本1,一共是3
解法:刚开始就想到是操作系统里面的页面置换算法,到底是哪一种呢,当然是最不可能实现的那种,最佳置换算法啦,就是当前使用,以后很久以后才又有用,或者以后也用不到了,因为现实是不知道以后到底要不要用,题目已经告诉你全部数据了,当然很好写,第一题数据量不大就写一次暴力喽
#include <bits/stdc++.h> using namespace std; int a[];
int g[];
set<int> s;
int n; int secr(int num,int k)
{
for(int i = k + ; i<n;i++)
{
if(a[i] == num)
{
return (i-k);
}
}
return ;
} int modify(int k)
{
int maxs = ;
int pos;
for(auto x:s)
{
int q = secr(x,k);
if(q > maxs)
{
maxs = q;
pos = x;
}
}
return pos;
} int main()
{
int k;
scanf("%d %d",&n,&k);
for(int i=;i<n;i++)
{
scanf("%d",a+i);
g[a[i]]++;
}
int sum = ;
for(int i=;i<n;i++)
{
if(s.size() == )
{
s.insert(a[i]);
g[a[i]]--;
sum ++ ;
}
else
{
if(s.find(a[i]) == s.end())
{
sum++;
if(s.size() >= k)
{
s.erase(s.find(modify(i)));
s.insert(a[i]);
g[a[i]]--;
}
else
{
s.insert(a[i]);
g[a[i]]--;
}
}
else
{
g[a[i]]--;
}
}
}
printf("%d\n",sum);
return ;
}
Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A的更多相关文章
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)
G. Fake News (easy) time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
Description Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their comm ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M
Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...
- Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)
http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...
- Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)
题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- [Helvetic Coding Contest 2017 online mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...
- 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记
第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...
- Helvetic Coding Contest 2016 online mirror A1
Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...
随机推荐
- selenium-python问题日记
今天在学习selenium时遇到了两个问题,在这里记录一下: 使用unittest框架组织了测试用例后,拓展一下功能就成了我最想做的事情, 所以决定添加发邮件功能. 使用python自带的smtpli ...
- Mysql中show processlist结果中的status状态总结
一 般情况下,DBA能从监控mysql的状态列表中查看出数据库的运行端倪,需要注意的是STATUS所表示的不同内容.且需要注意的是TIME字段表示的 意思.它表示的只是最后那个STAT状态持续的时间. ...
- 【项目发起】千元组装一台大型3D打印机全教程(一)前言
前言 最近又碰到了大尺寸模型打样的需求,我这台17cm直径的kossel mini就捉襟见肘了.怎么办呢,这个时候kossel的好就体现出来了,随意扩展,那么就自己做个kossel-max吧.为了向前 ...
- cocos2d-x(vs2012)环境搭建(第一篇)[版本号:cocos2d-x-3.1.1]
1.下载资源 下载cocos2d-x包V3.1.1,下载戳这里: http://www.cocos2d-x.org/download vs2012下载戳这里: http://www.xiazaiba. ...
- POJ 2586 Y2K Accounting Bug(枚举大水题)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10674 Accepted: 53 ...
- SSM整理笔记3——配置解析
github:https://github.com/lakeslove/SSM 项目的目录结构如下 首先,配置web.xml <?xml version="1.0" enco ...
- ssh原理【转】
1 转自 http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 2 ssh远程登陆的原理 普通用户远程登陆 ssh jason@ho ...
- $.post 使用案例
$.post( aplnCommon.topUrl + 'ajaxLogin/ajaxLogin.action', { 'userLoginId' : userName, 'pwd' : userPw ...
- 正则工具类以及FinalClass
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/27811103 完整版见https://jadyer. ...
- Kafka使用kclient三种使用方法
kclient提供了三种使用方法,对于每一种方法,按照下面的步骤可快速构建Kafka生产者和消费者程序. 前置步骤1) 下载源代码后在项目根目录执行如下命令安装打包文件到你的Maven本地库. mvn ...