#include <bits/stdc++.h>
using namespace std;
struct T { // 贪心 优先弹出相邻靠后的材料
int id;
int p;
bool operator < (const T& t) const {
return p < t.p;
}
};
const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
int a[N], sort_a[N], num;
bool isok[N];
int pre[N];
int p[N];
int n, k;
priority_queue<T> q;
inline int f (int x) {
return lower_bound(sort_a + , sort_a + + num, x) - sort_a; // 错误一 离散化后是num
}
int main ()
{
int w; scanf ("%d",&w);
while (w--) {
while (!q.empty()) q.pop();
memset (isok, , sizeof(isok));
scanf ("%d %d",&n,&k);
for (int i = ; i <= n; i++) {
scanf ("%d", &a[i]);
sort_a[i] = a[i];
}
sort (sort_a + , sort_a + + n);
num = ;
for (int i = ; i <= n; i++) {
if (sort_a[i] != sort_a[num])
sort_a[++num] = sort_a[i];
}
int sum = ; int t = ;
for (int i = ; i <= num; i++)
pre[i] = inf;
for (int i = n; i >= ; i--) {
int x = f(a[i]);// pre[i] 元素i最最近出现的位置
p[i] = pre[x];// p[i] a[i] 与a[i]相同元素相邻位置
pre[x] = i;
}
for (int i = ; i <= n; i++) {
int x = f(a[i]);
if (!isok[x]) {
sum++;
if (t < k) {
T temp = {x, p[i]};
q.push(temp); isok[x] = ;
t++;
}
else {
T t1 = q.top(); q.pop(); isok[t1.id] = ;
T temp = {x, p[i]};
q.push(temp); isok[x] = ;
}
}
else {// 错误二: 即使原来在内存中也要去更新它的位置
T temp= {x,p[i]};
q.push(temp);
}
}
printf ("%d\n", sum);
}
return ;
}

链接 : http://codeforces.com/gym/101498/problem/F

codefoce Cooking Time的更多相关文章

  1. cooking构建工具报错MSBUILD :error MSB4132解决办法

    最近学习cooking构建工具的时候,在自己的笔记本上运行的好好的,项目在公司电脑上clone下来的时候,发现构建报错,逐条查错,试了好多方法也不行 最后在github上找到了答案,只是之前一直没找到 ...

  2. CodeForces - 1040B Shashlik Cooking(水题)

    题目: B. Shashlik Cooking time limit per test 1 second memory limit per test 512 megabytes input stand ...

  3. A - Shashlik Cooking CodeForces - 1040B

    http://codeforces.com/problemset/problem/1040/B Long story short, shashlik is Miroslav's favorite fo ...

  4. CodeForces - 1040B Shashlik Cooking

    Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simu ...

  5. 2017浙江省赛 A - Cooking Competition ZOJ - 3958

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 题目: "Miss Kobayashi's Drag ...

  6. Cooking Schedule Problem Code: SCHEDULE(优先队列)

    Cooking Schedule Problem Code: SCHEDULE Chef is a well-known chef, and everyone wishes to taste his ...

  7. Shashlik Cooking

    Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simu ...

  8. selenium+python cooking用法 (转)

    selenium-webdriver(python)--cookie处理 driver.get_cookies() 获得cookie信息 add_cookie(cookie_dict)  向cooki ...

  9. cooking java ——加密解密

    java安全与密码概述 主要分为三部分: 密码学基础,包括:相关术语:分类:常用安全体系. java的安全组成:jdk以及第三方扩展. 相关实现代码,包括:base64.MD5········ 密码学 ...

随机推荐

  1. globals() 和 locals() 函数

    globals() 和 locals() 函数 根据调用地方的不同,globals() 和 locals() 函数可被用来返回全局和局部命名空间里的名字. 如果在函数内部调用 locals(),返回的 ...

  2. 把旧系统迁移到.Net Core 2.0 日记 (17) --多租户和SoftDelete

    在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilter,它出现的意义在哪里?能够解决什么问题呢? 通过HasQueryFilter方法来创建过滤器能够允许我们对访问特定数据库 ...

  3. css 让div 的高度和屏幕的高度一样

    <html><head><title>无标题文档</title><style type="text/css">html, ...

  4. 事务隔离级别引发的"血案"

    事务引发的"血案"见的多了也麻木了,这回遇到个事务隔离级别的"案子",坑了我小半天的时间...也怪自己细节不牢.. 敲着代码遇到这么一个怪事情: class X ...

  5. python settings :RROR 1130: Host 'XXXXXX' is not allowed to connect to this MySQL server

    pymysql.err.InternalError: (1130, u"Host '127.0.0.1' is not allowed to connect to this MySQL se ...

  6. 【转】java提高篇(二)-----理解java的三大特性之继承

    [转]java提高篇(二)-----理解java的三大特性之继承 原文地址:http://www.cnblogs.com/chenssy/p/3354884.html 在<Think in ja ...

  7. 逆袭之旅DAY24.XIA.数组练习

    2018-07-20 08:40:19 1. public void stringSort(){ String[] s = new String[]{"George"," ...

  8. 利用VisualVm和JMX远程监控Java进程

    自Java 6开始,Java程序启动时都会在JVM内部启动一个JMX agent,JMX agent会启动一个MBean server组件,把MBeans(Java平台标准的MBean + 你自己创建 ...

  9. 初学Linux系统最应该做对的4件事情[长文]

    “闲来无事,逛逛贴吧”已经是本人无事消磨时间的最佳选择了.五花八门的问题,各式各样的回答,总能给自己带来无限的欢乐.当然也有些问题值得自己去思考或者回答.之前就有人在贴吧里问到“Linux好难啊!该怎 ...

  10. [BZOJ3173]最长上升子序列

    Problem 给你n个数A1~An,每次将i插入第Ai位后,最后输出每次插入后这个数列的最长上升子序列 Solution 这道题非常的妙.首先如果新加入的这个数构成了最长上升子序列,由于在它插入之前 ...