codefoce Cooking Time
#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的更多相关文章
- cooking构建工具报错MSBUILD :error MSB4132解决办法
最近学习cooking构建工具的时候,在自己的笔记本上运行的好好的,项目在公司电脑上clone下来的时候,发现构建报错,逐条查错,试了好多方法也不行 最后在github上找到了答案,只是之前一直没找到 ...
- CodeForces - 1040B Shashlik Cooking(水题)
题目: B. Shashlik Cooking time limit per test 1 second memory limit per test 512 megabytes input stand ...
- A - Shashlik Cooking CodeForces - 1040B
http://codeforces.com/problemset/problem/1040/B Long story short, shashlik is Miroslav's favorite fo ...
- CodeForces - 1040B Shashlik Cooking
Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simu ...
- 2017浙江省赛 A - Cooking Competition ZOJ - 3958
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3958 题目: "Miss Kobayashi's Drag ...
- Cooking Schedule Problem Code: SCHEDULE(优先队列)
Cooking Schedule Problem Code: SCHEDULE Chef is a well-known chef, and everyone wishes to taste his ...
- Shashlik Cooking
Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simu ...
- selenium+python cooking用法 (转)
selenium-webdriver(python)--cookie处理 driver.get_cookies() 获得cookie信息 add_cookie(cookie_dict) 向cooki ...
- cooking java ——加密解密
java安全与密码概述 主要分为三部分: 密码学基础,包括:相关术语:分类:常用安全体系. java的安全组成:jdk以及第三方扩展. 相关实现代码,包括:base64.MD5········ 密码学 ...
随机推荐
- Jquery的jqzoom插件的使用(图片放大镜)
今天学习一下,图片放大镜功能,需要使用插件JQzoom 引入文件 <script type="text/javascript" src="js/jquery.min ...
- 【Jmeter_WebService接口】对项目中的GetProduct接口生成性能脚本
一.环境信息 https://xxx.xxx.svc?wsdl 用户名:username 密码:password 对其中的GetProduct接口进行测试 二.通过soapui获取soup请求信息 1 ...
- spring boot 打jar包
想必大家经常会出现以下报错信息 java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Fai ...
- day037 行记录的操作
1.库操作 2.表操作 3.行操作 1.库操作 1)创建数据库 语法: create database 数据库名 charset utf8; 数据库命名规则: 由数字,字母,下划线,@,#,$ 等组成 ...
- js如何生成一个对象,并转化为json字符串
js如何生成一个对象,并转化为json字符串,很多人都会误写为: var ary = []; var obj = {}; for (var i = 0; i < 3; i++) { obj.na ...
- 二:通过VirtualBox+Vagrant创建一个centos的虚拟机:
官网安装VirtualBox及Vagrant. 下载centos7,添加到vagrant中. http://e-proxy.yfb.sunline.cn/download/vagrant/centos ...
- 理解JavaScript的运行
JavaScript可以运行在head和body标签中! HTML的脚本必须放在<script></script>标签中间! 浏览器会解释并执行位于script标签中的脚本! ...
- 深入margin
1.外边距叠加 外边距叠加是指两个垂直外边距相遇时,这两个外边距会合并成一个外边距,就是二变一,关键是叠加后的外边距会取值两个外边距最大的那个: 例子如下:创建A.B两个盒子,A定义一个margin- ...
- 5.10 C++内存管理操作符重载
参考:http://www.weixueyuan.net/view/6388.html 注意: 内存管理操作符new.new[].delete和delete[]同样也可以进行操作符重载,其重载形式既可 ...
- windows文件映射
0x01 使用文件映射实现共享内存. 用内存映射文件实现进程间的通讯:Windows中的内存映射文件的机制为我们高效地操作文件提供了一种途径,它允许我们在进程中保留一段内存区域,把硬盘或页文件上的目标 ...