QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#77445 | #5508. Job for a Hobbit | heno239 | AC ✓ | 15ms | 12400kb | C++17 | 9.4kb | 2023-02-14 17:28:01 | 2023-02-14 17:28:04 |
Judging History
answer
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
//ll mod = 1;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
template<typename T>
void chmin(T& a, T b) {
a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
rep(i, v.size()) {
if (i > 0)cout << " "; cout << v[i];
}
cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
//if (x == 0)return 0;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
//mod should be <2^31
struct modint {
int n;
modint() :n(0) { ; }
modint(ll m) {
if (m < 0 || mod <= m) {
m %= mod; if (m < 0)m += mod;
}
n = m;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= (int)mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += (int)mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
ll gcd(ll a, ll b) {
a = abs(a); b = abs(b);
if (a < b)swap(a, b);
while (b) {
ll r = a % b; a = b; b = r;
}
return a;
}
using ld = long double;
//typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-10;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
if (loc >= v.size())v.resize(loc + 1, 0);
v[loc] += val;
}
/*const int mn = 2000005;
bool isp[mn];
vector<int> ps;
void init() {
fill(isp + 2, isp + mn, true);
for (int i = 2; i < mn; i++) {
if (!isp[i])continue;
ps.push_back(i);
for (int j = 2 * i; j < mn; j += i) {
isp[j] = false;
}
}
}*/
//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
auto res = st.lower_bound(val);
if (res == st.begin())return st.end();
res--; return res;
}
//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
auto res = st.lower_bound(val);
return res;
}
using mP = pair<modint, modint>;
mP operator+(mP a, mP b) {
return { a.first + b.first,a.second + b.second };
}
mP operator+=(mP& a, mP b) {
a = a + b; return a;
}
mP operator-(mP a, mP b) {
return { a.first - b.first,a.second - b.second };
}
mP operator-=(mP& a, mP b) {
a = a - b; return a;
}
LP operator+(LP a, LP b) {
return { a.first + b.first,a.second + b.second };
}
LP operator+=(LP& a, LP b) {
a = a + b; return a;
}
LP operator-(LP a, LP b) {
return { a.first - b.first,a.second - b.second };
}
LP operator-=(LP& a, LP b) {
a = a - b; return a;
}
mt19937 mt(time(0));
const string drul = "DRUL";
string senw = "SENW";
//DRUL,or SENW
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------
void solve() {
int n, k; cin >> n >> k;
vector<vector<int>> v(n + 2);
rep1(i, n) {
rep(j, k) {
int x; cin >> x;
v[i].push_back(x);
}
}
if (k == 1) {
cout << "TAK\n";
cout << 0 << "\n";
return;
}
vector<int> alcs;
rep1(i, n)rep(j, k)alcs.push_back(v[i][j]);
vector<P> qcs;
sort(all(alcs));
rep(i, alcs.size()) {
int cnt = 1;
while (i + 1 < alcs.size() && alcs[i] == alcs[i + 1]) {
i++; cnt++;
}
while (cnt > k) {
qcs.push_back({ k,alcs[i] });
cnt -= k;
}
qcs.push_back({ cnt,alcs[i] });
}
if (qcs.size() > n + 2) {
cout << "NIE\n"; return;
}
sort(all(qcs),greater<P>());
vector<P> ans;
auto ord = [&](int i, int j) {
assert(abs(i - j) == 1);
assert(v[i].size());
assert(v[j].size() < k);
ans.push_back({ i,j });
v[j].push_back(v[i].back());
v[i].pop_back();
};
auto segord = [&](int i, int j) {
while (i < j) {
ord(i, i + 1); i++;
}
while (i > j) {
ord(i, i - 1); i--;
}
};
auto segr = [&](int l) {
while (l + 1 < v.size() && v[l + 1].size() < k) {
ord(l, l + 1); l++;
}
};
per1(i, n) {
rep(j, k)ord(i, i + 1);
}
rep(i, qcs.size()) {
int num = qcs[i].first;
int col = qcs[i].second;
//cout << "hello\n";
//cout << i << " " << num << " " << col << "\n";
vector<int> prs;
vector<int> ssrs;
rep(j, i) {
int rest = k - v[j].size();
rep(_, rest)ssrs.push_back(j);
}
int stmp = 0;
while (v[i].size()) {
int to = ssrs[stmp]; stmp++;
prs.push_back(to);
if (v[i].back() == col)num--;
segord(i, to);
}
rep(_, num) {
//cout << "now " << "\n";
//rep(j, v.size())coutarray(v[j]);
int ci = i + 1;
while (ci < v.size() && v[ci].size() < k)ci++;
vector<int> rs;
rep(j, i) {
int rest = k - v[j].size();
rep(_, rest)rs.push_back(j);
}
for (int j = i + 1; j < ci; j++) {
int rest = k - 1 - v[j].size();
rep(_, rest)rs.push_back(j);
}
int rest = k - v[i].size();
rep(_, rest)rs.push_back(i);
for (int j = i + 1; j < ci; j++) {
rs.push_back(j);
}
int chk = -1, loc = -1;
for (int j = n + 1; j > i; j--) {
rep(k, v[j].size()) {
if (v[j][k] == col) {
chk = j; loc = k;
}
}
}
assert(chk >= 0);
if (chk <= ci) {
int tmp = 0;
while (v[chk].back() != col) {
segord(chk, rs[tmp]);
tmp++;
}
segord(chk, i);
per(i, tmp) {
segr(rs[i]);
}
}
else {
int tmp = 0;
while (v[chk].back() != col) {
segord(ci, rs[tmp]); tmp++;
for (int j = ci + 1; j <= chk; j++) {
ord(j, j - 1);
}
}
rep(_, 2) {
segord(ci, rs[tmp]); tmp++;
for (int j = ci + 1; j < chk; j++) {
ord(j, j - 1);
}
}
ord(chk, chk - 1);
while (v[chk].size() < k - 2) {
if (chk - 2 >= ci) {
segord(chk - 2, chk);
for (int j = chk - 3; j >= ci; j--) {
ord(j, j + 1);
}
tmp--;
segord(rs[tmp], ci);
}
else {
tmp--;
segord(rs[tmp], chk);
}
}
if (v[chk].size() > k - 2) {
if (chk - 2 >= ci) {
segord(ci, rs[tmp]); tmp++;
for (int j = ci + 1; j <= chk - 2; j++) {
ord(j, j - 1);
}
segord(chk, chk - 2);
}
else {
segord(chk, rs[tmp]); tmp++;
}
}
int cur = chk - 1;
while (cur > ci) {
segord(cur - 1, cur + 1);
segord(cur - 1, cur + 1);
segord(cur, cur - 1);
cur--;
}
tmp--;
segord(rs[tmp], cur + 1);
tmp--;
segord(rs[tmp], cur + 1);
segord(cur, i);
while (tmp > 0) {
tmp--; segr(rs[tmp]);
}
}
for (int j = v.size() - 2; j > i; j--) {
while (v[j].size() && v[j + 1].size() < k) {
segr(j);
}
}
}
while (prs.size()) {
int loc = prs.back(); prs.pop_back();
if (v[loc].back() == col) {
segord(loc, i);
}
else {
segr(loc);
}
}
}
cout << "TAK\n";
cout << ans.size() << "\n";
rep(i, ans.size()) {
cout << ans[i].first << " " << ans[i].second << "\n";
}
//cout << "last " << "\n";
//rep(i, n + 2)coutarray(v[i]);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//while(true)
int t; cin >> t; rep(i, t)
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 5ms
memory: 11608kb
input:
2 2 2 1 2 2 1 1 4 1 2 3 4
output:
TAK 16 2 3 2 3 1 2 1 2 2 1 2 1 1 0 1 2 3 2 2 1 1 0 2 3 3 2 2 1 3 2 2 1 NIE
result:
ok Correct! Used 16 swaps
Test #2:
score: 0
Accepted
time: 8ms
memory: 11724kb
input:
25 2 7 4 1 2 2 3 2 4 4 4 6 4 2 2 5 2 5 6 5 5 3 3 1 6 5 2 5 2 8 1 4 2 1 4 1 2 1 1 3 4 4 2 2 1 2 2 4 1 1 5 2 1 5 2 2 2 10 3 5 4 5 5 2 1 3 5 2 5 2 2 1 5 1 3 3 4 2 2 8 2 4 3 3 4 2 1 2 5 1 4 1 2 6 3 4 2 9 4 3 4 3 4 2 4 1 2 2 4 4 2 2 3 3 3 4 2 4 4 1 3 1 4 2 4 3 2 4 5 1 2 2 2 4 3 2 2 7 1 2 4 5 2 5 2 4 5 5 ...
output:
NIE NIE TAK 164 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2 1 1 0 2 1 2 1 2 1 1 0 1 2 1 2 2 1 2 1 2 1 2 1 1 0 1 2 1 2 1 2 2 1 2 1 2 1 2 1 2 1 1 0 1 2 1 2 1 2 1 2 3 2 2 1 1 0 2 3 3 2 2 1 3 2 2 1 3 2 2 1 3 2 2 1 3 2 2 1 3 2 2 1 3 2 2 1 1 0 1 2 2 3 1 2 2 3 1 2 2 3 1 2 2 3 1 2 2 3 ...
result:
ok Correct! Used 1238 swaps
Test #3:
score: 0
Accepted
time: 9ms
memory: 11860kb
input:
1 50 10 2 2 2 2 2 1 2 1 1 2 2 1 2 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 1 1 2 2 1 2 2 2 1 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 1 1 1 2 1 2 2 1 2 1 1 2 2 1 1 2 1 2 2 1 1 2 2 2 1 1 2 1 2 2 2 2 1 1 2 1 1 2 2 2 1 2 1 1 2 1 1 2 2 1 2 2 1 1 1 1 1 1 2 2 1 2 2 2 1 1 1 2 2 2 1 2 2 1 1 2 2 1 2 1 2 1 1 1 1 2 2 1 2 1 1 2 2 ...
output:
TAK 49461 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 46 47 46 47 46 47 46 47 46 47 46 47 46 47 46 47 46...
result:
ok Correct! Used 49461 swaps
Test #4:
score: 0
Accepted
time: 15ms
memory: 12400kb
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 102672 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 46 47 46 47 46 47 46 47 46 47 46 47 46 47 46 47 4...
result:
ok Correct! Used 102672 swaps
Test #5:
score: 0
Accepted
time: 9ms
memory: 11784kb
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 2976 10 11 10 11 10 11 10 11 10 11 10 11 10 11 9 10 9 10 9 10 9 10 9 10 9 10 9 10 8 9 8 9 8 9 8 9 8 9 8 9 8 9 7 8 7 8 7 8 7 8 7 8 7 8 7 8 6 7 6 7 6 7 6 7 6 7 6 7 6 7 5 6 5 6 5 6 5 6 5 6 5 6 5 6 4 5 4 5 4 5 4 5 4 5 4 5 4 5 3 4 3 4 3 4 3 4 3 4 3 4 3 4 2 3 2 3 2 3 2 3 2 3 2 3 2 3 1 2 1 2 1 2 ...
result:
ok Correct! Used 6967 swaps
Test #6:
score: 0
Accepted
time: 3ms
memory: 11984kb
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 1930 25 26 25 26 25 26 25 26 25 26 25 26 25 26 25 26 25 26 25 26 24 25 24 25 24 25 24 25 24 25 24 25 24 25 24 25 24 25 24 25 23 24 23 24 23 24 23 24 23 24 23 24 23 24 23 24 23 24 23 24 22 23 22 23 22 23 22 23 22 23 22 23 22 23 22 23 22 23 22 23 21 22 21 22 21 22 21 22 21 22 21 22 21 22 21 22 21 ...
result:
ok Correct! Used 43914 swaps
Test #7:
score: 0
Accepted
time: 13ms
memory: 12360kb
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 94225 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 50 51 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 49 50 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 48 49 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 47 48 46 47 46 47 46 47 46 47 46 47 46 47 46 47 46 47 46...
result:
ok Correct! Used 94225 swaps
Test #8:
score: 0
Accepted
time: 7ms
memory: 11624kb
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