QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#208640 | #4937. Permutation Transformation | Beevo# | TL | 0ms | 3704kb | C++23 | 1.4kb | 2023-10-09 19:34:23 | 2023-10-09 19:34:23 |
Judging History
answer
#include <bits/stdc++.h>
#define el '\n'
#define Beevo ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void testCase() {
int n;
cin >> n;
int p[n + 1], q[n + 1];
for (int i = 1; i <= n; i++)
cin >> p[i], q[i] = p[i];
ll mx = 1;
ll l = 1, cnt;
vector<bool> vis(n + 1);
for (int i = 1; i <= n; i++) {
if (vis[i])
continue;
vector<int> indices = {i}, vals = {p[i]};
while (p[indices.back()] != i)
indices.push_back(p[indices.back()]), vals.push_back(p[indices.back()]), vis[indices.back()] = 1;
cnt = 0;
set<vector<int>> visSt;
vector<int> cur = vals, lst;
while (1) {
if (visSt.find(cur) != visSt.end())
break;
cnt++, visSt.insert(cur), lst = cur, cur.clear();
for (auto &j: indices)
q[j] = p[p[j]], cur.push_back(q[j]);
for (auto &j: indices)
p[j] = q[j];
}
if (lst == cur)
mx = max(mx, cnt);
else
l = 1LL * l * cnt / __gcd(l, cnt);
}
cout << mx - 1 + l;
}
signed main() {
Beevo
int t = 1;
// cin >> t;
while (t--)
testCase();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3704kb
input:
5 3 5 1 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
8 7 5 1 6 8 2 3 4
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
1 1
output:
1
result:
ok single line: '1'
Test #4:
score: -100
Time 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...