Submission #68598445


Source Code Expand

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

using namespace std;

inline int nxt() {
	int x;
	cin >> x;
	return x;
}

void remax(int& x, int y) {
	x = (x > y) ? x : y;
}

int f(const vector<int>& values) {
	const int n = values.size();
	vector<vector<int>> dp(n, vector<int>(n, 0));
	vector<vector<int>> others(n);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (i != j && values[i] == values[j]) {
				others[i].push_back(j);
			}
		}
	}
	auto is_in = [&](int j, int i, int len) {
		if (j > i) {
			return j <= i + len;
		} else {
			return j + n <= i + len;
		}
	};
	auto dist = [&](int i, int j) {
		return j >= i ? (j - i) : (j - i + n);
	};
	for (int len = 1; len < n; ++len) {
		for (int i = 0; i < n; ++i) {
			const int j = (i + len) % n;
			dp[i][len] = dp[i][len - 1];
			for (int k : others[j]) {
				if (is_in(k, i, len - 1)) {
					int d = dist(i, k);
					remax(dp[i][len], dp[i][d - 1] + dp[k][len - d]);
				}
			}
			if (values[i] == values[j]) {
				++dp[i][len];
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		remax(ans, dp[i][n - 1]);
	}
	// cerr << ans << "\n";
	return ans;
}

void solve() {
	int n = nxt(), k = nxt();
	const int sz = n * k;
	vector<int> a(sz);
	for (auto& x : a) {
		x = nxt() - 1;
	}
	vector<char> used(sz);
	int ans = 0;
	for (int i = 0; i < sz; ++i) {
		if (used[i]) {
			continue;
		}
		int v = i;
		vector<int> values;
		while (!used[v]) {
			// cerr << v << " -> ";
			values.push_back(v % n);
			used[v] = true;
			v = a[v];
		}
		// cerr << i << "\n";
		ans += f(values);
	}
	cout << ans << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int t = 1; // nxt();
	while (t--) {
		solve();
	}

	return 0;
}

Submission Info

Submission Time
Task B - Sort Permutation
User Golovanov399
Language C++ 20 (gcc 12.2)
Score 800
Code Size 1834 Byte
Status AC
Exec Time 1847 ms
Memory 101536 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 53
Set Name Test Cases
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 02-small-01.txt, 02-small-02.txt, 02-small-03.txt, 02-small-04.txt, 02-small-05.txt, 02-small-06.txt, 02-small-07.txt, 02-small-08.txt, 02-small-09.txt, 02-small-10.txt, 02-small-11.txt, 02-small-12.txt, 02-small-13.txt, 02-small-14.txt, 02-small-15.txt, 02-small-16.txt, 03-random-01.txt, 03-random-02.txt, 03-random-03.txt, 03-random-04.txt, 03-random-05.txt, 04-large-01.txt, 04-large-02.txt, 04-large-03.txt, 04-large-04.txt, 04-large-05.txt, 05-id-01.txt, 05-id-02.txt, 05-id-03.txt, 05-id-04.txt, 06-large-cycle-01.txt, 06-large-cycle-02.txt, 06-large-cycle-03.txt, 06-large-cycle-04.txt, 06-large-cycle-05.txt, 07-large-cycle-2-01.txt, 07-large-cycle-2-02.txt, 07-large-cycle-2-03.txt, 07-large-cycle-2-04.txt, 07-large-cycle-2-05.txt, 08-divide-01.txt, 08-divide-02.txt, 08-divide-03.txt, 08-divide-04.txt, 08-divide-05.txt, 09-large-ans-01.txt, 09-large-ans-02.txt, 09-large-ans-03.txt, 09-large-ans-04.txt, 09-large-ans-05.txt
Case Name Status Exec Time Memory
01-sample-01.txt AC 1 ms 3580 KiB
01-sample-02.txt AC 1 ms 3440 KiB
01-sample-03.txt AC 1 ms 3488 KiB
02-small-01.txt AC 1 ms 3444 KiB
02-small-02.txt AC 1 ms 3428 KiB
02-small-03.txt AC 1 ms 3508 KiB
02-small-04.txt AC 1 ms 3508 KiB
02-small-05.txt AC 1 ms 3452 KiB
02-small-06.txt AC 1 ms 3500 KiB
02-small-07.txt AC 1 ms 3428 KiB
02-small-08.txt AC 1 ms 3560 KiB
02-small-09.txt AC 1 ms 3452 KiB
02-small-10.txt AC 1 ms 3496 KiB
02-small-11.txt AC 1 ms 3564 KiB
02-small-12.txt AC 1 ms 3508 KiB
02-small-13.txt AC 1 ms 3492 KiB
02-small-14.txt AC 1 ms 3428 KiB
02-small-15.txt AC 1 ms 3504 KiB
02-small-16.txt AC 1 ms 3448 KiB
03-random-01.txt AC 57 ms 16296 KiB
03-random-02.txt AC 161 ms 21788 KiB
03-random-03.txt AC 1 ms 3476 KiB
03-random-04.txt AC 3 ms 3780 KiB
03-random-05.txt AC 23 ms 8576 KiB
04-large-01.txt AC 348 ms 34656 KiB
04-large-02.txt AC 62 ms 14864 KiB
04-large-03.txt AC 155 ms 21468 KiB
04-large-04.txt AC 278 ms 36940 KiB
04-large-05.txt AC 155 ms 21088 KiB
05-id-01.txt AC 2 ms 3528 KiB
05-id-02.txt AC 2 ms 3664 KiB
05-id-03.txt AC 1 ms 3468 KiB
05-id-04.txt AC 2 ms 3516 KiB
06-large-cycle-01.txt AC 1847 ms 101536 KiB
06-large-cycle-02.txt AC 1268 ms 82784 KiB
06-large-cycle-03.txt AC 1693 ms 94144 KiB
06-large-cycle-04.txt AC 485 ms 50800 KiB
06-large-cycle-05.txt AC 1755 ms 98572 KiB
07-large-cycle-2-01.txt AC 146 ms 21588 KiB
07-large-cycle-2-02.txt AC 923 ms 68680 KiB
07-large-cycle-2-03.txt AC 1427 ms 86800 KiB
07-large-cycle-2-04.txt AC 65 ms 15424 KiB
07-large-cycle-2-05.txt AC 1018 ms 72672 KiB
08-divide-01.txt AC 2 ms 3584 KiB
08-divide-02.txt AC 5 ms 3748 KiB
08-divide-03.txt AC 2 ms 3528 KiB
08-divide-04.txt AC 2 ms 3616 KiB
08-divide-05.txt AC 2 ms 3556 KiB
09-large-ans-01.txt AC 284 ms 43096 KiB
09-large-ans-02.txt AC 82 ms 11524 KiB
09-large-ans-03.txt AC 415 ms 57692 KiB
09-large-ans-04.txt AC 127 ms 26208 KiB
09-large-ans-05.txt AC 72 ms 14404 KiB