QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#634068#7757. Palm IslandBai_xiaobo#TL 0ms3624kbC++201.8kb2024-10-12 16:39:272024-10-12 16:39:28

Judging History

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

  • [2024-10-12 16:39:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3624kb
  • [2024-10-12 16:39:27]
  • 提交

answer

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define endl "\n"
#define int long long
#define pr pair<int,int>
#define x first
#define y second
ll mod=1e9+7;

const ll N=1e3+10;
int a[N];
int b[N];
int l[N], r[N];
int suf[N];
int n;

void def(int s)
{
    r[l[s]] = r[s];
    l[r[s]] = l[s];
}

void ist(int s, int suf)
{
    r[s] = suf;
    l[s] = l[suf];
    r[l[suf]] = s;
    l[suf] = s;
}

void solve()
{
    queue<int> ans;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        if (i == 1)
            l[a[i]] = a[n];
        else
            l[a[i]] = a[i - 1];
        r[a[i]] = a[(i % n) + 1];
    }
    for (int i = 1; i <= n; i++)
        cin >> b[i];
    for (int i = 1; i <= n; i++)
        suf[b[i]] = b[(i % n) + 1];
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        if (r[a[i]] != suf[a[i]])
            cnt++;
    }
    int p = a[1];
    while (cnt)
    {
        if (r[p] == suf[p])
        {
            p = r[p];
            ans.push(1);
            continue;
        }
        if (p == suf[l[p]])
            cnt++;
        if (r[p] == suf[l[p]])
            cnt--;
        int pp = r[p];
        def(p);
        while (pp != suf[p])
        {
            pp = r[pp];
            ans.push(2);
        }
        ist(p, pp);
        cnt--;
        if (p == suf[l[p]])
            cnt--;
    }
    while (p != b[1])
    {
        p = r[p];
        ans.push(1);
    }
    while (!ans.empty())
    {
        cout << ans.front();
        ans.pop();
    }
    cout << '\n';
}

/*
1
4
 1 2 3 4
 2 1 3 4
*/

signed main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)
        solve();
}

詳細信息

Test #1:

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

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: