HDU4398 - Template Library Management(贪心 + STL)

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 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.
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 T1, T2, ..., TN (1 <= Ti<= 1e9), 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 

2012 Multi-University Training Contest 10

题目大意:

一个人最开始手上有1-m个模板,是模板1, 2, 3, ,,m的意思,不是需要的n个模板的前m个,比如说你有模板1, 2, 3,但是那需要模板11, 12, 13, 14,那么手上的模板都没用了(写这题时英语太菜产生歧义,导致样例都没搞懂,没写出来)。如果手上没有需要的模板,那么就要像场外的队友求助,可以用完就还给队友,也可以留下来下一次使用,但是要扔掉手中的一个模板,因为一个人只能有m个模板,做完一题才能做下一题,求向队友求助的最少次数。

题目分析:

 只要题目读懂,这题的贪心思路还是很好想的,如果要删掉一个模板,就删掉下次使用最晚的模板,我们可以预处理next数组,来存下一个坐标,如果下次不再使用就置为一个较大的数。我们可以用map和set来简化操作,注意由于一开始我们有1-m个模板,但是next数组是处理需要的n个模板的,所以会对next数组的使用产生麻烦,所以我们用set存结构体,每次通过删除再插入来更新结构体中的next值并使set重新排序。

AC代码:

#include <iostream>
#include <cstdio>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int N = 1e6 + 5;
int t[2 * N];
map<int, int> mp,vis, nex;
struct cmp{
    int num;
    int nex;
    bool operator <(const cmp &a)const{
        if (nex == a.nex)return num < a.num;
        return nex > a.nex;
    }
}tmp;


int main()
{
    int n, m;
    while(scanf("%d %d", &n, &m) == 2){
        for (int i = 1; i <= n; i ++){
            scanf("%d", &t[i]);
        }
        mp.clear();
        nex.clear();
        vis.clear();
        for (int i = n; i >= 1; i --){
            if (mp[t[i]])nex[i] = mp[t[i]];
            else nex[i] = 0x3f3f3f3f;
            mp[t[i]] = i;
        }
        set<cmp> s;
        set<cmp>:: iterator it;
        for (int i = 1; i <= m; i ++){
            if(!mp[i])mp[i] = 0x3f3f3f3f;
            tmp.num = i;
            tmp.nex = mp[i];
            s.insert(tmp);
        }
//        for (int i = 1; i <= n + m; i ++)cout << nex[i] << ' ';
//        cout << endl;
        int ans = 0;
        for (int i = 1; i <= n; i ++){
            tmp.num = t[i];
            tmp.nex = i;
            it = s.find(tmp);
            if (it == s.end()){
                    tmp.num = t[i];
                tmp.nex = nex[i];
                //cout << "**********" << t[*it] << ' ' << t[i] << endl;
                s.insert(tmp);
                s.erase(s.begin());
                ans ++;
            }else{
                s.erase(it);
                tmp.num = t[i];
                tmp.nex = nex[i];
                s.insert(tmp);
            }
//            for (it = s.begin(); it != s.end(); it ++)cout << t[*it] << ' ';
//                cout << endl;
//            for (it = s.begin(); it != s.end(); it ++)cout << nex[*it] << ' ';
//                cout << endl;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值