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. ...
随机推荐
- hdu 4965 矩阵快速幂 矩阵相乘性质
Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Jav ...
- CODEVS_1034 家园 网络流 最大流
原题链接:http://codevs.cn/problem/1034/ 题目描述 Description 由于人类对自然的疯狂破坏,人们意识到在大约2300年之后,地球不能再居住了,于是在月球上建立了 ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 转:C#并口热敏小票打印机打印位图
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class LptControl { private s ...
- Mac Ubuntu ----端口被占用
Mac下使用lsof(list open files)来查看端口占用情况,lsof 是一个列出当前系统打开文件的工具. 使用 lsof 会列举所有占用的端口列表: 1 $ lsof 使用less可以用 ...
- 辅助方法 @Html.Raw与 HtmlString区别
//Html.Raw其实是调用 new Microsoft.AspNetCore.Html.HtmlString(xxx) @{ ViewData["Title"] = " ...
- PPT中的图像失真
现象:Office PowerPoint 保存出来的PPT文件在WPS下播放的时候会出现图像失真的显现. 解决方法:Office PowerPoint打开PPT将里面的图像另存为BMP格式的图像文件, ...
- ubuntu 14.04安装nodejs
http://stackoverflow.com/questions/32902699/cannot-install-ember-on-ubuntu-1404/33495134
- python发声
python发声 学习了:http://www.jb51.net/article/62644.htm import winsound winsound.Beep(600,1000) #其中600表示声 ...
- ElasticSearch生产模式开发模式的区分
ElasticSearch生产模式开发模式的区分 network.host: 0.0.0.0 如果network.host不是localhost就是生产模式, 开发模式中的warning就是生产模式中 ...