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

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 ;
}

最新文章

  1. [codeforces 339]D. Xenia and Bit Operations
  2. 【leetcode】Dungeon Game
  3. 使用UILocalNotification给App添加本地消息通知
  4. 导出Exexcl类
  5. NYNU_省赛选拔题(8)
  6. thinkphp整合系列之phpqrcode生成二维码
  7. Htmlunit使用
  8. obj-c编程10:Foundation库中类的使用(5)[时间对象]
  9. java.lang.OutOfMemoryError: PermGen space (jvm内存泄漏解决办法)
  10. ethtool 解决网卡丢包严重和网卡原理【转】
  11. js 可拖动div 调整大小
  12. .net core 微服务之日志落盘设计
  13. 理解 vm.$nextTick
  14. linux系统配置jdk环境
  15. leetcode1011
  16. intellij 快捷键整理
  17. 大数据入门第十一天——hive详解(三)hive函数
  18. Hbase的安装和配置
  19. Computer Vision Tutorials from Conferences (1) -- ICCV
  20. 关于JDBC PreparedStatement

热门文章

  1. nginx负载均衡器处理session共享的几种方法(转)
  2. 下载安装resin-3.X服务器并配置到myeclipse
  3. win7电脑安装wamp出现httpd.exe无法找到组件MSVCR100.dll的解决办法
  4. 【原创】angularjs1.3.0源码解析之directive
  5. vmware, failed to lock the file
  6. 机器学习公开课笔记(8):k-means聚类和PCA降维
  7. The Pilots Brothers&#39; refrigerator(dfs)
  8. 新浪微博的XSS漏洞攻击过程详解
  9. 鸟哥的linux私房菜学习笔记 __ 命令与文件的搜寻
  10. Easy Multiple Copy to Clipboard by ZeroClipboard