QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#793578 | #5116. Contests | Awei | WA | 2ms | 4688kb | C++20 | 2.3kb | 2024-11-29 21:17:08 | 2024-11-29 21:17:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve(){
int n, m;
cin >> n >> m;
vector<vector<int>> a(m + 1, vector<int>(n + 1)), pos(m + 1, vector<int>(n + 1));
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
pos[i][a[i][j]] = j;
}
}
vector<vector<vector<int>>> f(6, vector<vector<int>>(n + 1, vector<int>(21, n)));
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
f[i][j][0] = pos[i][j];
}
}
for(int i = 1; i <= m; i++){
for(int k = 1; k <= m; k++){
int minn = n;
for(int j = n; j >= 1; j--){
minn = min(minn, pos[i][a[k][j]]);
f[i][j][0] = min(f[i][j][0], minn);
}
}
}
for(int k = 1; k <= 20; k++){
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
f[i][j][k] = f[i][j][k - 1];
for(int l = 1; l <= m; l++){
f[i][j][k] = min(f[i][j][k], f[i][a[l][f[l][j][k - 1]]][k - 1]);
}
}
}
}
int q;
cin >> q;
while(q--){
int x, y;
cin >> x >> y;
int L = 0;
bool ok = false;
vector<int> now(m + 1);
for(int i = 1; i <= m; i++){
now[i] = pos[i][x];
if(f[i][x][20] <= pos[i][y]) ok = true;
if(pos[i][x] < pos[i][y]) L = -1;
}
if(!ok){
cout << "-1\n";
continue;
}
for(int k = 20; k >= 0; k--){
vector to(m + 1, n);
bool flag = false;
for(int i = 1; i <= m; i++){
for(int l = 1; l <= m; l++){
to[i] = min(to[i], f[i][a[l][now[l]]][k]);
if(to[i] <= pos[i][y]) flag = true;
if(flag) break;
}
if(flag) break;
}
if(!flag){
now = to;
L += (1 << k);
}
}
cout << L + 2 << "\n";
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while(T--){
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3824kb
input:
6 2 1 3 2 5 4 6 2 1 4 3 6 5 4 1 4 5 3 6 1 5 2
output:
1 2 5 3
result:
ok 4 number(s): "1 2 5 3"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 4688kb
input:
998 5 1 2 6 3 4 5 7 11 8 9 14 12 13 10 15 16 18 19 20 17 23 22 21 26 24 25 27 29 30 28 32 31 34 33 37 38 39 35 40 36 41 43 42 46 44 48 45 47 51 49 50 56 52 57 53 54 55 62 58 59 63 64 60 65 66 61 67 69 70 68 71 73 74 75 72 78 77 79 82 76 81 85 84 80 86 87 83 88 90 91 89 92 96 98 94 95 97 101 93 103 1...
output:
-1 -1 1 1 1 26 1 -1 -1 1 25 -1 34 1 1 1 109 1 1 1 1 -1 1 -1 133 1 1 1 40 -1 1 1 -1 -1 41 -1 1 1 1 1 1 1 1 1 22 -1 -1 1 1 -1 -1 1 1 -1 1 1 -1 1 -1 1 1 1 4 115 1 1 -1 45 -1 1 1 24 -1 1 1 1 -1 -1 -1 1 36 -1 -1 45 -1 1 -1 -1 1 1 1 76 -1 -1 29 -1 -1 1 -1 1 1 2 1 1 1 17 -1 1 -1 1 100 -1 -1 -1 1 1 10 1 -1 ...
result:
wrong answer 1st numbers differ - expected: '94', found: '-1'