经典面替换算法,每次选择最远的那个碟片请求进行替换。

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 10000 + 5;
queue<int>pos[maxn];
int a[105], driv[15];
int k, n;

int solve() {
	int ans = 0, cnt = 0;
	for(int i = 0; i < n; ++i) {
		int ok = 0;
		for(int j = 0; j < cnt; ++j) {
			if(driv[j] == a[i]) {
				ok = 1;
				break;
			}
		}
		if(!ok) {
			++ans;
			if(cnt < k) {
				driv[cnt++] = a[i];
			}
			else if(cnt == k) {
				int ind = 0, far = -1;
				for(int j = 0; j < k; ++j) {
					int p = pos[driv[j]].empty() ? inf : pos[driv[j]].front();
					if(p > far) {
						ind = j;
						far = p;
					}
				}
				driv[ind] = a[i];
			}
		}
		pos[a[i]].pop();
	}
	return ans;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d%d", &k, &n);
		for(int i = 0; i < n; ++i) {
			scanf("%d", &a[i]);
			pos[a[i]].push(i);
		}
		printf("%d\n", solve());
	}
	return 0;
}

如有不当之处欢迎指出!

POJ - 2698 贪心的更多相关文章

  1. POJ - 1017 贪心训练

    Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59725   Accepted: 20273 Descrip ...

  2. POJ 2376 贪心

    题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...

  3. poj 1328 贪心

    /* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...

  4. Yogurt factory(POJ 2393 贪心 or DP)

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 De ...

  5. Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 ...

  6. POJ - 1456 贪心+并查集

    做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...

  7. poj supermaket (贪心)

    http://poj.org/problem?id=1456 #include<cstring> #include<iostream> #include<algorith ...

  8. poj 1456 贪心+STL

    题意:有n个商品,每个商品如果能在截止日期之前售出就会获得相应利益,求能获得的最大利益 一开始对每个时间进行贪心,后来发现后面的商品可以放到之前来卖,然后就wa了 这里就直接对价格排序,把物品尽量放到 ...

  9. zoj 1375||poj 1230(贪心)

    Pass-Muraille Time Limit: 2 Seconds      Memory Limit: 65536 KB In modern day magic shows, passing t ...

随机推荐

  1. The server's host key is not cached in the registry. You have no guarantee that the server……

    使用putty中的pscp.exe ,可以通过脚本方式实现windows向linux上传文件,但pscp.exe第一次运行时必须手工输入确认信息,本文主要解决掉初次运行时的人工交互,彻底实现静默运行. ...

  2. 理解Object.defineProperty的作用

    对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的值.定义对象可以使用构造函数或字面量的形式: var obj = new Object; //obj = {} obj.name = ...

  3. 【转】Shell执行MySql操作

    mysql  -hhostname -Pport -uusername -ppassword  -e  相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作m ...

  4. appium问题整理

    在刚进入appium的世界时,遇到无数的坑,趟过无数的浑水,现在整理一些常用的报错讯息,供大家参考 1.org.openqa.selenium.remote.UnreachableBrowserExc ...

  5. mongodb监控常用方法

    列举mongodb监控的常用命令 1.监控统计 mongostat 可用于查看当前QPS/内存使用/连接数,以及多个shard的压力分布 命令参考 ./mongostat --port 27071 - ...

  6. jquery中ajax跨域提交的时候会有2次请求

    我们平时在同域中请求页面什么的时候不会有这种情况,这种情况大多发生在移动端的跨域请求中发生的. 解决方法就是在服务端中加一层过滤HTTP请求的类型,把OPTION等不用的类型过滤掉.就是当请求为非 H ...

  7. 禁掉或启用firefox 的 javascript 脚本

    老版本的firefox可以直接在“选项”页设置启用或禁用javascript 脚本 新版的Firefox中,我找了半天,没有找到,看来是没法直接设置了 于是在 地址栏键入 about:config 搜 ...

  8. CentOS(Linux)下安装dmidecode包

    安装代码: yum install dmidecode 安装完成后,查看总体信息: dmidecode 查看服务器类型,测试环境为DELL R610: dmidecode -s system-prod ...

  9. java HotSpot 内存管理白皮书

    原文见:http://www.open-open.com/lib/view/open1381034220705.html.查阅资料后,对原文做了补充. 文中关于JVM的介绍基于JDK1.6的Hotsp ...

  10. BZOJ 3790: 神奇项链 [Manacher 贪心]

    3790: 神奇项链 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 405  Solved: 200[Submit][Status][Discuss] ...