Submission #995058


Source Code Expand

#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <iostream>

using namespace std;

#define MOD @
#define ADD(X,Y) ((X) = ((X) + (Y)) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

namespace MF {
#define MAXN 101010
#define MAXM 202020
#define wint int
const wint wEPS = 0;
const wint wINF = 1001001001;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
int lev[MAXN], see[MAXN], que[MAXN], *qb, *qe;
void init(int _n) {
	n = _n; m = 0; memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w0, wint w1 = 0) {
	next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = w0; ++m;
	next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = w1; ++m;
}
wint augment(int src, int ink, wint flo) {
	if (src == ink) return flo;
	wint f;
	for (int &i = see[src]; ~i; i = next[i]) if (capa[i] > wEPS && lev[src] < lev[zu[i]]) {
		if ((f = augment(zu[i], ink, min(flo, capa[i]))) > wEPS) {
			capa[i] -= f; capa[i ^ 1] += f; return f;
		}
	}
	return 0;
}
bool solve(int src, int ink, wint flo = wINF) {
	wint f;
	int i, u, v;
	for (tof = 0; tof + wEPS < flo; ) {
		qb = qe = que;
		memset(lev, ~0, n * 4);
		for (lev[*qe++ = src] = 0, see[src] = ptr[src]; qb != qe; ) {
			for (i = ptr[u = *qb++]; ~i; i = next[i]) if (capa[i] > wEPS && !~lev[v = zu[i]]) {
				lev[*qe++ = v] = lev[u] + 1; see[v] = ptr[v];
				if (v == ink) goto au;
			}
		}
		return 0;
	au:	for (; (f = augment(src, ink, flo - tof)) > wEPS; tof += f);
	}
	return 1;
}
}

int N, U[303], D[303], L[303], R[303];
enum {
	UP, DOWN, LEFT, RIGHT
};

int dir[303][303];
bool vis[42][42];

bool resolve()
{
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			vis[i][j] = false;
		}
	}
	bool ret = false;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			if (vis[i][j]) continue;
			int y = i, x = j;
			vector<pair<int, int> > vc;
			while (y >= 0 && x >= 0 && y < N && x < N) {
				if (vis[y][x]) {
					vector<pair<int, int> > vc2;
					for (int k = 0; k < vc.size(); ++k) {
						if (vc[k] == make_pair(y, x)) {
							for (int l = k; l < vc.size(); ++l) vc2.push_back(vc[l]);
							break;
						}
					}
					swap(vc, vc2);
				}
				vis[y][x] = true;
				vc.push_back({ y, x });
				switch (dir[y][x]) {
				case 0: --y; break;
				case 1: ++y; break;
				case 2: --x; break;
				case 3: ++x; break;
				}
			}
			if (!(y >= 0 && x >= 0 && y < N && x < N)) goto nex;

			ret = true;
			vector<int> cdir;
		//	printf("%d\n", cdir.size());
			for (auto a : vc) cdir.push_back(dir[a.first][a.second]);
			for (int t = 0; t < vc.size(); ++t) {
				auto pt = vc[(t + 1) % vc.size()];
				dir[pt.first][pt.second] = cdir[t];
			}
		}
	nex:
		continue;
	}
	return ret;
}

bool used[44][44];

int main()
{
	scanf("%d", &N);
	if (N > 40) return 0;

	for (int i = 0; i < N; ++i) scanf("%d", U + i);
	for (int i = 0; i < N; ++i) scanf("%d", D + i);
	for (int i = 0; i < N; ++i) scanf("%d", L + i);
	for (int i = 0; i < N; ++i) scanf("%d", R + i);

	MF::init(2 + 4 * N + N * N);
	int s = 4 * N + N * N;
	int t = s + 1;
	auto cell = [&](int y, int x) { return 4 * N + y * N + x; };

	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			MF::ae(j, cell(i, j), 1, 0);
			MF::ae(j + N, cell(i, j), 1, 0);
			MF::ae(i + 2 * N, cell(i, j), 1, 0);
			MF::ae(i + 3 * N, cell(i, j), 1, 0);
			MF::ae(cell(i, j), t, 1, 0);
		}
	}
	for (int i = 0; i < N; ++i) {
		MF::ae(s, i, U[i]);
		MF::ae(s, i + 1 * N, D[i]);
		MF::ae(s, i + 2 * N, L[i]);
		MF::ae(s, i + 3 * N, R[i]);
	}
	MF::solve(s, t);
	if (MF::tof != N * N) {
		puts("NO");
		return 0;
	}
	for (int i = 0; i < N * N; ++i) {
		for (int p = MF::ptr[4 * N + i]; p >= 0; p = MF::next[p]) {
			if (MF::zu[p] < 4 * N && MF::capa[p] == 1) {
				dir[i / N][i % N] = MF::zu[p] / N;
			}
		}
	}
/*	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) printf("%d ", dir[i][j]);
		puts("");
	}*/
	while (resolve());
	for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) used[i][j] = false;

	for (int i = 0; i < N * N; ++i) {
		for (int j = 0; j < N; ++j) {
			for (int k = 0; k < N; ++k) {
				if (!used[k][j]) {
					if (dir[k][j] == 0) {
						used[k][j] = true;
						printf("U%d\n", j + 1);
						goto nex;
					}
					break;
				}
			}
			for (int k = N - 1; k >= 0; --k) {
				if (!used[k][j]) {
					if (dir[k][j] == 1) {
						used[k][j] = true;
						printf("D%d\n", j + 1);
						goto nex;
					}
					break;
				}
			}
			for (int k = 0; k < N; ++k) {
				if (!used[j][k]) {
					if (dir[j][k] == 2) {
						used[j][k] = true;
						printf("L%d\n", j + 1);
						goto nex;
					}
					break;
				}
			}
			for (int k = N - 1; k >= 0; --k) {
				if (!used[j][k]) {
					if (dir[j][k] == 3) {
						used[j][k] = true;
						printf("R%d\n", j + 1);
						goto nex;
					}
					break;
				}
			}
		}
	nex:
		continue;

	}

	return 0;
}

Submission Info

Submission Time
Task J - Neue Spiel
User semiexp
Language C++14 (GCC 5.4.1)
Score 0
Code Size 5192 Byte
Status WA
Exec Time 4202 ms
Memory 512 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:127:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
                 ^
./Main.cpp:130:48: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < N; ++i) scanf("%d", U + i);
                                                ^
./Main.cpp:131:48: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < N; ++i) scanf("%d", D + i);
                                                ^
./Main.cpp:132:48: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < N; ++i) scanf("%d", L + i);
                                                ^
./Main.cpp:133:48: warning: ignoring return value of ‘int scanf(...

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 0 / 2000 0 / 100
Status
AC × 2
AC × 17
WA × 8
TLE × 23
AC × 17
WA × 20
TLE × 23
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt
dataset2 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt
Case Name Status Exec Time Memory
01-01.txt AC 3 ms 256 KB
01-02.txt TLE 4202 ms 256 KB
01-03.txt AC 3 ms 256 KB
01-04.txt TLE 4202 ms 256 KB
01-05.txt WA 3 ms 384 KB
01-06.txt TLE 4202 ms 384 KB
01-07.txt TLE 4202 ms 512 KB
01-08.txt TLE 4202 ms 512 KB
01-09.txt TLE 4202 ms 384 KB
01-10.txt TLE 4202 ms 512 KB
01-11.txt WA 5 ms 512 KB
01-12.txt TLE 4202 ms 512 KB
01-13.txt WA 5 ms 512 KB
01-14.txt AC 2 ms 256 KB
01-15.txt TLE 4202 ms 512 KB
01-16.txt TLE 4202 ms 512 KB
01-17.txt WA 5 ms 512 KB
01-18.txt TLE 4202 ms 512 KB
01-19.txt AC 3 ms 512 KB
01-20.txt TLE 4202 ms 512 KB
01-21.txt TLE 4202 ms 512 KB
01-22.txt AC 3 ms 512 KB
01-23.txt AC 3 ms 512 KB
01-24.txt TLE 4202 ms 512 KB
01-25.txt TLE 4202 ms 512 KB
01-26.txt AC 3 ms 384 KB
01-27.txt TLE 4202 ms 384 KB
01-28.txt TLE 4202 ms 512 KB
01-29.txt WA 5 ms 512 KB
01-30.txt TLE 4202 ms 512 KB
01-31.txt AC 3 ms 512 KB
01-32.txt TLE 4202 ms 512 KB
01-33.txt AC 3 ms 512 KB
01-34.txt TLE 4202 ms 512 KB
01-35.txt AC 6 ms 512 KB
01-36.txt AC 6 ms 512 KB
01-37.txt AC 3 ms 512 KB
01-38.txt TLE 4202 ms 512 KB
01-39.txt AC 6 ms 512 KB
01-40.txt TLE 4202 ms 512 KB
01-41.txt TLE 4202 ms 512 KB
01-42.txt WA 4 ms 512 KB
01-43.txt AC 4 ms 512 KB
01-44.txt WA 4 ms 512 KB
01-45.txt AC 4 ms 512 KB
01-46.txt WA 4 ms 512 KB
02-01.txt WA 2 ms 256 KB
02-02.txt WA 2 ms 256 KB
02-03.txt WA 2 ms 256 KB
02-04.txt WA 2 ms 256 KB
02-05.txt WA 2 ms 256 KB
02-06.txt WA 2 ms 256 KB
02-07.txt WA 2 ms 256 KB
02-08.txt WA 2 ms 256 KB
02-09.txt WA 2 ms 256 KB
02-10.txt WA 2 ms 256 KB
02-11.txt WA 2 ms 256 KB
02-12.txt WA 2 ms 256 KB
sample-01.txt AC 2 ms 256 KB
sample-02.txt AC 3 ms 256 KB