zoukankan      html  css  js  c++  java
  • 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
    ", ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    监控文件变化
    哈希+ LIst + 流文件 应用
    (转)Delphi版木马彩衣一个简单的花指令伪装器
    (转)TThread 详解
    遍历菜单
    哈希 + LIST简单应用(DELPHI)
    WINDOWS API速查
    ASP.NET 3.5的页面指令
    感冒怎么治?
    ASPNET应用程序文件夹
  • 原文地址:https://www.cnblogs.com/CJLHY/p/9758034.html
Copyright © 2011-2022 走看看