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日 ...
随机推荐
- derby数据库ql语法
[数据库知识] 主键.唯一键包含索引 主键包含唯一键.索引.非空 唯一键包含索引,可空或非空 数据库需要与执行服务的在同个目录下 唯一键 create table app.tyu ( primaryk ...
- ASP.NET MVC中通过Request.IsAjaxRequest()来判断是否要加载公共视图
个人目测 Request.IsAjaxRequest()这个东西是判断前台提交过来的header中的 X-Requested-With:XMLHttpRequest来区分是不是ajax请求的. ASP ...
- git基础知识总结
1,clone git clone https://github.com/KoMiles/helloword helloword 2,pull git pull 3,commit git commit ...
- ASP.NET使用FileUpload上传文件
前台代码: <asp:FileUpload ID="fuKeleyi" runat="server" /> <asp:Button ID=&q ...
- Ubuntu下编译SuiteSparse-4.4.1和METIS-4.0.3
网上关于编译的介绍非常多,其实ubuntu系统自带编译好的SuiteSparse,不想折腾的话,用新立得很容易就搞定 准备工作: 下载并编译OpenBLAS(会连带Lapack也下载和编译),图省事请 ...
- 支付宝也要上"服务号"?斗战微信继续升级
微信着实火了一阵,特别是微信服务号,在商户和消费者之间形成闭环,O2O领先了一步,作为曾经的电商支付老大支付宝来说是一种心头之痛,迫于压力,支付宝将于5月在支付宝钱包中上线服务窗功能,同时支付宝钱包将 ...
- ios反射
http://www.cr173.com/html/18677_1.html 1.反射获取类属性名和属性类型 unsigned ; objc_property_t *properties = clas ...
- nginx reload
iwangzheng.com Usage: nginx [-?hvVt] [-s signal] [-c filename] [-p prefix] [-g directives] Options:- ...
- php方法 隐藏手机号中间四位
$num = "13966778888"$str = substr_replace($num,'****',3,4);
- How can I pretty-print JSON in python?
python -m json.tool my_json.json 转自: http://stackoverflow.com/questions/352098/how-can-i-pretty-prin ...