题意描述半天描述不好,直接粘贴了

Now your team is participating a programming contest whose rules are slightly different from ICPC. This contest consists of N problems, and you must solved them in order: Before you solve the (i+1)th problem, you must solve the ith problem at first. And solving the ith problem requires a specified code template Ti.
You
are allowed to hold M code templates only. At the beginning of the
contest, your are holding templates numbered 1, 2, ..., M. During the
contest, if the problem you are trying to solve requires code template Ti, and Ti is happened at your hand (i.e, one of the M code templates you are holding is Ti), you can solve it immediately. On the other hand, if you are not holding Ti,
you must call your friends who are outside the arena for help (yes, it
is permitted, not cheating). They can give you the code template you
need. Because you are only allowed to hold M code templates, after
solving current problem, you must choose to drop the code you get from
your friends just now, or to keep it and drop one of the M templates at
your hand previously.

Sample Input
4 3
1 2 3 4
11 3
4 1 2 1 5 3 4 4 1 2 3
 
Sample Output
1
4
 /*
HDU 4398
G++ 156ms 2868K
贪心,维护一个M个元素的集合,根据当前位置的元素的
下一个位置选择,删除下一个位置最远的元素 */ #include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#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 classcomp
{
bool operator()(const Node &a,const Node &b)const
{
return a.next_id<b.next_id;//从小到大排序
}
};//这个逗号别忘记
multiset<Node,classcomp>T_info;
multiset<Node>::iterator it_n;
set<int>Te;
set<int>::iterator it; int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m)==)
{
for(int i=;i<=n;i++)
scanf("%d",&Ti[i]);
mp.clear();//清空map
for(int 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(int i=;i<=m;i++)//先把前面带的m个模板入set
{
if(!mp[i])mp[i]=n+;
Node temp;
temp.next_id=mp[i];
temp.ti=i;
T_info.insert(temp);
Te.insert(i);
}
int ans=;
for(int i=;i<=n;i++)
{
it=Te.find(Ti[i]);
if(it!=Te.end())
{
Node temp;
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]);
Node temp;
temp.next_id=next[i];
temp.ti=Ti[i];
T_info.insert(temp);
}
}
}
printf("%d\n",ans); }
return ;
}

hdu 4398 STL的更多相关文章

  1. hdu 4398 Template Library Management(贪心+stl)

    题意:n道题,每道题需要一个模板,现在手头有m个模板(标号1~m),解题的时候,如果没有需要的模板,可以向朋友借,但是用完之后必须在还给朋友一个模板(也就是说保持手头拥有m个模板),求解完n道题最少需 ...

  2. hdu 4022 STL

    题意:给你n个敌人的坐标,再给你m个炸弹和爆炸方向,每个炸弹可以炸横排或竖排的敌人,问你每个炸弹能炸死多少个人. /* HDU 4022 G++ 1296ms */ #include<stdio ...

  3. hdu 1412 (STL list)

    简单例题 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1412 list 相关博客:http://www.cnblogs.com/fangyukuan/a ...

  4. HDU 6040 stl

    Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. HDU 4022 stl multiset

    orz kss太腻害了. 一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include ...

  6. hdu 4941 stl的map<node,int>用法

    #include<iostream> #include<cstdio> #include<cstring> #include<map> using na ...

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

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

  8. 【转载】ACM总结——dp专辑

    感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一 ...

  9. 【DP专辑】ACM动态规划总结

    转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 ...

随机推荐

  1. 锋利的jQuery-7--query ui效果库--拖动排序插件sortable

    一个简单的拖动排序效果,具体请参看jQuery ui官网demo. jquery ui :http://jqueryui.com/ sortable例子:http://jqueryui.com/sor ...

  2. centos7中systemctl命令使用方法和心得体会

    使用linux的同学对service和chkconfig两个命令都不陌生,其重要性不言而喻,那么怎么会突然冒出个systemctl命令呢?其实,为了简化操作,systemctl命令将service和c ...

  3. msmms (二) sms与mms 简述!

    mms 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . MMS是英文缩写,它可以是Membership Management System的缩写,中文译名为会员管理系统.也可以是M ...

  4. POJ 3349 Snowflake Snow Snowflakes

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27598 Accepted: ...

  5. Robot Motion(imitate)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11065   Accepted: 5378 Des ...

  6. MVC中html转义问题(直接输出html的方法)

    MVC中如果用@string(string是包含html代码的字符串)形式输出字符串,那么对应的html标签会自动转义,如果想直接输出html可用以下方法: @(new HtmlString( &qu ...

  7. UVa 11462 Age Sort

    解题报告:给若干个居民的年龄排序,年龄的范围在1到100之间,输入的总人数在0到200W.这题要注意的输入的文件约有25MB,而内存限制为2MB,所以如果人数是像200W这样多的话,甚至都不能把它们都 ...

  8. Controller之间传递数据:协议传值

    http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...

  9. 【消息队列MQ】各类MQ比较

    目录(?)[-] RabbitMQ Redis ZeroMQ ActiveMQ JafkaKafka 目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息 ...

  10. Linux: xclip,pbcopy,xsel用法 terminal 复制粘帖 (mac , ubuntu)

    ubuntu下的用户可以只用apt-get来安装: sudo apt-get install xclip echo "Hello, world" | xclip mac下使用pbc ...