Codeforces Round #451 (Div. 2) D. Alarm Clock

来源:互联网 发布:行知小学 编辑:程序博客网 时间:2024/06/05 20:52

题目大意

删去最少的点使没有连续的长度m的一段有超过k个点。

题解

从前到后找,有一个时刻不符合条件就把当前点删掉,显然这样是最优的。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;int read(){    char ch=getchar();int f=0,x=1;    while(ch<'0'||ch>'9'){if(ch=='-') x=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}    return f*x;}int n,a[500005],s[500005],top=0,tail=1,ans,m,k;int main(){    n=read();m=read();k=read()-1;    for(int i=1;i<=n;i++) a[i]=read();    sort(a+1,a+n+1);    for(int i=1;i<=n;i++)    {        while(top>=tail&&s[tail]<=a[i]-m)        {            tail++;        }        s[++top]=a[i];        while(top-tail+1>k)        {            top--;            ans++;        }    }    cout<<ans;}
原创粉丝点击