QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#666386 | #5116. Contests | Repeater# | WA | 0ms | 3560kb | C++20 | 2.0kb | 2024-10-22 18:07:31 | 2024-10-22 18:07:42 |
Judging History
answer
#include <bits/stdc++.h>
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m; std::cin >> n >> m;
std::vector a(m, std::vector<int>(n)), rk(m, std::vector<int>(n));
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
std::cin >> a[i][j], a[i][j]--;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
rk[i][a[i][j]] = j;
int log = std::__lg(n) + 1;
std::vector f(n, std::vector(log + 1, std::vector<int>(m, n)));
for(int x = 0; x < m; x++)
{
for(int y = 0; y < m; y++) // x -> y
{
std::vector<int> min(n);
min[n - 1] = rk[y][a[x][n - 1]];
for(int i = n - 2; i >= 0; i--)
min[i] = std::min(min[i + 1], rk[y][a[x][i]]);
for(int i = 0; i < n - 1; i++) f[a[x][i]][0][y] = std::min(f[a[x][i]][0][y], min[i + 1]);
}
}
for(int j = 1; j <= log; j++)
for(int i = 0; i < n; i++)
for(int x = 0; x < m; x++)
for(int y = 0; y < m; y++)
f[i][j][x] = std::min(f[i][j - 1][x], f[a[y][f[i][j - 1][y]]][j - 1][x]);
int q; std::cin >> q;
while(q--)
{
int x, y; std::cin >> x >> y;
x--, y--;
bool ok = false;
for(int i = 0; i < m; i++)
if(rk[i][x] < rk[i][y])
ok = true;
if(ok)
{
std::cout << 1 << "\n";
continue;
}
std::vector<int> cur(m);
for(int i = 0; i < m; i++) cur[i] = rk[i][x];
int ans = 0;
for(int i = log; i > 0; i--)
{
auto tmp = cur;
for(int j = 0; j < m; j++)
for(int k = 0; k < m; k++)
tmp[k] = std::min(f[a[j][cur[j]]][i][k], tmp[k]);
bool flg = true;
for(int i = 0; i < m; i++)
if(tmp[i] < rk[i][y]) flg = false;
if(flg)
{
ans |= 1 << i;
cur = tmp;
}
}
auto tmp = cur;
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
tmp[j] = std::min(tmp[j], f[a[i][cur[i]]][0][j]);
cur = tmp;
ok = false;
for(int i = 0; i < m; i++)
if(cur[i] < rk[i][y]) ok = true;
if(!ok) std::cout << -1 << "\n";
else std::cout << ans + 2 << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3560kb
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 -1 -1
result:
wrong answer 3rd numbers differ - expected: '5', found: '-1'