bzoj 1293 //1293: [SCOI2009]生日礼物 队列

bzoj 1293   //1293: [SCOI2009]生日礼物   队列

bzoj 1293   //1293: [SCOI2009]生日礼物   //在线测评地址https://www.lydsy.com/JudgeOnline/problem.php?id=1293
//在线测评地址https://www.luogu.org/problem/P2564

更多题解,详见https://blog.csdn.net/mrcrack/article/details/90228694BZOJ刷题记录

bzoj 1293 //1293: [SCOI2009]生日礼物 队列_第1张图片

Accepted 12540 kb 2336 ms C++/Edit 1033 B

//1293: [SCOI2009]生日礼物
//在线测评地址https://www.lydsy.com/JudgeOnline/problem.php?id=1293
//在线测评地址https://www.luogu.org/problem/P2564
/*
思路:开结构体,记录种类,位置。
按位置由小到大排序。
开单调队列,若无该种类,进队列,若种类齐全,计算位置差。
开个in_queue[]=0队列中无,in_queue[]=1队列中有2个,in_queue[]=1队列中有2个,进入队列in_queue[]++,队首in_queue[]>1,可弹出,则in_queue[]--;
sum统计种类数量,若sum==k可以开始计算位置差。加入与弹出雷同,就可以计算位置差了。
*/
//样例通过,提交AC.2019-11-16 23:07 提交过程中,很忐忑,难得AC一道省选题啊。
#include
#include
#include
#define maxn 1000100
using namespace std;
int q[maxn],h,t,in_queue[65],n,k,cnt=0;//in_queue[i]记录某种类在队列中的数量
struct node{
    int kind,pos;
}a[maxn];
int cmp(node a,node b){
    return a.pos }
int min(int a,int b){
    return a }
int main(){
    int i,m,pos,j,mn=2147483647,kinds=0;//kinds记录队列中已有的种类数量
    scanf("%d%d",&n,&k);
    for(i=1;i<=k;i++){
        scanf("%d",&m);
        for(j=1;j<=m;j++)scanf("%d",&pos),cnt++,a[cnt].kind=i,a[cnt].pos=pos;
    }
    sort(a+1,a+1+n,cmp);
    memset(in_queue,0,sizeof(in_queue)),h=t=1,q[t]=1,t++,in_queue[a[1].kind]++,kinds++;
    for(i=2;i<=n;i++){//i一定要被加入队列
        if(!in_queue[a[i].kind])kinds++;
        q[t]=i,t++,in_queue[a[i].kind]++;//q[t]=i记录结构题a[i]中i的位次
        while(h1)in_queue[a[q[h]].kind]--,h++;//此处顺序写错,错写成while(h1)h++,in_queue[a[q[h]].kind]--;
        if(kinds==k)mn=min(mn,a[q[t-1]].pos-a[q[h]].pos);
    }
    printf("%d\n",mn);
    return 0;
}

 

 

 

你可能感兴趣的:(跟着大佬学算法)