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;
    }
    
    /*
    */
  • 相关阅读:
    第四周作业
    第三周作业
    第二周作业
    7-1,求最大值及下标值
    7-1.查找整数
    打印沙漏
    赚了还是亏了
    秋末学期总结
    机器学习小知识
    python 小知识
  • 原文地址:https://www.cnblogs.com/CJLHY/p/9758034.html
Copyright © 2011-2022 走看看