CodeForces 190D Non-Secret Cypher

双指针。

#include
#include
#include
#include
using namespace std;

const int maxn=400000+10;
int n,k,a[maxn],b[maxn];
int cnt[maxn];

int get(int val)
{
    int l=1,r=n;
    int pos=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(b[mid]>val) r=mid-1;
        else if(b[mid]1;
        else pos=mid, r=mid-1;
    }
    return pos;
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++) a[i]=get(a[i]);

    memset(cnt,0,sizeof cnt);

    int l=1,r=1; cnt[a[1]]++;
    long long ans=0;
    while(1)
    {
        if(cnt[a[r]]==k)
        {
            ans=ans+(long long)(n-r+1);
            cnt[a[l]]--;
            l++;
        }

        else
        {
            r++;
            cnt[a[r]]++;
        }
        if(l>n||r>n) break;

    }
    printf("%lld\n",ans);

    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/5671758.html

你可能感兴趣的:(CodeForces 190D Non-Secret Cypher)