QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#729948#9564. Hey, Have You Seen My Kangaroo?ucup-team5062WA 2354ms32816kbC++204.0kb2024-11-09 18:05:272024-11-09 18:05:35

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 18:05:35]
  • 评测
  • 测评结果:WA
  • 用时:2354ms
  • 内存:32816kb
  • [2024-11-09 18:05:27]
  • 提交

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];
bool CycleUsed[1 << 18];
int CycleCount[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++) CycleUsed[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 + 2; j++) CycleCount[j] = 0;

		// Simulation
		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];
		}

		// Get Cycles
		for (int k = 0; k < H * W; k++) {
			if (Used[k] == false || CycleUsed[k] == true) continue;

			// Cycles
			int cx = k, l = 0;
			for (int j = 0; j < 2 * H * W; j++) {
				if (CycleUsed[cx] == true) break;
				cx = P[cx]; l = j;
				CycleCount[cx] += 1;
				if (CycleCount[cx] >= 2) Cycle[cx] = true;
				if (CycleCount[cx] == 3) break;
			}
			cx = k;
			for (int j = 0; j <= l; j++) {
				CycleUsed[cx] = true;
				cx = P[cx];
			}
		}
		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: 20172kb

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: 3ms
memory: 20168kb

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: 20780kb

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
Wrong Answer
time: 2354ms
memory: 32816kb

input:

1 200000 200
RDRLDRULURDLDRULLRULLRRULRULRDLLDLRUDDLRURLURLULDRUUURDLUDUDLLLLLURRDURLUDDRRLRRURUUDDLLDDUUUDUULRLRUDULRRUURUDDDDLULULLLLLLLLLLLUDURLURLRLLRRRURUURLUDULDUULRRLULLRUDRDRUUDDRUDRDLLDLURDDDURLUULDRRDLDD
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

3999923
3999865
3999864
3999740
3999729
3999728
3999727
3999726
3999725
3999724
3999723
3999665
3999600
3999540
3999529
3999528
3999527
3999526
3999525
3999524
3999523
3999465
3999400
3999340
3999329
3999328
3999327
3999326
3999325
3999324
3999323
3999265
3999200
3999140
3999129
3999128
3999127
3999...

result:

wrong answer 13th numbers differ - expected: '3999664', found: '3999600'