QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729880 | #9564. Hey, Have You Seen My Kangaroo? | ucup-team5062# | WA | 2116ms | 31212kb | C++20 | 3.5kb | 2024-11-09 17:58:30 | 2024-11-09 17:58:31 |
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];
// Simulation
int V1[1 << 18];
int V2[1 << 18];
int GetNext(int px, int py, char c) {
if (c == 'U') {
if (px >= 1 && C[px - 1][py] == '1') px--;
}
if (c == 'D') {
if (px <= H - 2 && C[px + 1][py] == '1') px++;
}
if (c == 'L') {
if (py >= 1 && C[px][py - 1] == '1') py--;
}
if (c == 'R') {
if (py <= W - 2 && C[px][py + 1] == '1') py++;
}
return px * W + py;
}
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() {
int DEBUG = 1;
// Step 1. Input
cin >> H >> W >> K;
if (DEBUG == 1) {
cin >> S;
for (int i = 0; i < H; i++) cin >> C[i];
}
if (DEBUG == 2) {
for (int i = 0; i < K; i++) {
string str = "LRUD";
S += str[rand() % 4];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) C[i] += ('0' + (rand() % 100 < 99 ? 1 : 0));
}
}
for (int i = 1; i <= H * W; i++) Answer[i] = (1 << 30);
// Step 2. Pre Simulation
for (int i = 0; i < H * W; i++) {
if (C[i / W][i % W] == '0') continue;
pair<int, int> v = Simulation(i / W, i % W, 0, K);
V1[i] = i;
V2[i] = v.first * W + v.second;
}
// Step 3. 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;
// Simulation
int root = 0;
for (int j = 0; j < H * W; j++) {
if (C[j / W][j % W] == '0') continue;
Used[V1[j]] = true;
P[V1[j]] = V2[j];
root = V1[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]].emplace_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);
}
for (int j = 0; j < H * W; j++) {
if (C[j / W][j % W] == '0') continue;
V1[j] = GetNext(V1[j] / W, V1[j] % W, S[i]);
V2[j] = GetNext(V2[j] / W, V2[j] % W, S[i]);
}
}
// Step 4. 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: 0ms
memory: 19068kb
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: 0ms
memory: 20184kb
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: 16248kb
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: 0
Accepted
time: 2116ms
memory: 31212kb
input:
1 200000 200 RDRLDRULURDLDRULLRULLRRULRULRDLLDLRUDDLRURLURLULDRUUURDLUDUDLLLLLURRDURLUDDRRLRRURUUDDLLDDUUUDUULRLRUDULRRUURUDDDDLULULLLLLLLLLLLUDURLURLRLLRRRURUURLUDULDUULRRLULLRUDRDRUUDDRUDRDLLDLURDDDURLUULDRRDLDD 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
3999923 3999865 3999864 3999740 3999729 3999728 3999727 3999726 3999725 3999724 3999723 3999665 3999664 3999540 3999529 3999528 3999527 3999526 3999525 3999524 3999523 3999465 3999464 3999340 3999329 3999328 3999327 3999326 3999325 3999324 3999323 3999265 3999264 3999140 3999129 3999128 3999127 3999...
result:
ok 200000 numbers
Test #5:
score: -100
Wrong Answer
time: 925ms
memory: 30624kb
input:
2 100000 200 UULDRDLURDLDDRDRDUULDLUUULLRURLUUDDDRURURLLRRUDLDDDUDDRRUUURDDULURURLRULLUDLULURUUDURLDRRRDULRDLRRLDUUUDDUUDUDRDRUDLDRRUDRDLDRDLDRRDLRRDRDLRRLUDUDRULLRRLDDLUDDULDRLLLDLURRDDULDDUDULLRRRUURLRRRLURDLRLU 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
76468 76341 76268 76141 76068 75941 75868 75741 75668 75541 75468 75341 75268 75141 75068 74941 74868 74741 74668 74541 74468 74341 74268 74141 74068 73941 73868 73741 73668 73541 73468 73341 73268 73141 73068 72941 72868 72741 72668 72541 72468 72341 72268 72141 72068 71941 71868 71741 71668 71541 ...
result:
wrong answer 1st numbers differ - expected: '-1', found: '76468'