bzoj 1826

思路:贪心取最后出现的。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-7;

map<int, int> mp;
map<int, int> in;
set<PII> st;
int n, m, a[N], nx[N];
int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = n; i >= 1; i--) {
        if(mp.find(a[i]) == mp.end()) nx[i] = inf;
        else nx[i] = mp[a[i]];
        mp[a[i]] = i;
    }
    int ans = 0;
    mp.clear();
    for(int i = 1; i <= n; i++) {
//        for(auto t : st) printf("(%d, %d) ", t.fi, t.se);
//        puts("");
        if(in[a[i]]) {
            int id = in[a[i]];
            st.erase(mk(nx[id], a[id]));
            st.insert(mk(nx[i], a[i]));
            in[a[i]] = i;
            continue;
        }
        ans++;
        if(st.size() < m) {
            st.insert(mk(nx[i], a[i]));
            in[a[i]] = i;
        } else {
            PII cur = *(--st.end());
            st.erase(cur);
            in[cur.se] = 0;
            st.insert(mk(nx[i], a[i]));
            in[a[i]] = i;
        }
    }
    printf("%d\n", ans);
    return 0;
}

/*
*/

 

posted @ 2018-10-08 23:45  NotNight  阅读(113)  评论(0编辑  收藏  举报