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?

Input

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.

Output

On a single line print the minimum cost of buying books at the store so as to satisfy all requests.

Examples
input
4 80
1 2 2 1
output
2
input
4 1
1 2 2 1
output
3
input
4 2
1 2 3 1
output
3
Note

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

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)

    http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...

  5. 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+.. ...

  6. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

  7. [Helvetic Coding Contest 2017 online mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...

  8. 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记

    第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...

  9. Helvetic Coding Contest 2016 online mirror A1

    Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...

随机推荐

  1. Linux环境下如何查找哪个线程使用CPU最长

    top -H -p pid 查看端口是否被占用: netstat -apn|grep 80

  2. Pycharm下运行程序查看每个变量的值的方法(类似于Spyder和MATLAB)

    昨天,用了大量篇幅讲了Spyder的各种问题,之所以要用Spyder,最重要的一个原因就是能够非常方便的查看中间变量的值.类似MATLAB的工作空间,非常方便.如下图所示: 但是Spyder的代码自动 ...

  3. DOM操作二

    1.创建节点 createElement():   创建新的Element节点 var s = document.createElement('script'); createTextNode(): ...

  4. code[VS] 1297 硬币

    题目描写叙述 Description 我们知道即使是同一种面值的硬币,它们的重量也有可能不一样,由于它受到很多因素的影响,包含制造工艺和流程上的.可是不论什么一种面值的硬币的重量总是处于某个特定范围之 ...

  5. 开启 J2EE(六)— Servlet之Filter具体解释及乱码处理实例

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/akkzhjj/article/details/36471477 定义和功能 Filter:过滤器,它 ...

  6. java -jar 与nohup的区别

    ——作为java程序员,经常会遇到这样一个问题,打个jar包,测试或者上线生产,于是乎面临的选择来了,java –jar or nohup? 下面我来扒一扒: 一.    java -jar a.ja ...

  7. css的书写规范,有哪些注意点

    一.框架为先,细节次之. 先写一些浮动,然后高与宽,然后再是细节方面的书写. 比如写一个浮动容器的样式,我们应该先让这个容器的框架被渲染出来,让大家看到基本的 网站框架.然后再再去渲染容器里面的内容. ...

  8. chdir函数的使用【学习笔记】

    #include "apue.h" #include <fcntl.h> int main(void) { ) err_sys("chdir failed&q ...

  9. vue开发:移动端图片上传

    因为最近遇到个移动端上传头像的需求,上传到后台的数据是base64位,其中为了提高用户体验,把比较大的图片用canvas进行压缩之后再进行上传.在移动端调用拍照功能时,会发生图片旋转,为了解决这个问题 ...

  10. FileReader、 FileWriter、readLine()和newLine()、LineNumberReader(二十一)

    1.字符流FileReader * 1.字符流是什么 * 字符流是可以直接读写字符的IO流 * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. ...