Groundhog Looking Dowdy

https://ac.nowcoder.com/acm/contest/5674/F

做法:伪单调队列
按值升序排序,如果够m了就更新答案同时pop队首

#include <bits/stdc++.h>
#define inf 2333333333333333
#define N 3000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2020.8.8
using namespace std;
long long n,m,temp,cnt,tot,ans,l,r;
long long q[N];
long long vis[N];
struct node{
    long long day;
    long long v;
    bool operator < (const node & k) const{
        return v<k.v;
    }
}a[N];

void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

signed main(){
    in(n);in(m);
    For(i,1,n){
        in(temp);
        while(temp--){
            a[++cnt].day=i;
            in(a[cnt].v);
        }
    }
    ans=inf;
    sort(a+1,a+cnt+1);
    l=1;r=0;
    For(i,1,m-1){
        if(!vis[a[i].day]) tot++;
        vis[a[i].day]++;
        q[++r]=i;
    }
    For(i,m,cnt){
        if(!vis[a[i].day]) tot++;
        vis[a[i].day]++;
        q[++r]=i;
        while(tot==m){
            ans=min(ans,a[i].v-a[l].v);
            vis[a[l].day]--;
            if(!vis[a[l].day]) tot--;
            l++;
        }
    }
    o(ans);
    return 0;
}
原文地址:https://www.cnblogs.com/war1111/p/13457818.html