P3419 [POI2005]SAM-Toy Cars

Description

​ Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到它们. 为了让他的房间有足够的空间,在任何时刻地板上都不会有超过k 个玩具. Jasio 在地板上玩玩具. Jasio'的妈妈则在房间里陪他的儿子. 当Jasio 想玩地板上的其他玩具时,他会自己去拿,如果他想玩的玩具在架子上,他的妈妈则会帮他去拿,当她拿玩具的时候,顺便也会将一个地板上的玩具放上架子使得地板上有足够的空间. 他的妈妈很清楚自己的孩子所以他能够预料到Jasio 想玩些什么玩具. 所以她想尽量的使自己去架子上拿玩具的次数尽量的少,应该怎么安排放玩具的顺序呢?

Input

​ n,k,p(1≤k≤n≤100 000, 1≤p≤500 000), 之后p行表示想玩玩具的顺序。

Output

​ 最少的拿玩具的次数。

Sample Input

3 2 7
1
2
3
1
3
1
2

Sample Output

4

​ 一道挺好的贪心题。

​ 挺简单就过了,地上最多放(k)个玩具,如果要加入一个新的玩具,那就把下一次出现时间最晚的那个玩具拿走,用堆每次取一个最大出现时间就好了。还有一个注意的点就是:如果有两个相同的玩具,不用把先出现的那个弹走,如果把它弹走会(Tle),因为晚出现的那个玩具一定比早出现的那个更优,如果要取出只会取出晚出现的。

​ 还有一个点:一开始有相同的玩具,记得判断一下。

#include <iostream>
#include <cstdio>
#include <cctype>
#include <queue>
	
using namespace std;
	
inline long long read() {
    long long s = 0, f = 1; char ch;
    while(!isdigit(ch = getchar())) (ch == '-') && (f = -f);
    for(s = ch ^ 48;isdigit(ch = getchar()); s = (s << 1) + (s << 3) + (ch ^ 48));
    return s * f;
}

const int N = 5e5 + 5;
int n, k, p, ans;
int a[N], vis[N], siz[N], last[N];
struct toy {
    int id, siz;
    toy() {}
    toy(int x, int y) { id = x; siz = y; }
    friend bool operator < (const toy &a, const toy &b) {
        return a.siz < b.siz;
    }
} ;
priority_queue <toy> q; 

void init() {
    n = read(); k = read(); p = read();
    for(int i = 1;i <= p; i++) a[i] = read();
    for(int i = 1;i <= n; i++) last[i] = p + 1;
    for(int i = p;i >= 1; i--) {
        siz[i] = last[a[i]], last[a[i]] = i;
    }
}

void work() {
    int tmp;
    for(tmp = 1; ; tmp++) {
        if(vis[a[tmp]]) {
             q.push(toy(a[tmp], siz[tmp]));
        }
        else {
            ans++; q.push(toy(a[tmp], siz[tmp]));
            vis[a[tmp]] = 1;
        }
        if(ans == k) break;
    }
    for(int i = tmp + 1;i <= p; i++) {
        if(vis[a[i]]) {
            q.push(toy(a[i], siz[i]));
            continue;
        }
        int x = q.top().id; 
        vis[x] = 0; q.pop();
        q.push(toy(a[i], siz[i]));
        vis[a[i]] = 1;
        ans++;
    }
    printf("%d", ans);
}

int main() {

    init();
    work();

    return 0;
}
原文地址:https://www.cnblogs.com/czhui666/p/13568081.html