QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729734 | #9564. Hey, Have You Seen My Kangaroo? | ucup-team5062# | TL | 4ms | 16968kb | C++20 | 2.7kb | 2024-11-09 17:42:58 | 2024-11-09 17:43:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int Answer[1 << 18];
string C[1 << 18];
string S;
// Other than input
bool Used[1 << 18];
bool Cycle[1 << 18];
int P[1 << 18];
int Dist[1 << 18];
int Count[1 << 18];
vector<int> G[1 << 18];
pair<int, int> Simulation(int px, int py, int cl, int cr) {
for (int i = cl; i < cr; i++) {
int idx = (i >= K ? i - K : i);
if (S[idx] == 'U') {
if (px >= 1 && C[px - 1][py] == '1') px--;
}
if (S[idx] == 'D') {
if (px <= H - 2 && C[px + 1][py] == '1') px++;
}
if (S[idx] == 'L') {
if (py >= 1 && C[px][py - 1] == '1') py--;
}
if (S[idx] == 'R') {
if (py <= W - 2 && C[px][py + 1] == '1') py++;
}
}
return make_pair(px, py);
}
void dfs(int pos) {
Dist[pos] = 0;
for (int to : G[pos]) {
dfs(to);
Dist[pos] = max(Dist[pos], Dist[to] + 1);
}
}
int main() {
// Step 1. Input
cin >> H >> W >> K;
cin >> S;
for (int i = 0; i < H; i++) cin >> C[i];
for (int i = 1; i <= H * W; i++) Answer[i] = (1 << 30);
// Step 2. Brute Force
for (int i = 0; i < K; i++) {
for (int j = 0; j <= H * W + 2; j++) Used[j] = false;
for (int j = 0; j <= H * W + 2; j++) Cycle[j] = false;
for (int j = 0; j <= H * W + 2; j++) G[j].clear();
for (int j = 0; j <= H * W + 2; j++) Count[j] = 0;
for (int j = 0; j < H * W; j++) {
if (C[j / W][j % W] == '0') continue;
pair<int, int> x = Simulation(j / W, j % W, 0, i);
Used[x.first * W + x.second] = true;
}
// Make Graph
int root = 0;
for (int j = 0; j < H * W; j++) {
if (Used[j] == false) continue;
pair<int, int> L = Simulation(j / W, j % W, i, i + K);
P[j] = L.first * W + L.second;
root = j;
}
// Get Cycles
int cx = root;
for (int j = 0; j < 3 * H * W; j++) {
cx = P[cx];
if (j >= 2 * H * W) Cycle[cx] = true;
}
for (int j = 0; j < H * W; j++) {
if (Used[j] == false || Cycle[j] == true) continue;
G[P[j]].push_back(j);
}
// Get Distance
for (int j = 0; j < H * W; j++) {
if (Cycle[j] == false) continue;
dfs(j);
}
// Get Answer
for (int j = 0; j < H * W; j++) {
if (Used[j] == false) continue;
int dst = Dist[j];
if (Cycle[j] == true) dst = H * W + 1;
Count[dst] += 1;
}
for (int j = H * W + 1; j >= 1; j--) Count[j - 1] += Count[j];
// Update Answer
for (int j = 0; j <= H * W; j++) {
Answer[Count[j]] = min(Answer[Count[j]], j * K + i);
}
}
// Step 3. Output
for (int i = 2; i <= H * W; i++) Answer[i] = min(Answer[i], Answer[i - 1]);
for (int i = 1; i <= H * W; i++) {
if (Answer[i] == (1 << 30)) printf("-1\n");
else printf("%d\n", Answer[i]);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 16596kb
input:
3 3 6 ULDDRR 010 111 010
output:
-1 4 2 1 0 0 0 0 0
result:
ok 9 numbers
Test #2:
score: 0
Accepted
time: 4ms
memory: 16968kb
input:
3 3 6 ULDDRR 010 111 011
output:
7 4 2 1 1 0 0 0 0
result:
ok 9 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 16792kb
input:
1 5 1 R 11111
output:
4 3 2 1 0
result:
ok 5 number(s): "4 3 2 1 0"
Test #4:
score: -100
Time Limit Exceeded
input:
1 200000 200 RDRLDRULURDLDRULLRULLRRULRULRDLLDLRUDDLRURLURLULDRUUURDLUDUDLLLLLURRDURLUDDRRLRRURUUDDLLDDUUUDUULRLRUDULRRUURUDDDDLULULLLLLLLLLLLUDURLURLRLLRRRURUURLUDULDUULRRLULLRUDRDRUUDDRUDRDLLDLURDDDURLUULDRRDLDD 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111...