QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#101875 | #5446. 琪露诺的符卡交换 | hos_lyric# | 20 | 5ms | 4528kb | C++14 | 4.2kb | 2023-05-01 15:30:27 | 2024-05-26 02:49:40 |
Judging History
answer
#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; }
int N;
int A[210][210];
struct Op {
int i0, j0, i1, j1;
};
int ansLen;
Op ans[20010];
namespace sub1 {
bool validate() {
for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
if (A[i][0] != A[i][j]) return false;
}
return true;
}
void run() {
for (int i = 0; i < N; ++i) for (int j = i + 1; j < N; ++j) {
ans[ansLen++] = Op{i, j, j, i};
}
}
} // sub1
namespace sub2 {
vector<int> P, Q, J;
bool validate() {
assert(N >= 2);
P.assign(N, -1);
Q.assign(N, -1);
J.assign(N, -1);
for (int i = 0; i < N; ++i) {
vector<int> freq(N, 0);
for (int j = 0; j < N; ++j) {
++freq[A[i][j]];
}
P[i] = max_element(freq.begin(), freq.end()) - freq.begin();
if (freq[P[i]] == N) {
Q[i] = P[i];
J[i] = 0;
} else if (freq[P[i]] == N - 1) {
for (int j = 0; j < N; ++j) if (P[i] != A[i][j]) {
Q[i] = A[i][j];
J[i] = j;
break;
}
assert(~Q[i]);
} else {
return false;
}
}
cerr<<"[sub2] P = "<<P<<", Q = "<<Q<<", J = "<<J<<endl;
return true;
}
bool adj[210][210];
void run() {
vector<int> invP(N, -1), invQ(N, -1);
for (int i = 0; i < N; ++i) {
invP[P[i]] = i;
invQ[Q[i]] = i;
}
vector<vector<int>> jss(N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) if (J[i] != j) {
jss[i].push_back(j);
}
jss[i].push_back(J[i]);
}
for (int k = 0; k < N - 2; ++k) {
// size N-k
const int spe = invQ[A[k][jss[k][N - 1]]];
// cerr<<"k = "<<k<<", N-k = "<<N-k<<", spe = "<<spe<<endl;
// cerr<<" jss = "<<jss<<endl;
int pos = k + 1;
for (int i = k + 1; i < N; ++i) if (spe != i) {
ans[ansLen++] = Op{k, jss[k][pos++], i, jss[i][k]};
}
if (k != spe) {
assert(pos == N - 1);
ans[ansLen++] = Op{k, jss[k][pos++], spe, jss[spe][k]};
rotate(jss[spe].begin() + k, jss[spe].begin() + (N - 1), jss[spe].end());
}
}
}
} // sub1
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d", &N);
for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) {
scanf("%d", &A[i][j]);
--A[i][j];
}
ansLen = 0;
if (sub1::validate()) {
sub1::run();
} else if (sub2::validate()) {
sub2::run();
} else {
// HELP
puts("-1");
continue;
}
printf("%d\n", ansLen);
for (int h = 0; h < ansLen; ++h) {
printf("%d %d %d %d\n", ans[h].i0 + 1, ans[h].j0 + 1, ans[h].i1 + 1, ans[h].j1 + 1);
}
#ifdef LOCAL
int oped[210][210]={};
for(int h=0;h<ansLen;++h){
assert(!oped[ans[h].i0][ans[h].j0]++);
assert(!oped[ans[h].i1][ans[h].j1]++);
swap(A[ans[h].i0][ans[h].j0],A[ans[h].i1][ans[h].j1]);
}
cerr<<"A = "<<endl;for(int i=0;i<N;++i)pv(A[i],A[i]+N);
for(int i=0;i<N;++i){
vector<int>freq(N,0);
for(int j=0;j<N;++j)++freq[A[i][j]];
for(int a=0;a<N;++a)assert(freq[a]==1);
}
#endif
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 3ms
memory: 3980kb
input:
7 132 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 ...
output:
8646 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1 1 6 6 1 1 7 7 1 1 8 8 1 1 9 9 1 1 10 10 1 1 11 11 1 1 12 12 1 1 13 13 1 1 14 14 1 1 15 15 1 1 16 16 1 1 17 17 1 1 18 18 1 1 19 19 1 1 20 20 1 1 21 21 1 1 22 22 1 1 23 23 1 1 24 24 1 1 25 25 1 1 26 26 1 1 27 27 1 1 28 28 1 1 29 29 1 1 30 30 1 1 31 31 1 1 32 32 1 1...
result:
ok your solution is correct.
Test #2:
score: 0
Accepted
time: 2ms
memory: 3940kb
input:
8 14 13 13 13 13 13 13 13 13 13 13 13 13 13 13 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 14 14 14 14 14 14 14 14 14 14 14 14 14 14 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10 10 10 2 2 2 2 2 2 2 2 2 2 2 2 2 2 9...
output:
91 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1 1 6 6 1 1 7 7 1 1 8 8 1 1 9 9 1 1 10 10 1 1 11 11 1 1 12 12 1 1 13 13 1 1 14 14 1 2 3 3 2 2 4 4 2 2 5 5 2 2 6 6 2 2 7 7 2 2 8 8 2 2 9 9 2 2 10 10 2 2 11 11 2 2 12 12 2 2 13 13 2 2 14 14 2 3 4 4 3 3 5 5 3 3 6 6 3 3 7 7 3 3 8 8 3 3 9 9 3 3 10 10 3 3 11 11 3 3 12 12 3...
result:
ok your solution is correct.
Test #3:
score: 0
Accepted
time: 2ms
memory: 3968kb
input:
4 82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1...
output:
3321 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1 1 6 6 1 1 7 7 1 1 8 8 1 1 9 9 1 1 10 10 1 1 11 11 1 1 12 12 1 1 13 13 1 1 14 14 1 1 15 15 1 1 16 16 1 1 17 17 1 1 18 18 1 1 19 19 1 1 20 20 1 1 21 21 1 1 22 22 1 1 23 23 1 1 24 24 1 1 25 25 1 1 26 26 1 1 27 27 1 1 28 28 1 1 29 29 1 1 30 30 1 1 31 31 1 1 32 32 1 1...
result:
ok your solution is correct.
Test #4:
score: 0
Accepted
time: 4ms
memory: 4192kb
input:
8 3 1 1 1 3 3 3 2 2 2 3 1 1 1 3 3 3 2 2 2 1 1 11 5 5 5 5 5 5 5 5 5 5 5 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 9 9 9 9 9 9 9 9 9 9 9 4 4 4 4 4 4 4 4 4 4 4 11 11 11 11 11 11 11 11 11 11 11 2 2 2 2 2 2 2 2 2 2 2 6 6 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10 10 10 7 7 7 7 7...
output:
3 1 2 2 1 1 3 3 1 2 3 3 2 3 1 2 2 1 1 3 3 1 2 3 3 2 0 55 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1 1 6 6 1 1 7 7 1 1 8 8 1 1 9 9 1 1 10 10 1 1 11 11 1 2 3 3 2 2 4 4 2 2 5 5 2 2 6 6 2 2 7 7 2 2 8 8 2 2 9 9 2 2 10 10 2 2 11 11 2 3 4 4 3 3 5 5 3 3 6 6 3 3 7 7 3 3 8 8 3 3 9 9 3 3 10 10 3 3 11 11 3 4 5 5 4 4 6 6 4...
result:
ok your solution is correct.
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #5:
score: 0
Wrong Answer
time: 5ms
memory: 4528kb
input:
5 17 9 9 9 9 9 9 9 9 9 9 9 9 9 2 9 9 9 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 2 2 2 2 2 2 2 2 2 2 2 2 11 2 2 2 2 4 4 4 4 4 4 10 4 4 4 4 4 4 4 4 4 4 10 10 10 10 10 10 8 10 10 10 10 10 10 10 10 10 10 12 12 12 12 12 12 12 12 12 12 12 12 14 12 12 12 12 14 14 14 14 14 14 14 14 14 14 14 12 14 14 14 14 14 16 16...
output:
135 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1 1 6 6 1 1 7 7 1 1 8 8 1 1 9 9 1 1 10 10 1 1 11 11 1 1 12 12 1 1 13 13 1 1 15 14 1 1 16 15 1 1 17 16 1 1 14 17 1 2 3 3 2 2 4 4 2 2 5 5 2 2 6 6 2 2 7 7 2 2 8 8 2 2 9 9 3 2 10 10 2 2 11 11 3 2 12 12 3 2 13 13 2 2 14 14 2 2 15 15 2 2 16 16 2 2 17 17 2 3 4 4 3 3 5 5 3 ...
result:
wrong answer exist a kind of card which a person don't hold.
Subtask #3:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%