zoj 3790

来源:互联网 发布:360软件管家有安卓版 编辑:程序博客网 时间:2024/06/05 00:33

排序 尺取法

#include <cstdio>#include <cstring>#include <cctype>#include <iostream>#include <vector>#include <cstdio>#include <set>#include <algorithm>#include <map>#include <string>#include <queue>using namespace std;const int maxn = 100100;struct node{    int head, tail;    int x, num;    bool operator < (const node & thy) const {        if(x != thy.x)            return x < thy.x;        return head < thy.head;    }}pi[maxn];int a[maxn], n, k;int main(){    while(scanf("%d%d", &n, &k) == 2) {        int cc = 0;        a[0] = 0;        pi[0].x = 0;        for(int i = 1; i <= n; ++ i) {            scanf("%d", &a[i]);            if(a[i] != a[i-1]) {                pi[cc].tail = i-1;                cc++;                pi[cc].head = i;                pi[cc].x = a[i];                pi[cc].num = 1;            }            else {                pi[cc].num++;            }        }        pi[cc].tail = n;        sort(pi+1, pi+cc+1);//        for(int i = 1; i <= cc; ++ i) {//            printf("%d %d %d %d\n", pi[i].num, pi[i].x, pi[i].head, pi[i].tail);//        }        int start = 0, ans = 1, ck = 0, now;        for(int i = 1; i <= cc; ++ i) {            if(pi[i].x != pi[i-1].x) {                start = i;                ck = 0;                now = pi[i].num;                ans = max(ans, now);            }            else {                ck += pi[i].head-pi[i-1].tail-1;                now += pi[i].num;                while(ck > k) {                    ck -= (pi[start+1].head-pi[start].tail-1);                    now -= pi[start].num;                    start ++;                }                ans = max(ans, now);            }        }        printf("%d\n", ans);    }    return 0;}/*8 11 1 1 2 2 3 2 2*/


0 0