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; }/* */?
轉載于:https://www.cnblogs.com/CJLHY/p/9758034.html
總結
- 上一篇: 第一次随笔
- 下一篇: 【spring】jar包详解与模块依赖关