【BZOJ 3048】【USACO2013 Jan】Cow Lineup 滑块思想
昨天下午想了好久没想出来,果然是很弱,思考能力低下。
用的类似单调队列的思想,维护一个长度为$k+1$的滑块,每次统计下$ans$就可以了
#include<cstdio> #include<algorithm> using namespace std; int n, k, H[100003], id[100003], a[100003], cnt = 0, c[100003], st, ans = 0; inline bool cmp(int X, int Y) { return H[X] < H[Y]; } int main() { scanf("%d%d\n", &n, &k); for(int i = 1; i <= n; ++i) { scanf("%d\n", &H[i]); id[i] = i; } id[0] = 0; H[0] = 100003; sort(id + 1, id + n + 1, cmp); for(int i = 1; i <= n; ++i) { if (H[id[i]] != H[id[i-1]]) ++cnt; a[id[i]] = cnt; } cnt = 0; st = 0; for(int i = 1; i <= n; ++i) { ++c[a[i]]; if (c[a[i]] == 1) { ++cnt; while (cnt > k + 1) { --c[a[st]]; if (c[a[st]] == 0) --cnt; ++st; } } ans = max(ans, c[a[i]]); } printf("%d\n",ans); return 0; }
没了
NOI 2017 Bless All