QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#119155 | #5508. Job for a Hobbit | hos_lyric | AC ✓ | 29ms | 8384kb | C++14 | 7.9kb | 2023-07-05 01:54:48 | 2023-07-05 01:54:49 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
constexpr int MAX = 60;
int N, K;
int A[MAX][MAX];
int L[MAX];
int B[MAX][MAX];
vector<pair<int, int>> ans;
void oper(int i, int j) {
assert(0 <= i); assert(i <= N + 1);
assert(0 <= j); assert(j <= N + 1);
assert(abs(j - i) == 1);
if (L[i] > 0 && L[j] < K) {
ans.emplace_back(i, j);
B[j][L[j]++] = B[i][--L[i]];
}
}
void out() {
#ifdef LOCAL
cerr << string(8, '-') << endl;
for (int i = 0; i <= N + 1; ++i) {
cerr << i << ": ";
for (int k = 0; k < L[i]; ++k) {
cerr << B[i][k] << " ";
}
cerr << endl;
}
cerr << string(8, '-') << endl;
#endif
}
void judge() {
vector<vector<int>> a(N + 2);
for (int i = 1; i <= N; ++i) {
for (int k = 0; k < K; ++k) {
a[i].push_back(A[i][k]);
}
}
for (const auto &op : ans) {
const int i = op.second;
const int j = op.first;
// cerr<<"judge "<<a<<"; "<<i<<" -> "<<j<<endl;
assert(0 <= i); assert(i <= N + 1);
assert(0 <= j); assert(j <= N + 1);
assert(abs(j - i) == 1);
assert((int)a[i].size() > 0);
assert((int)a[j].size() < K);
a[j].push_back(a[i].back());
a[i].pop_back();
}
for (int i = 0; i <= N + 1; ++i) if (!a[i].empty()) {
for (const int val : a[i]) {
assert(a[i][0] == val);
}
}
}
void move(int ti, int tk, int si, int val) {
#ifdef LOCAL
cerr<<"move ("<<ti<<", "<<tk<<") "<<si<<" "<<val<<endl;
#endif
out();
if (ti == si) {
int sk = -1;
for (int k = tk; k < L[ti]; ++k) {
if (B[ti][k] == val) {
sk = k;
break;
}
}
assert(~sk);
if (tk == sk) {
return;
}
for (int i = N; i > ti; --i) {
for (int k = 0; k < K; ++k) oper(i, i + 1);
}
// cerr<<"sk = "<<sk<<endl;
assert(L[ti] == K);
assert(L[ti + 1] == 0);
assert(L[ti + 2] == 0);
for (; L[ti] > sk; ) {
oper(ti, ti + 1);
oper(ti + 1, ti + 2);
}
for (; L[ti] > tk; ) {
oper(ti, ti + 1);
}
oper(ti + 2, ti + 1);
oper(ti + 1, ti);
// ue tsumeru
for (int i = ti + 1; i <= ti + 2; ++i) {
for (int j = i; j > ti; --j) {
for (int k = 0; k < K; ++k) oper(j, j - 1);
}
}
} else {
for (int i = ti + 1; i <= ti + 2; ++i) {
if (si == i) {
for (int k = 0; k < K; ++k) oper(i, i + 1);
si = i + 1;
}
}
out();
assert(ti + 3 <= si);
// bring empty
for (int i = 1; i < si; ++i) {
for (int k = 0; k < K; ++k) {
oper(i, i - 1);
if (i >= 2) {
oper(i - 1, i - 2);
}
}
}
out();
// swap rows
for (int i = si; i > ti + 3; --i) {
assert(L[i - 3] == K);
assert(L[i - 2] == 0);
assert(L[i - 1] == 0);
assert(L[i] == K);
for (int k = 0; k < K; ++k) {
oper(i - 3, i - 2);
}
oper(i - 2, i - 1);
for (int k = 0; k < K; ++k) {
oper(i, i - 1);
oper(i - 1, i - 2);
oper(i - 2, i - 3);
}
for (int k = 0; k < K - 1; ++k) {
oper(i - 2, i - 1);
oper(i - 1, i);
}
oper(i - 1, i);
for (int k = 0; k < K; ++k) oper(i - 3, i - 2);
for (int k = 0; k < K; ++k) oper(i - 2, i - 1);
}
out();
// move to top
si = ti + 3;
int sk = -1;
for (int k = 0; k < L[si]; ++k) {
if (B[si][k] == val) {
sk = k;
break;
}
}
assert(~sk);
for (; L[si] > sk; ) oper(si, si - 1);
oper(si - 1, si - 2);
for (int k = 0; k < K; ++k) oper(si - 1, si);
oper(si - 2, si - 1);
oper(si - 1, si);
// go to target
for (; L[ti] > tk; ) oper(ti, ti + 1);
if (L[ti + 1] == K) oper(ti + 1, ti + 2);
oper(si, si - 1);
oper(si - 1, si - 2);
oper(si - 2, si - 3);
// ue tsumeru
for (int i = ti + 1; i <= ti + 3; ++i) {
for (int j = i; j > ti; --j) {
for (int k = 0; k < K; ++k) oper(j, j - 1);
}
}
}
out();
#ifdef LOCAL
cerr<<"move done"<<endl;
#endif
}
void solve() {
ans.clear();
out();
// flatten
for (int i = N; i >= 0; --i) {
for (int k = 0; k < K; ++k) {
for (int j = i; j <= N; ++j) {
oper(j, j + 1);
}
}
}
out();
for (int i = 0; i < N; ++i) {
// ?
for (int k = 0; k < K; ++k) oper(i + 2, i + 1);
for (int k = 0; k < K; ++k) oper(i + 1, i);
for (int k = 0; k < K; ++k) {
// shita tsumeru
for (int j = N + 1; j > i; --j) {
for (int jj = j; jj <= N && jj <= j + 2; ++jj) {
for (int l = 0; l < K; ++l) oper(jj, jj + 1);
}
}
const int val = A[i + 1][K - 1 - k];
for (int j = 0; j <= N + 1; ++j) {
for (int l = 0; l < L[j]; ++l) {
if (make_pair(i, k) <= make_pair(j, l) && val == B[j][l]) {
move(i, k, j, val);
goto found;
}
}
}
assert(false);
found:{}
}
}
// [0, N) -> [1, N]
for (int i = N; --i >= 0; ) {
for (int k = 0; k < K; ++k) {
oper(i, i + 1);
}
}
out();
for (int i = 1; i <= N; ++i) {
assert(L[i] == K);
for (int k = 0; k < K; ++k) {
assert(A[i][k] == B[i][k]);
}
}
assert((int)ans.size() <= 1'000'000);
reverse(ans.begin(), ans.end());
puts("TAK");
printf("%d\n", (int)ans.size());
for (const auto &op : ans) {
printf("%d %d\n", op.second, op.first);
}
#ifdef LOCAL
judge();
#endif
}
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; ++i) {
for (int k = 0; k < K; ++k) {
scanf("%d", &A[i][k]);
}
}
if (K == 1) {
puts("TAK");
puts("0");
} else {
map<int, vector<pair<int, int>>> ikss;
for (int i = 1; i <= N; ++i) {
for (int k = 0; k < K; ++k) {
ikss[A[i][k]].emplace_back(i, k);
}
}
// (freq, key)
vector<pair<int, int>> target;
for (const auto &kv : ikss) {
const int len = kv.second.size();
const int num = (len + K - 1) / K;
for (int p = 0; p < num; ++p) {
target.emplace_back(min(K * (p + 1), len) - K * p, kv.first);
}
}
// cerr<<"target = "<<target<<endl;
if ((int)target.size() <= N + 2) {
fill(L, L + (N + 2), 0);
for (int i = 0; i < (int)target.size(); ++i) {
L[i] = target[i].first;
fill(B[i], B[i] + L[i], target[i].second);
}
solve();
} else {
puts("NIE");
}
}
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3656kb
input:
2 2 2 1 2 2 1 1 4 1 2 3 4
output:
TAK 40 1 0 1 0 2 1 2 1 1 2 1 2 2 3 2 3 3 2 3 2 2 1 2 1 1 2 2 3 1 2 0 1 0 1 1 2 2 3 2 1 1 0 1 0 3 2 2 1 3 2 1 2 2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 2 1 1 2 2 1 2 1 2 1 1 1 1 2 2 1 2 1 1 2 2 ...
output:
TAK 46194 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 8 7 8 7 8 ...
result:
ok Correct! Used 46194 swaps
Test #4:
score: 0
Accepted
time: 29ms
memory: 7732kb
input:
1 50 10 33 28 16 37 35 47 28 35 31 21 47 40 33 25 16 40 50 25 13 33 10 14 48 38 1 38 32 43 28 18 11 16 1 51 4 45 16 31 27 41 52 18 32 51 17 31 38 48 49 47 5 24 20 48 51 20 29 32 35 20 39 18 21 45 10 30 11 5 32 32 5 46 19 39 30 26 15 17 15 8 15 15 21 25 41 28 6 8 6 20 47 28 34 12 44 16 48 49 52 47 23...
output:
TAK 399413 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 8 7 8 7 8...
result:
ok Correct! Used 399413 swaps
Test #5:
score: 0
Accepted
time: 3ms
memory: 3788kb
input:
5 10 1 11 2 10 3 1 12 8 4 5 7 10 7 11 5 6 5 2 1 2 8 7 1 9 10 6 4 6 8 4 6 2 12 9 12 3 10 5 11 4 7 9 11 4 2 9 3 9 6 7 5 11 1 10 6 11 7 6 7 12 1 1 5 3 2 3 4 7 12 3 8 11 9 12 9 8 1 12 12 4 10 1 2 10 7 9 8 12 8 10 7 4 1 1 10 7 3 5 1 11 6 11 5 2 4 12 6 12 8 3 8 6 8 12 7 4 1 11 8 6 7 6 2 5 9 3 6 12 10 4 9 ...
output:
TAK 0 TAK 8599 1 0 1 0 1 0 1 0 1 0 1 0 1 0 2 1 2 1 2 1 2 1 2 1 2 1 2 1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 4 3 4 3 4 3 4 3 4 3 4 3 4 3 5 4 5 4 5 4 5 4 5 4 5 4 5 4 6 5 6 5 6 5 6 5 6 5 6 5 6 5 7 6 7 6 7 6 7 6 7 6 7 6 7 6 8 7 8 7 8 7 8 7 8 7 8 7 8 7 9 8 9 8 9 8 9 8 9 8 9 8 9 8 10 9 10 9 10 9 10 9 10 9 10 9 10 ...
result:
ok Correct! Used 19799 swaps
Test #6:
score: 0
Accepted
time: 1ms
memory: 3856kb
input:
2 25 10 27 26 27 26 27 26 27 26 27 26 26 27 26 27 26 27 26 27 26 27 25 25 25 25 25 25 25 25 25 25 24 24 24 24 24 24 24 24 24 24 23 23 23 23 23 23 23 23 23 23 22 22 22 22 22 22 22 22 22 22 21 21 21 21 21 21 21 21 21 21 20 20 20 20 20 20 20 20 20 19 19 19 19 19 19 19 19 19 18 18 18 18 18 18 18 18 18 1...
output:
TAK 43934 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 8 7 8 7 8 ...
result:
ok Correct! Used 47272 swaps
Test #7:
score: 0
Accepted
time: 24ms
memory: 8384kb
input:
1 50 10 1 1 1 37 35 47 1 35 31 21 47 40 1 25 1 40 1 25 13 1 10 14 48 38 1 38 32 43 1 18 11 1 1 1 4 45 1 31 27 41 1 18 32 1 17 31 38 48 49 47 1 24 1 48 1 1 29 32 35 1 39 18 21 45 10 30 11 1 32 32 1 46 19 39 30 26 15 17 15 8 15 15 21 25 41 1 6 8 6 1 47 1 34 12 1 1 48 49 1 47 23 18 1 1 37 1 41 1 2 30 4...
output:
TAK 347457 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 7 6 8 7 8 7 8...
result:
ok Correct! Used 347457 swaps
Test #8:
score: 0
Accepted
time: 1ms
memory: 3616kb
input:
1 50 10 15 52 42 32 47 29 31 51 12 48 43 19 14 2 28 39 51 10 43 36 33 6 29 11 4 18 12 41 15 34 24 2 48 30 25 23 34 17 9 28 24 8 17 4 21 14 42 19 48 30 23 16 30 9 33 25 33 36 21 12 36 18 46 35 31 13 44 34 15 50 34 11 33 38 9 23 9 42 4 3 37 12 2 41 7 34 23 16 10 27 24 8 38 16 24 11 47 29 3 50 34 52 47...
output:
NIE
result:
ok Correct! Used 0 swaps