답안 #53849

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53849 2018-07-01T09:38:24 Z ainta(#1540) Sandwich (JOI16_sandwich) C++11
35 / 100
8000 ms 26428 KB
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int Q[3 << 17], Deg[3 << 17], Res[3 << 17], ord[3 << 17], cnt, vis[3 << 17];
char p[1 << 9][1 << 9];
vector<int>F[3 << 17];
int n, m;
int Num(int x, int y, int ck) {
	if (x<1 || x>n || y<1 || y>m)return 0;
	int cc = 0;
	if (p[x][y] == 'N') {
		if (ck == 0 || ck == 3)cc = 1;
		else cc = 2;
	}
	else {
		if (ck == 0 || ck == 2)cc = 1;
		else cc = 2;
	}
	return ((x - 1)*m + (y - 1)) * 2 + cc;
}
void Add_Edge(int a, int b) {
	if (!a || !b)return;
	Deg[a]++;
	F[b].push_back(a);
}
int main() {
	int i, j;
	//freopen("input.txt", "r", stdin);
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; i++) {
		scanf("%s", p[i] + 1);
	}
	for (i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++) {
			Add_Edge(Num(i, j, 0), Num(i - 1, j, 0));
			Add_Edge(Num(i, j, 1), Num(i + 1, j, 1));
			Add_Edge(Num(i, j, 2), Num(i, j - 1, 2));
			Add_Edge(Num(i, j, 3), Num(i, j + 1, 3));
		}
	}
	int head = 0, tail = 0;
	for (i = 1; i <= n*m * 2; i++) {
		if (!Deg[i])Q[++tail] = i;
	}
	while (head < tail) {
		int x = Q[++head];
		for (auto &t : F[x]) {
			Deg[t]--;
			if (!Deg[t])Q[++tail] = t;
		}
	}
	for (i = 1; i <= tail; i++)ord[++cnt] = Q[i];
	int cur = 0;
	for (i = 1; i <= n; i++) {
		cur++;
		for (j = 1; j <= m; j++) {
			int bs = Num(i, j, 3);
			int head = 0, tail = 0;
			if (vis[bs] != cur && !Deg[bs]) {
				vis[bs] = cur;
				Q[++tail] = bs;
			}
			while (head < tail) {
				int x = Q[++head];
				for (auto &t : F[x]) {
					if (vis[t] != cur && !Deg[t]) {
						vis[t] = cur;
						Q[++tail] = t;
					}
				}
			}
			for (int k = 1; k <= tail; k++)Res[Q[k]] += m-j+1;
		}
		cur++;
		for (j = m; j >= 1; j--) {
			int bs = Num(i, j, 2);
			int head = 0, tail = 0;
			if (vis[bs] != cur && !Deg[bs]) {
				vis[bs] = cur;
				Q[++tail] = bs;
			}
			while (head < tail) {
				int x = Q[++head];
				for (auto &t : F[x]) {
					if (vis[t] != cur && !Deg[t]) {
						vis[t] = cur;
						Q[++tail] = t;
					}
				}
			}
			for (int k = 1; k <= tail; k++)Res[Q[k]] += j;
		}
	}
	for (i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++) {
			int bs = ((i - 1)*m + j - 1) * 2, res = 1e9;
			if (!Deg[bs + 1])res = min(res, Res[bs + 1]);
			if (!Deg[bs + 2])res = min(res, Res[bs + 2]);
			if (res > 8e8)res = -1;
			else res *= 2;
			printf("%d ", res);
		}
		printf("\n");
	}
}

Compilation message

sandwich.cpp: In function 'int main()':
sandwich.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
sandwich.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", p[i] + 1);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9596 KB Output is correct
2 Correct 9 ms 9596 KB Output is correct
3 Correct 10 ms 9624 KB Output is correct
4 Correct 9 ms 9684 KB Output is correct
5 Correct 10 ms 9684 KB Output is correct
6 Correct 11 ms 9828 KB Output is correct
7 Correct 17 ms 10084 KB Output is correct
8 Correct 15 ms 10084 KB Output is correct
9 Correct 12 ms 10084 KB Output is correct
10 Correct 15 ms 10084 KB Output is correct
11 Correct 23 ms 10084 KB Output is correct
12 Correct 13 ms 10084 KB Output is correct
13 Correct 19 ms 10084 KB Output is correct
14 Correct 17 ms 10084 KB Output is correct
15 Correct 17 ms 10084 KB Output is correct
16 Correct 17 ms 10176 KB Output is correct
17 Correct 17 ms 10176 KB Output is correct
18 Correct 20 ms 10176 KB Output is correct
19 Correct 19 ms 10176 KB Output is correct
20 Correct 12 ms 10176 KB Output is correct
21 Correct 13 ms 10176 KB Output is correct
22 Correct 12 ms 10176 KB Output is correct
23 Correct 14 ms 10176 KB Output is correct
24 Correct 21 ms 10176 KB Output is correct
25 Correct 18 ms 10176 KB Output is correct
26 Correct 23 ms 10176 KB Output is correct
27 Correct 17 ms 10176 KB Output is correct
28 Correct 16 ms 10292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9596 KB Output is correct
2 Correct 9 ms 9596 KB Output is correct
3 Correct 10 ms 9624 KB Output is correct
4 Correct 9 ms 9684 KB Output is correct
5 Correct 10 ms 9684 KB Output is correct
6 Correct 11 ms 9828 KB Output is correct
7 Correct 17 ms 10084 KB Output is correct
8 Correct 15 ms 10084 KB Output is correct
9 Correct 12 ms 10084 KB Output is correct
10 Correct 15 ms 10084 KB Output is correct
11 Correct 23 ms 10084 KB Output is correct
12 Correct 13 ms 10084 KB Output is correct
13 Correct 19 ms 10084 KB Output is correct
14 Correct 17 ms 10084 KB Output is correct
15 Correct 17 ms 10084 KB Output is correct
16 Correct 17 ms 10176 KB Output is correct
17 Correct 17 ms 10176 KB Output is correct
18 Correct 20 ms 10176 KB Output is correct
19 Correct 19 ms 10176 KB Output is correct
20 Correct 12 ms 10176 KB Output is correct
21 Correct 13 ms 10176 KB Output is correct
22 Correct 12 ms 10176 KB Output is correct
23 Correct 14 ms 10176 KB Output is correct
24 Correct 21 ms 10176 KB Output is correct
25 Correct 18 ms 10176 KB Output is correct
26 Correct 23 ms 10176 KB Output is correct
27 Correct 17 ms 10176 KB Output is correct
28 Correct 16 ms 10292 KB Output is correct
29 Correct 10 ms 10292 KB Output is correct
30 Correct 12 ms 10292 KB Output is correct
31 Correct 107 ms 21888 KB Output is correct
32 Correct 99 ms 21948 KB Output is correct
33 Correct 41 ms 21948 KB Output is correct
34 Execution timed out 8058 ms 26428 KB Time limit exceeded
35 Halted 0 ms 0 KB -