[10.3模拟] color

题意:n个仆人,n个房间,每个房间都有一个颜色,每个仆人都有自己喜欢的颜色,仆人第一天从1-n排列,之后每天依次往后移动一位,若仆人移动到的房间是他最喜欢的颜色,那么你就会知道这个房间的颜色是什么,求知道所有房间颜色的最小天数

题解:

记点东西搞一搞 = =

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define RG register
#define N 100010
#define C 1000010
using namespace std;

int a[N],b[N],f[C],dis[N];

inline int gi() {
  int x=0,o=1; char ch=getchar();
  while(ch!='-' && (ch<'0' || ch>'9')) ch=getchar();
  if(ch=='-') o=-1,ch=getchar();
  while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return o*x;
}

int main() {
  RG int n=gi(),i,t,ans=0,inf;
  for(i=1; i<=n; i++) a[i]=gi();
  for(i=1; i<=n; i++) b[i]=gi();
  memset(dis,63,sizeof(dis));
  inf=dis[0];
  for(int i=1; i<=(n<<1); i++) {
    t=i%n?i%n:n,f[b[t]]=i;
    if(f[a[t]]) dis[t]=min(dis[t],i-f[a[t]]+1);
  }
  for(int i=1; i<=n; i++) ans=max(ans,dis[i]);
  if(ans==inf) puts("-1");
  else printf("%d", ans);
  return 0;
}
posted @ 2017-10-04 08:48  HLX_Y  阅读(123)  评论(0编辑  收藏  举报