题意: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)的更多相关文章

  1. HDU 4398 Template Library Management (最优页面调度算法)

    中等偏易题.操作系统理论中的最优页面调度算法,贪心.当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个. 代码: #include <iostream> ...

  2. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  3. hdu 4398 STL

    题意描述半天描述不好,直接粘贴了 Now your team is participating a programming contest whose rules are slightly diffe ...

  4. [c++] STL = Standard Template Library

    How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...

  5. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  6. Simple Library Management System HDU - 1497(图书管理系统)

    Problem Description After AC all the hardest problems in the world , the ACboy 8006 now has nothing ...

  7. Calibre - book library management application

    http://calibre-ebook.com/ Library Management E-book conversion Syncing to e-book reader devices Down ...

  8. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  9. 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. ...

随机推荐

  1. scanf printf函数返回值

    1. scanf 函数是有返回值的,它的返回值可以分成三种情况  1) 正整数,表示正确输入参数的个数.例如执行 scanf("%d %d", &a, &b);   ...

  2. Redis数据结构之字典

    Redis的字典使用哈希表作为底层实现,一个哈希表里面可以有多个哈希表节点,而每个哈希表节点就保存了字典中的一个键值对. 一.字典结构定义1. 哈希表节点结构定义: 2. 哈希表结构定义: 3. 字典 ...

  3. TreeView 与 ListView

    ListView: viewStyle icon 大图标 list  列表,单列 report 报表 smallIcon 小图标 largeImage 与icon对应 smallImage 与saml ...

  4. Ubuntu 16.04下在Shell终端下使用nautilus快速打开窗口文件夹

    Ubunut 16.04默认使用nautilus进行管理资源文件夹,nautilus默认是支持参数传递的. 使用: nautilus /dirurl 打开当前文件夹(可以使用$PWD代替): naut ...

  5. oracle内核学习总结

    http://blog.csdn.net/bcbobo21cn/article/category/3092145/1

  6. ganglia-monitoring-centos-linux

    http://www.tecmint.com/install-configure-ganglia-monitoring-centos-linux/ http://monitor.gitlab.net/ ...

  7. Docker 的CMD与ENTRYPOINT区别

    我们在构建一个docker镜像的时候,Dockerfile里面有两个命令会引起我们的注意,它们就是 CMD 和 ENTRYPOINT,看起来很相似,实际上并非如此. 一.CMD 顾名思义就是允许用户指 ...

  8. 【Android开发—智能家居系列】(三):手机连接WIFI模块

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 概述 实现连接WIFI的功能会用到一个工具类,源码可以点击链接下载.网上这些类似的工具类里的代码差不多是一样的.连接无线网主要有 ...

  9. 【Todo】Java学习笔记 100==100 & Reflection API & Optional类详解 & DIP、IoC、DI & token/cookie/session管理会话方式

    为什么1000 == 1000返回为False,而100 == 100会返回为True?   Link Java Reflection API:Link Java8 Optional 类深度解析: L ...

  10. leetCode 65.Valid Number (有效数字)

    Valid Number  Validate if a given string is numeric. Some examples: "0" => true " ...