2 条题解

  • 0
    @ 2023-8-16 16:52:14
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1000005;
    int n,k,t,len=0x3f3f3f3f;
    int id[maxn],b;
    struct node{
    	int pos;
    	int val;
    }f[maxn];
    bool cmp(node a,node b)
    {
    	return a.pos<b.pos;
    }
    int main()
    {
    	memset(id,-1,sizeof(id));
    	scanf("%d%d",&n,&k);
    	for(int i=1;i<=k;i++)
    	{
    		scanf("%d",&t);
    		for(int j=1;j<=t;j++)
    		{
    			scanf("%d",&f[++b].pos);
    			f[b].val=i;
    		}
    	}
    	sort(f+1,f+n+1,cmp);
    	int hd=1,cnt=0;
    	for(int i=1;i<=n;i++)
    	{
    		if(id[f[i].val]==-1) cnt++;
    		id[f[i].val]=f[i].pos;
    		while(hd<=i&&f[hd].pos!=id[f[hd].val]) hd++;
    		if(cnt==k&&f[i].pos-f[hd].pos<len) len=f[i].pos-f[hd].pos;
    	}
    	printf("%d",len);
    	return 0;
    }
    
    • -1
      @ 2022-9-23 22:51:02
      #include<bits/stdc++.h>
      using namespace std;
      const int maxn=1000005;
      int n,k,t,len=0x3f3f3f3f;
      int id[maxn],b;
      struct node{
      	int pos;
      	int val;
      }f[maxn];
      bool cmp(n0de a,node b)
      {
      	return a.pos<b.pos;
      }
      int main()
      {
      	memset(id,-1,sizeof(id));
      	scanf("%d%d",&n,&k);
      	for(int i=1;i<=k;i++)
      	{
      		scanf("%d",&t);
      		for(int j=1;j<=t;j++)
      		{
      			scanf("%d",&f[++b].pos);
      			f[b].val=i;
      		}
      	}
      	sort(f+1,f+n+1,cmp);
      	int hd=1,cnt=0;
      	for(int i=1;i<=n;i++)
      	{
      		if(id[f[i].val]==-1) cnt++;
      		id[f[i].val]=f[i].pos;
      		while(hd<=i&&f[hd].pos!=id[f[hd].val]) hd++;
      		if(cnt==k&&f[i].pos-f[hd].pos<len) len=f[i].pos-f[hd].pos;
      	}
      	printf("%d",len);
      	return o;
      }
      
      • @ 2023-8-16 16:54:10

        你只是把0写成了o,o写成了0,代码对的。

    • 1

    信息

    ID
    1293
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    (无)
    递交数
    24
    已通过
    9
    上传者