[SCOI2009]生日禮物

題面描述

傳送門

思考

本來想用二分的,

讀完數據之後,又發現不用了。

然而需要開多一個數組pp記錄顏色最近出現的位置,以便進行玄學操作

當達成目標(now==endnow==end)時,

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;

因爲a[l].cola[l].col最近出現的位置不是a[l].pa[l].p,那麼證明在a[l].pa[l].p之後還有a[l].cola[l].col出現,不影響nownow的結果,因此可以刪去。

貌似除了這個沒有什麼奇奇怪怪的東西了。

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章