hdu 4398 Template Library Management(贪心+stl)
题意:n道题,每道题需要一个模板,现在手头有m个模板(标号1~m),解题的时候,如果没有需要的模板,可以向朋友借,但是用完之后必须在还给朋友一个模板(也就是说保持手头拥有m个模板),求解完n道题最少需要向朋友请求多少次帮助。
思路:贪心,每次抛弃模板的时候抛弃下次使用最靠后的那一个。(怎么想到的。。怎么证明。)
#include<iostream>
#include<stdio.h>
#include<map>
#include<set>
using namespace std; const int MAXN=; int Ti[MAXN];//模板标号
int _next[MAXN];//下个模板位置
map<int,int>mp;//模板出现位置 struct Node{
int next_id;//下个模板位置
int ti;//当前标号
}; struct classCompare{
bool operator()(const Node &a,const Node &b)const{
return a.next_id<b.next_id;//升序
}
}; multiset<Node,classCompare>T_info;//模板信息
multiset<Node>::iterator it_n;
set<int>Te;//模板标号
set<int>::iterator it; int main(){
int n,m,i,ans;
Node temp;
while(~scanf("%d%d",&n,&m)){
for(i=;i<=n;++i)scanf("%d",&Ti[i]);
mp.clear();//清空map
for(i=n;i>=;--i){//从后往前扫描
if(mp[Ti[i]])//出现过
_next[i]=mp[Ti[i]];
else _next[i]=n+;
mp[Ti[i]]=i;
}
Te.clear();
T_info.clear();
for(i=;i<=m;++i){//把带的m个模板加入set
if(!mp[i])mp[i]=n+;
temp.next_id=mp[i];
temp.ti=i;
Te.insert(i);
T_info.insert(temp);
}
ans=;
for(i=;i<=n;++i){//遍历每道题需要的模板
it=Te.find(Ti[i]);
if(it!=Te.end()){
temp.next_id=i;
temp.ti=Ti[i];
T_info.erase(temp);
temp.next_id=_next[i];//更新
T_info.insert(temp);
}
else{
++ans;
it_n=T_info.end();//
--it_n;//最靠后的一个
if(_next[i]<(*it_n).next_id){
Te.erase((*it_n).ti);
T_info.erase(it_n);
Te.insert(Ti[i]);
temp.next_id=_next[i];
temp.ti=Ti[i];
T_info.insert(temp);
}
}
}
printf("%d\n",ans);
}
return ;
}
hdu 4398 Template Library Management(贪心+stl)的更多相关文章
- HDU 4398 Template Library Management (最优页面调度算法)
中等偏易题.操作系统理论中的最优页面调度算法,贪心.当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个. 代码: #include <iostream> ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- hdu 4398 STL
题意描述半天描述不好,直接粘贴了 Now your team is participating a programming contest whose rules are slightly diffe ...
- [c++] STL = Standard Template Library
How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...
- C++ Standard Template Library STL(undone)
目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...
- Simple Library Management System HDU - 1497(图书管理系统)
Problem Description After AC all the hardest problems in the world , the ACboy 8006 now has nothing ...
- Calibre - book library management application
http://calibre-ebook.com/ Library Management E-book conversion Syncing to e-book reader devices Down ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- django升级2.1python升级3.7时出现的错误:"trying to load '%s': %s" % (entry[1], e) django.template.library.InvalidTemplateLibrary:
django升级2.1python升级3.7时出现如下的错误: "trying to load '%s': %s" % (entry[1], e) django.template. ...
随机推荐
- PatentTips - Write Combining Buffer for Sequentially Addressed Partial Line Operations
SUMMARY OF THE INVENTION The present invention pertains to a write combining buffer for use in a mic ...
- Scrapy学习-1-入门
基础知识 爬虫发展史 爬虫去重 1. 存储到数据库中 存取速度慢 2. 存储到内存中的集合里,内存占用十分大 当爬取数据有1亿条时 1*10**8*2Byte*50str_len/1024/102 ...
- ajax 分页(jquery分页插件pagination) 小例1
<link rel="stylesheet" href="/plugins/jQuery/page/pagination.css"/> <sc ...
- curl抓取数据
抓取数据的代码: $url='抓取数据的网站路径'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示 ...
- Chrome V8系列--浅析Chrome V8引擎中的垃圾回收机制和内存泄露优化策略
V8 实现了准确式 GC,GC 算法采用了分代式垃圾回收机制.因此,V8 将内存(堆)分为新生代和老生代两部分. 一.前言 V8的垃圾回收机制:JavaScript使用垃圾回收机制来自动管理内存.垃圾 ...
- 洛谷 P2613 【模板】有理数取余
P2613 [模板]有理数取余 题目描述 给出一个有理数c=\frac{a}{b}c=ba,求c\ \bmod 19260817c mod19260817的值. 输入输出格式 输入格式: 一共两行. ...
- IntelliJ IDEA设置properties文件显示中文
配置这里: 注意:上面是Default Settings,还需要在Settings中设置成上面一样的.
- Intent传递简单对象与集合
我们在Intent传递传递对象.能够有三种方式,实现Serializable接口.实现Parcelable接口,使用json格式序列化与反序列化. 在此我们使用第二方式,现实Parcelable接口, ...
- ubutu强制结束进程 kill -9 ProcessID
强制终止进程 kill -9 2128 表示强制结束进程号 2128 对应的进程.
- docker save docker load
docker save && docker load docker save 镜像1 镜像2 | gzip > images.tar.gz 打包镜像为压缩文件 docker sa ...