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 led子系统(二)

    对于led子系统中,有那么多得trigger,下面就来简单了解下. 1.default-on static void defon_trig_activate(struct led_classdev * ...

  2. app发布流程

    在app上架之前做两件事(instruments,profile): 1.代码静态分析:不用运行程序,直接检测代码有没有潜在的一些内存泄漏 2.动态分析:a l loctions/leaks 内存溢出 ...

  3. enumerated types

    控制台手动输入. package enums; import java.util.Scanner; public class EnumTest { public static void main(St ...

  4. GitExtensions 官方手册(英文)

    在线版本(最新版):https://git-extensions-documentation.readthedocs.io/en/latest/ PDF版本(版本号 2.48):http://pan. ...

  5. 使用zxing编写的二维码生成解析工具:QRCoder

    zxing GitHub地址 QRCoder GitHub地址 TipDialog.java package com.wolf_pan; import java.util.Timer; import ...

  6. (linux)main.c中的初始化

    main.c中的初始化 head.s在最后部分调用main.c中的start_kernel() 函数,从而把控制权交给了它. 所以启动程序从start_kernel()函数继续执行.这个函数是main ...

  7. hihocoder 1015 KMP(找多个位置的 【*模板】)

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  8. HDU3045 Picnic Cows —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-3045 Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memor ...

  9. Scanner、String(java基础知识十二)

    1.Scanner的概述和方法介绍 * A:Scanner的概述 * 是一个从键盘输入的类,有final修饰,不能被子类继承 * Scanner sc = new Scanner(System.in) ...

  10. linux初级学习笔记十:linux grep及正则表达式!(视频序号:04_4)

    本节学习的命令:grep 本节学习的技能: grep对文本的匹配 正则表达式的使用 知识点十:grep及正则表达式(4_4) grep,egrep,fgrep: grep: 根据模式搜索文本,并将符合 ...