QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#183756#7398. Triangle TilingzhoukangyangTL 460ms35016kbC++113.3kb2023-09-19 20:06:372023-09-19 20:06:38

Judging History

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

  • [2023-09-19 20:06:38]
  • 评测
  • 测评结果:TL
  • 用时:460ms
  • 内存:35016kb
  • [2023-09-19 20:06:37]
  • 提交

answer

#include<bits/stdc++.h> 
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long 
#define vi vector < int > 
#define sz(a) (int) (a).size()
#define me(a, x) memset(a, x, sizeof(a))
using namespace std;
template < int N, int Ne >  struct flows { 
	using F = int; // flow type
	F inf = 1e9;
	int n, s, t; // Remember to assign n, s and t !
	int ehd[N], cur[N], ev[Ne << 1], enx[Ne << 1], eid = 1;
	void clear() {
		eid = 1;
		L(i, 1, n) ehd[i] = 0;
	}
	F ew[Ne << 1], dis[N];
	void Eadd(int u, int v, F w) {
		++eid, enx[eid] = ehd[u], ew[eid] = w, ev[eid] = v, ehd[u] = eid;
	}
	void add(int u, int v, F w) {
		Eadd(u, v, w), Eadd(v, u, 0);
	}
	int que[N], ql, qr;
	bool bfs() {
		ql = 1, qr = 0;
		L(i, 1, n) dis[i] = inf, cur[i] = ehd[i]; 
		que[++qr] = s, dis[s] = 0;
		while(ql <= qr) {
			int u = que[ql++];
			if(u == t) return true;
			for(int i = ehd[u]; i; i = enx[i]) if(ew[i] && dis[ev[i]] == inf) {
				dis[ev[i]] = dis[u] + 1, que[++qr] = ev[i];
			}
		}
		return dis[t] < inf;
	}
	F dfs(int x, F now) {
		if(!now || x == t) return now;
		F res = 0, f;
		for(int i = cur[x]; i; i = enx[i]) {
			cur[x] = i;
			if(ew[i] && dis[ev[i]] == dis[x] + 1) {
				f = dfs(ev[i], min(now, ew[i])), ew[i] -= f, now -= f, ew[i ^ 1] += f, res += f;
				if(!now) break;
			}
		}
		return res;
	}
	F max_flow() {
		F res = 0;
		while(bfs()) res += dfs(s, inf);
		return res;
	}
} ; 

const int N = 5007;

flows < N * N, N * N * 3 / 2 > G;

int n;
char s[N][N];
int Pl[N][N], Pr[N][N];
bool win[N][N], gol[N][N];
short ans[N][N];

mt19937_64 orz(time(0) ^ *new int);
void Main() {
	G.clear();
	cin >> n;
	L(i, 1, n) {
		cin >> (s[i] + 1); 
	}
//	n = 1000;
//	L(i, 1, n) {
//		L(j, 1, i) {
//			s[i][j] = '0';
//		}
//	}
	L(i, 0, n + 1) {
		L(j, 0, i + 1) {
			Pl[i][j] = 0;
			Pr[i][j] = 0;
			win[i][j] = 0;
			gol[i][j] = 0;
			ans[i][j] = 0;
		}
	}
	G.n = 0;
	L(i, 1, n) {
		L(j, 1, i) {
			Pl[i][j] = ++G.n; 
			Pr[i][j] = ++G.n;
		}
	}
	G.s = ++G.n, G.t = ++G.n;
	L(i, 1, n) 
		Pr[i][0] = G.t;
	L(i, 1, n) 
		L(j, 1, i) 
			if(s[i][j] == '0') 
				G.add(G.s, Pr[i][j], 1);
	int cur = G.eid;
	L(i, 1, n) {
		L(j, 1, i) {
			G.add(Pr[i][j], Pl[i][j], 1);
			G.add(Pl[i][j], Pr[i][j - 1], 1);
			if(i > 1 && j > 1) G.add(Pl[i][j], Pr[i - 1][j - 1], 1);
		}
	} 
	if(G.max_flow() < n) {
		cout << "Impossible!\n";
		return ;
	}
	L(i, 1, n) {
		L(j, 1, i) {
			win[i][j] = 0;
			if(G.ew[cur + 2]) {
				win[i][j] = 1;
			} 
			cur += 2;
			if(win[i][j]) {
				if(G.ew[cur + 2]) {
					gol[i][j] = true;
				} else {
					gol[i][j] = false;
				}
			}
			cur += 2;
			if(i > 1 && j > 1) cur += 2;
		}
	}
	L(i, 1, n) {
		L(j, 1, i) {
			if(s[i][j] != '0') {
				ans[i][j] = 1;
			}
		}
	}
	L(i, 1, n) {
		L(j, 1, i) {
			if(s[i][j] == '0') {
				int x = i, y = j;
				while(y > 0) {
					if(gol[x][y]) {
						if(y > 1) ans[x][y - 1] = 3;
						--y;
					} else {
						ans[x - 1][y - 1] = 2;
						--x, --y;
					}
				}
			}
		}
	}
//	cout<<"Yes\n";
	L(i, 1, n) {
		L(j, 1, i) {
			if(s[i][j] == '0') cout << "-";
			else cout << ans[i][j];
		}
		cout << '\n';
	}
}
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t; while(t--) Main();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 28412kb

input:

1
4
0
11
010
1101

output:

-
21
-3-
33-1

result:

ok ok

Test #2:

score: 0
Accepted
time: 0ms
memory: 28352kb

input:

909
6
0
11
100
1111
11100
111110
7
0
11
011
1001
10111
100111
1111111
6
0
00
001
1111
11101
111111
8
1
01
111
1111
10111
101110
1101101
11111100
2
1
00
2
0
01
3
0
01
110
7
1
00
011
1010
11011
110111
1111111
8
1
11
111
1011
11010
011110
1101110
01111111
5
1
00
010
1111
10111
6
0
10
011
0101
01111
111...

output:

-
32
3--
3332
333--
33333-
Impossible!
Impossible!
Impossible!
2
--
-
-1
-
-1
33-
Impossible!
2
22
222
2-22
23-2-
-3323-
33-133-
-1111111
Impossible!
Impossible!
Impossible!
Impossible!
2
--
2
--
Impossible!
-
21
--1
-
-1
Impossible!
2
22
2--
-3-1
Impossible!
2
-2
-1-
2111
-2111
3233-1
3--1111
3333-...

result:

ok ok

Test #3:

score: 0
Accepted
time: 8ms
memory: 30492kb

input:

500
10
0
10
110
1110
11101
011111
0111111
11101111
111011111
1111111011
10
0
10
110
1011
11101
111110
1011111
11111101
111110111
1111111110
10
0
10
011
1011
11101
111011
0111111
11110111
111110111
1111110111
10
0
01
011
0111
01111
101111
1110111
11011111
110111111
1111111011
10
0
01
110
0111
11101
1...

output:

-
3-
33-
333-
333-1
-11111
-111111
333-1111
333-11111
3333333-11
-
3-
33-
3-11
333-1
33333-
3-11111
333333-1
33333-111
333333333-
-
3-
-11
3-11
333-1
333-11
-111111
3333-111
33333-111
333333-111
-
-1
-11
-111
-1111
3-1111
333-111
33-11111
33-111111
3333333-11
-
-1
33-
-111
333-1
33333-
3-11111
33-11...

result:

ok ok

Test #4:

score: 0
Accepted
time: 5ms
memory: 28332kb

input:

500
10
0
11
111
1111
11111
101101
1111100
10111011
110111110
1111110111
10
1
00
101
0101
10110
111110
1111111
11111111
111111100
1111111111
10
1
11
111
1111
11010
011111
0111110
11011111
101111101
1101111101
10
0
01
100
1111
11010
111110
1110110
11111111
111111111
1011111111
10
0
10
100
1010
11111
1...

output:

-
32
322
3222
32222
3-22-2
33223--
3-123-11
33-13333-
333333-111
Impossible!
2
22
222
2222
22-2-
-23232
-13232-
33-13221
3-11112-1
33-11113-1
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
2
-2
212
2212
--212
3-121-
333-211
-1111211
3-1111321
3333333--1
Impossible!
2
2-
2-1
2332
2321-
2...

result:

ok ok

Test #5:

score: 0
Accepted
time: 4ms
memory: 28364kb

input:

500
10
0
00
110
0110
11101
001111
1110111
11111111
111111111
1111111111
10
1
00
111
0100
00011
111101
1111011
11111111
111111111
1111111111
10
1
00
001
0001
01001
111111
1111111
11111111
111111111
1111111111
10
1
00
111
0100
01001
111111
0011111
11111111
111111111
1111111111
10
0
00
111
1011
10100
0...

output:

Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
...

result:

ok ok

Test #6:

score: 0
Accepted
time: 22ms
memory: 32572kb

input:

250
20
0
01
101
1011
01111
111110
1111011
11110111
011111111
1111111101
11111111110
111110111111
1111111111110
11111111111101
111111111110111
1111111111011111
11111111111011111
011111111111111111
1111111110111111111
11111111110111111111
20
0
01
101
1110
11110
101111
1111011
11011111
011111111
111011...

output:

-
-1
3-1
3-11
-1111
33333-
3333-11
3333-111
-11111111
33333333-1
3333333333-
33333-111111
333333333333-
333333333333-1
33333333333-111
3333333333-11111
33333333333-11111
-11111111111111111
333333333-111111111
3333333333-111111111
-
-1
3-1
333-
3333-
3-1111
3333-11
33-11111
-11111111
333-111111
33333...

result:

ok ok

Test #7:

score: 0
Accepted
time: 14ms
memory: 28396kb

input:

250
20
1
01
101
1010
11101
111111
1111111
00111111
111111111
1110011111
11111101111
111111111111
1111111111111
11011111111111
111111111111111
1111110110111111
11011111101111111
111111111110111111
1111111111111011011
01111111111111011111
20
1
10
111
1111
11111
111110
1001111
11111111
111101101
110111...

output:

2
-2
3-2
3-1-
333-1
211111
2211111
--211111
332211111
332--11111
332333-1111
332333333211
3212111111211
32-12111111211
323323332111211
323323-11-111211
32-113333-1111211
32333333333-111211
3233333333333-11-11
-1333333333333-11111
Impossible!
Impossible!
Impossible!
2
22
2--
2321
232-1
232321
2322121...

result:

ok ok

Test #8:

score: 0
Accepted
time: 8ms
memory: 28368kb

input:

250
20
0
10
011
0110
10001
010111
1111111
11111111
111110111
1110111111
10111111111
111101111111
1111111110111
11111111111111
111111110111111
1011111111110111
11111110111111111
111111111011111111
1111111111111111111
11111111111111111111
20
1
11
100
1111
01101
100011
0001111
10110111
010011111
010111...

output:

Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
...

result:

ok ok

Test #9:

score: 0
Accepted
time: 460ms
memory: 35016kb

input:

50
100
1
11
110
0101
11101
011111
1111111
11111111
111111111
0111111111
01111101111
111111111111
1111111111111
11111111111111
111111110111110
1111111111111111
01111111111111101
111111111111111111
1111110111111111111
11111111101111111111
110111111111011111111
1111111111111111111111
111111011111111111...

output:

Impossible!
2
22
-2-
323-
32211
212211
2-12-11
22112111
2-211-111
2212111111
22212111111
222322111111
22-3-22111111
22333--3332111
22333333211-111
222111111-111111
22221111111111111
22-221111111111111
2221221111111111111
22221221111111111111
222221221111111111111
222-221221111111111111
2223222123333...

result:

ok ok

Test #10:

score: 0
Accepted
time: 59ms
memory: 32944kb

input:

50
100
0
01
111
1011
01111
111111
1111111
11110001
111000111
1111111111
01110101111
111111111111
1111011111111
11101111111111
111111011111111
1111011111011101
11101111011111110
111111111111111111
0111110111111011111
11111111111111111111
110111011111111101110
0111111111111110110111
111111111110011111...

output:

Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
Impossible!
...

result:

ok ok

Test #11:

score: -100
Time Limit Exceeded

input:

5
1000
0
01
101
1101
11101
111110
0111111
11011111
111111110
1110111111
11111110111
111111111110
1111111101111
11011111111111
111111101111111
1101111111111111
10111111111111111
101111111111111111
1011111111111111111
11101111111111111111
111111111111111111011
1111111011111111111111
111111011111111111...

output:


result: