hdu 4398 STL
题意描述半天描述不好,直接粘贴了
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.
/*
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的更多相关文章
- hdu 4398 Template Library Management(贪心+stl)
题意:n道题,每道题需要一个模板,现在手头有m个模板(标号1~m),解题的时候,如果没有需要的模板,可以向朋友借,但是用完之后必须在还给朋友一个模板(也就是说保持手头拥有m个模板),求解完n道题最少需 ...
- hdu 4022 STL
题意:给你n个敌人的坐标,再给你m个炸弹和爆炸方向,每个炸弹可以炸横排或竖排的敌人,问你每个炸弹能炸死多少个人. /* HDU 4022 G++ 1296ms */ #include<stdio ...
- hdu 1412 (STL list)
简单例题 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1412 list 相关博客:http://www.cnblogs.com/fangyukuan/a ...
- HDU 6040 stl
Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 4022 stl multiset
orz kss太腻害了. 一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include ...
- hdu 4941 stl的map<node,int>用法
#include<iostream> #include<cstdio> #include<cstring> #include<map> using na ...
- HDU 4398 Template Library Management (最优页面调度算法)
中等偏易题.操作系统理论中的最优页面调度算法,贪心.当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个. 代码: #include <iostream> ...
- 【转载】ACM总结——dp专辑
感谢博主—— http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 动态规划一 ...
- 【DP专辑】ACM动态规划总结
转载请注明出处,谢谢. http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 ...
随机推荐
- iOS Xcode 调试技巧 全局断点这样加才有意思
http://blog.sina.com.cn/s/blog_876a2c9901016ezh.html
- 高性能的分布式内存对象缓存系统Memcached
Memcached概述 什么是Memcached? 先看看下面几个概念: Memory:内存存储,不言而喻,速度快,对于内存的要求高,不指出的话所缓存的内容非持久化.对于CPU要求很低,所以常常采 ...
- ORA-00931: missing identifier ORA-06512: at "SYS.DBMS_UTILITY"
Database db = DatabaseFactory.CreateDatabase(); string sql = "SELECT * FROM table&qu ...
- 关于笔记本安装双系统windows and linux
ps1.安装完成后,补充下如何设在win7为默认启动系统, 大家也都知道,在linux 运行当软件都是以配置文件来设置参数当,当然grub菜单也不例外, 修改菜单可以进入grub.conf [root ...
- 论文的构思!姚小白的html5游戏设计开发与构思----给审核我论文的导师看的
此处只为笔记 游戏么基本上确定是用canvas做个能一只手玩的游戏!基本打飞机之类的.毕竟手机也就上下班玩玩的.上下班么基本就是一只手拉着扶手一只手撸啊撸! 当然啦,如果能搞出超级牛逼的游戏,比如刺客 ...
- Node.js 创建HTTP服务器(经过测试,这篇文章是靠谱的T_T)
Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...
- undefined与null的区别
最近在默默的看面试题,其中有一个题目就是“undefined和null的区别”,突然意识到自己从未关注过这个问题,心中莫名有种急躁的感觉,百度一下发现阮大神的一篇文章(http://www.ruany ...
- IOS 页面之间的跳转
1.UINavigationController popToViewController 对应popViewControllerAnimated: 也可以使用: [self.navigationCon ...
- 对 Linux 新手非常有用的 20 个命令
参考:http://www.oschina.net/translate/useful-linux-commands-for-newbies 英文原文:http://www.tecmint.com/us ...
- Android数据存储之SQLite的操作
Android作为一个应用在移动设备上的操作系统,自然也就少不了数据的存储.然而SQLite作为一个轻型的关系型数据库,基于其轻量.跨平台.多语言接口及安全性等诸多因数考虑,因而Android较大的数 ...