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

对于知道使用情况的置换算法,最优解是找一个最后需要使用的物品替换掉

也就是,如果一个物品后面已经不需要用到,就要拿出来了,碍地方

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + ;
int a[maxn], b[maxn];
int pos[maxn], last[maxn]; typedef pair<int, int> pii;
int tim[maxn], in[maxn], DFN;
struct cmp {
bool operator () (const pii &a, const pii &b) {
if (a.first != b.first) return a.first > b.first;
else return a.second > b.second;
}
};
set< pair<int, int>, cmp> ss;
void work() {
ss.clear();
int n, k;
scanf("%d%d", &n, &k);
for (int i = ; i <= n; ++i) {
scanf("%d", a + i);
b[i] = a[i];
last[i] = n + ;
}
sort(a + , a + + n);
for (int i = ; i <= n; ++i) {
b[i] = lower_bound(a + , a + + n, b[i]) - a;
// printf("%d ", b[i]);
}
// printf("\n");
for (int i = n; i >= ; --i) {
pos[i] = last[b[i]];
last[b[i]] = i;
}
int fuck = n + ;
for (int i = ; i <= n; ++i) {
if (pos[i] == n + ) pos[i] = fuck++;
}
int ans = ;
++DFN;
set< pair<int, int> > :: iterator it;
for (int i = ; i <= n; ++i) {
if (in[b[i]] == DFN) {
ss.erase(make_pair(tim[b[i]], b[i]));
ss.insert(make_pair(pos[i], b[i]));
tim[b[i]] = pos[i];
continue;
}
if (ss.size() == k) {
it = ss.begin();
in[it->second] = DFN - ;
ss.erase(it);
}
ss.insert(make_pair(pos[i], b[i]));
in[b[i]] = DFN;
tim[b[i]] = pos[i];
ans++;
}
printf("%d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

F. Cooking Time 贪心的更多相关文章

  1. CodeForces 1042 F Leaf Sets 贪心

    Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点 ...

  2. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)

    "Oh, There is a bipartite graph.""Make it Fantastic."X wants to check whether a ...

  4. 集训第四周(高效算法设计)F题 (二分+贪心)

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...

  5. AtCoder Beginner Contest 249 F - Ignore Operations // 贪心 + 大根堆

    传送门:F - Keep Connect (atcoder.jp) 题意: 给定长度为N的操作(ti,yi). 给定初值为0的x,对其进行操作:当t为1时,将x替换为y:当t为2时,将x加上y. 最多 ...

  6. 牛客练习赛3 F - 监视任务——贪心&&树状数组

    题目 链接 $Reki$ 在课余会接受一些民间的鹰眼类委托,即远距离的狙击监视防卫..$Reki$ 一共接收到$m$份委托,这些委托与 $n$ 个直线排布的监视点相关.第 $i$ 份委托的内容为:对于 ...

  7. 2017 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...

  8. ZOJ 1025 Wooden Sticks(快排+贪心)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25 题目大意:机器运送n个木条,每个木条有一个长度和重量.运送第一根木 ...

  9. 【题解】期末考试 六省联考 2017 洛谷 P3745 BZOJ 4868 贪心 三分

    题目传送门:这里是萌萌哒传送门(>,<) 啊♀,据说这题有个完全贪心的做法,但是要维护太多东西好麻烦的(>,<),于是就来口胡一发三分的做法. 思路很简单,假设我指定了一个x, ...

随机推荐

  1. Understanding the Effective Receptive Field in Deep Convolutional Neural Networks

    Understanding the Effective Receptive Field in Deep Convolutional Neural Networks 理解深度卷积神经网络中的有效感受野 ...

  2. How a web page loads

    The major web browsers load web pages in basically the same way. This process is known as parsing an ...

  3. javascript 文本框值变化触发事件

    javascript 文本框值变化触发事件jo.find(".price").bind('input onpropertychange', function () { me.cal ...

  4. 正经学C#_判断[switch语句]:[c#入门经典]

    switch是一个和IF语句极其相似的语句.但是Switch允许条件可以有多个值. 程序的基本结构如下 switch(textVal) { case Val: 程序代码 break case Val2 ...

  5. CompletionService详解

    public interface CompletionService<V> { Future<V> submit(Callable<V> task); Future ...

  6. 21.Longest Palindromic Substring(最长回文子串)

    Level:   Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...

  7. 黑马JavaScript学习一 BOM之Window对象定时器功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. springboot整合xxl-mq学习笔记

    首先xxl-mq是大神xuxueli开发的一个消息中间件框架: 与springboot整合过程: <?xml version="1.0" encoding="UTF ...

  9. 将优狐智能插座接入 Domoticz

    前言 前几天在某淘宝优惠中看到一个 WiFi 智能插座卖 29 块包邮,心想要是里面是 ESP8266 模块说不定可以刷上固件玩玩,就买了俩回来,记下折腾过程. 拆解 WiFi 智能插座的淘宝介绍页 ...

  10. java 常用操作(保留小数位数、int转string,string转int)

    1.保留2位小数 //similarityTemp为double类型,需要保留2位有效数据,利用String.format String strTemp=String.format("%.2 ...