HDU 4398 Template Library Management(贪心,STL)

Template Library Management

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 298    Accepted Submission(s): 87


Problem Description
As an experienced ACMer, you must have known the importance of "code template library". With the help of pre-printed code library, you can implement the complicated algorithms correctly and efficiently. However, the size of the library is strictly limited during the contest. For example, you can only take at most 25 pages of printed codes in World Finals. So you must choose carefully which code template should be included.
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 i th problem at first. And solving the i th problem requires a specified code template T i.
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 T i, and T i is happened at your hand (i.e, one of the M code templates you are holding is T i), you can solve it immediately. On the other hand, if you are not holding T i, 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.
Given the problem sequence in the contest and the limitation M, you want finish all the problems with minimum number of calling your friends.
 

 

Input
The first line of each test case contains two numbers N (1 <= N <= 100000) and M (1 <= M <= 100000). The second line contains N numbers T 1, T 2, ..., T N (1 <= T i <= 10 9), indicating the code templates required by each problem.
 

 

Output
Output one line for each test case, indicating the minimum number of calling friends for help.
 

 

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
 

 

Author
RoBa
 

 

Source
 

 

Recommend
zhuyuanchen520
 
 
 
 
贪心。
最优调度算法
。如果要淘汰一个模板T,就要淘汰下一个T坐标最远的那个,如果不再出现就是无   穷远。
这个程序用STL来实现,免去了离散化等操作,而且代码写起来比较简洁。
看来要好好掌握STL,可以事半功倍。
/*
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=100010;

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)==2)
    {
        for(int i=1;i<=n;i++)
          scanf("%d",&Ti[i]);
        mp.clear();//清空map
        for(int i=n;i>=1;i--)//从后往前扫描
        {
            if(mp[Ti[i]])//出现过
               next[i]=mp[Ti[i]];
            else next[i]=n+1;\
            mp[Ti[i]]=i;
        }
        Te.clear();
        T_info.clear();
        for(int i=1;i<=m;i++)//先把前面带的m个模板入set
        {
            if(!mp[i])mp[i]=n+1;
            Node temp;
            temp.next_id=mp[i];
            temp.ti=i;
            T_info.insert(temp);
            Te.insert(i);
        }
        int ans=0;
        for(int i=1;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 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值