QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#729921#9564. Hey, Have You Seen My Kangaroo?ucup-team5062TL 4ms20296kbC++203.8kb2024-11-09 18:01:522024-11-09 18:01:53

Judging History

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

  • [2024-11-09 18:01:53]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:20296kb
  • [2024-11-09 18:01:52]
  • 提交

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 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;

		// 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
		for (int k = 0; k < H * W; k++) {
			if (CycleUsed[k] == true) continue;
			int cx = k;
			for (int j = 0; j < 3 * H * W; j++) {
				cx = P[cx];
				if (CycleUsed[cx] == true) break;
				if (j >= 2 * H * W) Cycle[cx] = true;
			}
			cx = k;
			for (int j = 0; j < H * W; 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: 4ms
memory: 20156kb

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

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

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...

output:


result: