#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. Mac下安装和配置Maven

    1.下载Maven 官网:http://maven.apache.org/download.cgi 下载版本:apache-maven-3.5.3-bin.tar.gz 2.配置环境变量 打开term ...

  2. 使用AndroidStudio运行eclipse开发的app项目

    由于AS和eclipse开发的APP项目格式不同,所以直接用AS运行eclipse项目是行不通的. 下面给大家讲解一下如何在AS上成功运行eclipse项目 首先有这么个eclipse项目文件夹 然后 ...

  3. Java Web(九) JDBC及数据库连接池及DBCP,c3p0,dbutils的使用

    DBCP.C3P0.DBUtils的jar包和配置文件(百度云盘):点我下载 JDBC JDBC(Java 数据库连接,Java Database Connectify)是标准的Java访问数据库的A ...

  4. shell脚本分析二

    Shell 基本运算符Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符原生bash不支持简单的数学运算,但是可以通过其他命令来实 ...

  5. Linux command nmon

    Linux command nmon [Purpose]        Learning linux command nmon   [Eevironment]        Ubuntu 16.04 ...

  6. global 全局变量 nonlocal 局部变量

    # x= # def func(): # x= # # func() # print(x) # x=[] # def func(): # x.append() # x.append() # x.app ...

  7. redis、memcache、mongoDB 对比

    从以下几个维度,对 redis.memcache.mongoDB 做了对比. 1.性能 都比较高,性能对我们来说应该都不是瓶颈. 总体来讲,TPS 方面 redis 和 memcache 差不多,要大 ...

  8. VSTO:使用C#开发Excel、Word【3】

    <Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath >——By Eric C ...

  9. idea 一些插件配置

    接触maven快2年了吧,对maven还是一知半解其实.得到了一些教训,就是少转牛角尖,多把握实际需要的东西,一口一口吃饭. 插件化很常见了.这里记录idea使用的jetty插件 和tomcat插件和 ...

  10. android 应用程序中执行Linux 命令

    ADB 无线调试命令son = "setprop service.adb.tcp.port 5555\n" + "stop adbd\n" + "st ...