題面描述
思考
本來想用二分的,
讀完數據之後,又發現不用了。
然而需要開多一個數組記錄顏色最近出現的位置,以便進行玄學操作
當達成目標()時,
if(now==end)
{
while(l<i&&p[a[l].col]!=a[l].p)++l;//不影響,而且更短
ans=min(ans,(ll)a[i].p-a[l].p);
now^=b[a[l++].col];//保證now!=end
}
while(l<i&&p[a[l].col]!=a[l].p)++l;
因爲最近出現的位置不是,那麼證明在之後還有出現,不影響的結果,因此可以刪去。
貌似除了這個沒有什麼奇奇怪怪的東西了。
AC code
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#define ll long long
#define gc getchar()
using namespace std;
const int N=1e6+10;
inline void qr(int &x)
{
x=0;int f=1;char c=gc;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc;}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;}
x*=f;
}
void qw(int x)
{
if(x<0)x=-x,putchar('-');
if(x/10)qw(x/10);
putchar(x%10+48);
}
ll b[61];
struct node
{
int p;int col;
}a[N];
int p[61];
int q[N];
bool cmp(node a,node b){return a.p<b.p;}
int main()
{
ll end=1;int n,m;qr(n),qr(m);
b[1]=1;for(int i=2;i<=m;i++)b[i]=b[i-1]<<1,end|=b[i];
ll now=0;
int tot=0;
for(int i=1;i<=m;i++)
{
int len;qr(len);
while(len--)qr(a[++tot].p),a[tot].col=i;
}
sort(a+1,a+n+1,cmp);
int l=1;ll ans=1e10;
for(int i=1;i<=n;i++)
{
p[a[i].col]=a[i].p;
now|=b[a[i].col];
if(now==end)
{
while(l<i&&p[a[l].col]!=a[l].p)++l;
ans=min(ans,(ll)a[i].p-a[l].p);
now^=b[a[l++].col];
}
}
qw(ans);puts("");
return 0;
}