题意: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. Cucumber Vs RobotFramework

    I asked this same question a little over a year ago on this list when we were at your stage. Before ...

  2. Laravel 控制器的request

    public function request1(Request $request){ //取值 $name = Request::input('name'); //是否有值 if($request- ...

  3. vba比较日期大小,定义日期;vba让excel保存

    Private Sub CommandButton1_Click()Dim i, j As IntegerDim a As Datea = #10/1/2013#j = 2i = 2' If  Wor ...

  4. win7系统使用engine进行开发报错,“未能加载文件或程序集”

    http://www.gisall.com/wordpress/?p=7161 使用vs2010加 arcengine 开发winfrom应用,新建了uc,拖了几个控件后,编译,报未能加载文件或程序集 ...

  5. android 获取屏幕宽高 和 获取控件坐标

    一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width ...

  6. HDU2897( 巴什博奕变形)

    邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  7. 数组遍历 map()、forEach() 及 字符串切割 split() / 字符串截取 slice()、substring()、substr()

    JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代 ...

  8. C#3.0之神奇的Lambda表达式和Lambda语句

    “Lambda 表达式”是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型.所有 Lambda 表达式都使用 Lambda 运算符 =>,该运算符读为“goes to” ...

  9. vs2010配置VL_FEAT库

    VL_FEAT库是计算机视觉中的一个开源库,支持C/C++,Matlab,可以在http://www.vlfeat.org/下载. 本文主要讲一下VS2010中如何配置vl_feat库(算是对原文的一 ...

  10. 记录:50多行程序中找出多写的一个字母e

    小霍同学调程序,做的是第11周的项目1 - 存储班长信息的学生类,可是她写的程序(就在以下),呃,请读者自己执行一下吧.(下午在机房调试时用的是Code::Blocks10.05.输出的是非常长的莫名 ...