QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#133605#4937. Permutation Transformationckz#ML 1ms3540kbC++201.5kb2023-08-02 11:51:012023-08-02 11:51:05

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-02 11:51:05]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:3540kb
  • [2023-08-02 11:51:01]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()
using namespace std;

#define int ll
const int N = 1e5 + 10;

int n;
int fa[N], sz[N];

int find(int x){
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y)
{
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(sz[x] > sz[y]) swap(x, y);
    fa[x] = y;
    sz[y] += sz[x];
}

void solve()
{
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i], a[i]--;

    for(int i = 0; i < n; i++) fa[i] = i, sz[i] = 1;
    for(int i = 0; i < n; i++) merge(i, a[i]);

    vector<int> vis(n);
    int lc = 1, mxtail = 0;
    for(int i = 0; i < n; i++)
    {
        if(vis[find(i)]) continue;
        vis[find(i)] = 1;

        int s = sz[find(i)];

        vector<int> v(s);
        iota(all(v), 1);
        v[s - 1] = 0;

        map<vector<int>, int> mp;
        mp[v] = 1;
        int tot = 1;
        while(1)
        {
            auto t = v;
            for(auto &it : t) it = v[it];
            v = t;

            if(mp[v])
            {
                mxtail = max(mxtail, mp[v] - 1);
                lc = lcm(lc, tot - mp[v] + 1);
                break;
            }
            mp[v] = ++tot;
        }
    }

    cout << lc + mxtail;
}

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

詳細信息

Test #1:

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

input:

5
3 5 1 2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3476kb

input:

8
7 5 1 6 8 2 3 4

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3472kb

input:

1
1

output:

1

result:

ok single line: '1'

Test #4:

score: -100
Memory Limit Exceeded

input:

100000
20864 34918 58550 1465 75674 30743 27235 88900 47488 50029 46054 84871 20330 72228 16506 44561 92519 97750 82891 60324 90508 39290 24663 38077 90189 30671 95476 64027 70888 90749 22566 8525 33675 16635 23392 97636 35788 89625 41966 78051 94034 15407 26545 83799 2233 10873 56946 71566 19045 44...

output:


result: