QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672045#7757. Palm IslandkkkkkTL 0ms3540kbC++201.7kb2024-10-24 15:26:352024-10-24 15:26:36

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 15:26:36]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3540kb
  • [2024-10-24 15:26:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
typedef pair<PII,ll> PIII;
#define endl '\n'
#define x first
#define y second
const ll N =2e5+100,M=30,INF = 1e18;
ll n,a[N],b[N];
deque<ll> q;
bool check(){
	deque<ll> pp=q;
	ll idx=1;
	while(!pp.empty()){
		ll x=pp.front();
		pp.pop_front();
		if(x==idx) idx++;
		else return false;
	}
	return true;
}
void solve(){
	cin>>n;
	vector<ll> ans;
	map<ll,ll> pos;
	for(ll i=1;i<=n;i++){
		cin>>a[i];
		pos[a[i]]=i;
	}
	for(ll i=1;i<=n;i++) cin>>b[i];
	for(ll i=1;i<=n;i++){
		if(b[i]!=i){
			a[pos[b[i]]]=i;
			b[i]=i;
		}
	}
	q.clear();
	for(ll i=1;i<=n;i++) q.push_back(a[i]);
	if(check()){
		cout<<endl;
		return ;
	}
	bool flag=true;
	if(n%2==0){
	    for(ll i=1;i<=n;i+=2){
		if(a[i]+1!=a[i+1]) flag=false;
	    }
	    if(flag){
	    	ll x=q.front();
	    	q.pop_front();
	    	ans.push_back(1);
	    	q.push_back(x);
	    }
	}
	while(1){
		ll x=q.front();
		q.pop_front();
		ll y=q.front();
		if(x==n){
			q.push_back(x);
			ans.push_back(1);
		}
		else if(x+1==y){
			ll cnt=0;
			while(x+1==y){
				ans.push_back(1);
				q.push_back(x);
				x=y;
				q.pop_front();
				y=q.front();
				cnt++;
			}
			q.push_back(x);
			ans.push_back(1);
			cnt++;
			if(cnt==n){
				break;
			}
		}
		else{
			while(x+1!=y){
				q.pop_front();
				q.push_back(y);
				ans.push_back(2);
				y=q.front();
			}
			q.push_front(x);
		}
	}
	for(ll i=0;i<ans.size()-n;i++) cout<<ans[i];
	cout<<endl;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	ll __=1;cin>>__;
    while(__--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3540kb

input:

2
3
1 2 3
2 3 1
4
1 2 3 4
2 1 3 4

output:

1
2111

result:

ok Correct. (2 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

200
3
3 1 2
2 3 1
4
2 4 1 3
2 1 4 3
4
1 4 2 3
2 1 3 4
5
4 3 2 1 5
2 4 5 3 1
5
2 1 5 4 3
5 2 4 1 3
4
4 3 1 2
1 2 4 3
3
1 2 3
3 1 2
4
1 4 2 3
2 1 4 3
4
1 3 2 4
1 4 3 2
3
3 2 1
1 3 2
3
2 3 1
1 3 2
4
1 4 3 2
3 1 2 4
3
1 2 3
1 3 2
3
3 2 1
2 3 1
5
5 1 3 2 4
2 4 5 1 3
4
4 3 1 2
1 4 3 2
4
1 3 4 2
2 4 3 1
3
...

output:


result: