Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
829 B
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int t;
	cin>>t;
	int c=0;
	while(t--){
		int n;
		cin>>n;
		int a[n+1],b[n+1];
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++) cin>>b[i];
		int left[n+1],right[n+1];
		for(int i=1;i<=n;i++) left[i]=n+1;
		for(int i=1;i<=n;i++) right[i]=0;
		int idx=1;
		for(int i=1;i<=n;i++){
			if(a[i]==b[idx]){
				left[idx]=i;
				idx++;
			}
		}
	 	idx=n;
		for(int i=n;i>=1;i--){
			if(a[i]==b[idx]){
				right[idx]=i;
				idx--;
			}
		}
		for(int i=1;i<=n;i++) cout<<left[i]<<" ";cout<<endl;
		for(int i=1;i<=n;i++) cout<<right[i]<<" ";cout<<endl;	
		int ans=n;
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				if(left[i]<=n && right[j]>=1 && left[i]<right[j]){
					ans=min(ans,j-i-1);
					ans=min(ans,min(n-i,j-1));
				}
			}
		}
		cout<<ans<<"\n";
	}
}
Leave a Comment