记录编号 168222 评测结果 AAAAAAAAAA
题目名称 [NOIP 2005]篝火晚会 最终得分 100
用户昵称 Gravatarmikumikumi 是否通过 通过
代码语言 C++ 运行时间 0.100 s
提交时间 2015-07-01 23:05:58 内存使用 1.27 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,want[2][50010],goal[50010],H[50010]={0},H2[50010]={0},x,y,ans=-1;
int slove()
{
	int re=0;
	for(int i=1;i<=n;i++)
	{
		x=i-goal[i];
		if(x<0) x+=n;
		H[x]++;
		if(H[x]>re) re=H[x];
		y=i-(n-goal[i]+1);
		if(y<0) y+=n;
		H2[y]++;
		if(H2[y]>re) re=H2[y];
		//cout<<x<<endl;
	}
	return re;
}
int main()
{
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&want[0][i],&want[1][i]);
	goal[0]=want[0][1];
	goal[1]=1;
	goal[n+1]=1;
	for(int i=1;i<=n;i++)
	{
		int x=want[0][goal[i]],y=want[1][goal[i]];
		//cout<<goal[i]<<endl;
		if(x==goal[i-1]) goal[i+1]=y;
		else if(y==goal[i-1]) goal[i+1]=x;
		else goto NEXT;
	}
	ans=n-slove();
	printf("%d",ans);
	return 0;
	NEXT:;
	printf("-1");
	return 0;
}