zoukankan      html  css  js  c++  java
  • CodeForces 616D Longest k-Good Segment

    用队列维护一下即可

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int maxn=500000+10;
    int n,k;
    int a[maxn];
    int tot[1000000+10];
    struct Node
    {
        int id;
        int val;
    } node[maxn];
    queue<Node>Q;
    
    int main()
    {
        scanf("%d%d",&n,&k);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            node[i].id=i;
            node[i].val=a[i];
        }
    
        memset(tot,0,sizeof tot);
        int ans=0,ansl,ansr;
        int now=0;
    
        for(int i=1; i<=n; i++)
        {
            Q.push(node[i]);
            if(tot[node[i].val]==0) now++;
            tot[node[i].val]++;
            if(now<=k)
            {
                Node head=Q.front();
                if(node[i].id-head.id+1>ans)
                {
                    ans=node[i].id-head.id+1;
                    ansl=head.id;
                    ansr=node[i].id;
                }
            }
            else if(now>k)
            {
                while(1)
                {
                    Node head=Q.front();
                    if(tot[head.val]==1) now--;
                    tot[head.val]--;
                    Q.pop();
                    if(now==k) break;
                }
    
                Node head=Q.front();
                if(node[i].id-head.id+1>ans)
                {
                    ans=node[i].id-head.id+1;
                    ansl=head.id;
                    ansr=node[i].id;
                }
            }
        }
        printf("%d %d
    ",ansl,ansr);
        return 0;
    }
  • 相关阅读:
    Jzoj1307 Jail
    Jzoj1307 Jail
    Jzoj1306 Sum
    Jzoj1306 Sum
    Jzoj1279 解题
    Jzoj1279 解题
    Jzoj1277最高的奶牛
    Jzoj1277最高的奶牛
    Jzoj1155 有根树的同构(树的Rabin-Karp)
    Jzoj1155 有根树的同构(树的Rabin-Karp)
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5130246.html
Copyright © 2011-2022 走看看