답안 #505944

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
505944 2022-01-11T10:53:56 Z pragmatist Bomb (IZhO17_bomb) C++17
28 / 100
1000 ms 105956 KB
#include <bits/stdc++.h>                            
 
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
 
using namespace std;

typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;

const int N = (int)1e6 + 7;
const int M = (int)5e6 + 7;
const ll MOD = (ll)1e9 + 7;                    
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7;

pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}};

int n, m;
char c[2501][2501];
int used[2501][2501], w[2501][2501];

int get(int x1, int y1, int x2, int y2) {
	return w[x2][y2] - w[x2][y1-1] - w[x1-1][y2] + w[x1-1][y1-1];
}

void upd(int x1, int y1, int x2, int y2) {
	used[x1][y1]++;
	used[x1][y2+1]--;
	used[x2+1][y1]--;
	used[x2+1][y2+1]++;
}

void solve() {           
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			cin >> c[i][j];
			w[i][j] = w[i-1][j] + w[i][j-1] - w[i-1][j-1] + (c[i][j] == '1');
		}
	}
	int ans = inf;
	if(n == 1) {
		int cur = 0;
		for(int i = 1; i <= m; ++i) {
			if(c[1][i] == '1') cur++;
			else {
				if(cur > 0) ans = min(ans, cur);
				cur = 0;
			}
		}
		if(c[1][m] == '1') ans = min(ans, cur);
		cout << ans << nl;		
		return;
	}
	if(m == 1) {
		int cur = 0;
		for(int i = 1; i <= n; ++i) {
			if(c[i][1] == '1') cur++;
			else {
				if(cur > 0) ans = min(ans, cur);
				cur = 0;
			}
		}                                      		
		if(c[n][1] == '1') ans = min(ans, cur);
		cout << ans << nl;		
		return;
	}
	ans = 0;
	int l = 1, r = n;
	while(l <= r) {
		int e = (l + r) >> 1;
		int L = 1, R = m;
		bool cur = 0;
		while(L <= R) {
		    int f = (L + R) >> 1;
			for(int i = 1; i <= n; ++i) fill(used[i] + 1, used[i] + 1 + m, 0);
			for(int i = 1; i <= n; ++i) {
				for(int j = 1; j <= m; ++j) {
			        if(i + e - 1 > n || j + f - 1 > m) break;
					int x = get(i, j, i + e - 1, j + f - 1);
					if(x == e * f) {
						upd(i, j, i + e - 1, j + f - 1);					
					}					
				}
			}
	        for(int i = 1; i <= n; ++i) {
	        	for(int j = 1; j <= m; ++j) {
	        		used[i][j] += used[i-1][j] + used[i][j-1] - used[i-1][j-1];
	        	}
	        }
	        bool ok = 1;
			for(int i = 1; i <= n && ok; ++i) {
				for(int j = 1; j <= m && ok; ++j) {
					if(c[i][j] == '1') ok &= (used[i][j] > 0);
				}
			}
			if(ok) {
				cur = 1;
				ans = max(ans, e * f);
				L = f + 1;
			} else
				R = f - 1;
		}
		if(cur)
			l = e + 1;
		else
			r = e - 1;
	}
	l = 1, r = m;
	while(l <= r) {
		int f = (l + r) >> 1;
		int L = 1, R = n;
		bool cur = 0;
		while(L <= R) {
		    int e = (L + R) >> 1;
			for(int i = 1; i <= n; ++i) fill(used[i] + 1, used[i] + 1 + m, 0);
			for(int i = 1; i <= n; ++i) {
				for(int j = 1; j <= m; ++j) {
			        if(i + e - 1 > n || j + f - 1 > m) break;
					int x = get(i, j, i + e - 1, j + f - 1);
					if(x == e * f) {
						upd(i, j, i + e - 1, j + f - 1);					
					}					
				}
			}
	        for(int i = 1; i <= n; ++i) {
	        	for(int j = 1; j <= m; ++j) {
	        		used[i][j] += used[i-1][j] + used[i][j-1] - used[i-1][j-1];
	        	}
	        }
	        bool ok = 1;
			for(int i = 1; i <= n && ok; ++i) {
				for(int j = 1; j <= m && ok; ++j) {
					if(c[i][j] == '1') ok &= (used[i][j] > 0);
				}
			}
			if(ok) {
				cur = 1;
				ans = max(ans, e * f);
				L = e + 1;
			} else
				R = e - 1;
		}
		if(cur)
			l = f + 1;
		else
			r = f - 1;
	}  
	cout << ans << nl;
}                                                                              
                
signed main() {                   
	ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
   	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; ++i) {
        //cout << "Case " << i << ": ";
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 8 ms 16460 KB Output is correct
4 Correct 12 ms 16460 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Incorrect 1 ms 564 KB Output isn't correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 444 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 0 ms 424 KB Output is correct
14 Correct 0 ms 448 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 444 KB Output is correct
17 Correct 2 ms 1100 KB Output is correct
18 Correct 2 ms 1100 KB Output is correct
19 Incorrect 3 ms 1344 KB Output isn't correct
20 Incorrect 4 ms 1356 KB Output isn't correct
21 Correct 2 ms 972 KB Output is correct
22 Correct 3 ms 1220 KB Output is correct
23 Incorrect 5 ms 1484 KB Output isn't correct
24 Incorrect 4 ms 1228 KB Output isn't correct
25 Incorrect 5 ms 1484 KB Output isn't correct
26 Correct 5 ms 1484 KB Output is correct
27 Correct 60 ms 4740 KB Output is correct
28 Correct 71 ms 5208 KB Output is correct
29 Incorrect 86 ms 6468 KB Output isn't correct
30 Incorrect 121 ms 7964 KB Output isn't correct
31 Incorrect 104 ms 6268 KB Output isn't correct
32 Incorrect 138 ms 7244 KB Output isn't correct
33 Incorrect 127 ms 8404 KB Output isn't correct
34 Correct 46 ms 5324 KB Output is correct
35 Incorrect 139 ms 8408 KB Output isn't correct
36 Correct 212 ms 8416 KB Output is correct
37 Correct 1 ms 448 KB Output is correct
38 Execution timed out 1065 ms 105824 KB Time limit exceeded
39 Correct 1 ms 460 KB Output is correct
40 Correct 874 ms 22272 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Incorrect 6 ms 1480 KB Output isn't correct
43 Execution timed out 1093 ms 105760 KB Time limit exceeded
44 Incorrect 159 ms 8412 KB Output isn't correct
45 Execution timed out 1092 ms 105856 KB Time limit exceeded
46 Execution timed out 1105 ms 105852 KB Time limit exceeded
47 Execution timed out 1029 ms 105780 KB Time limit exceeded
48 Execution timed out 1016 ms 105788 KB Time limit exceeded
49 Execution timed out 1066 ms 105768 KB Time limit exceeded
50 Execution timed out 1048 ms 105856 KB Time limit exceeded
51 Execution timed out 1037 ms 105788 KB Time limit exceeded
52 Execution timed out 1062 ms 105856 KB Time limit exceeded
53 Execution timed out 1037 ms 105840 KB Time limit exceeded
54 Execution timed out 1016 ms 105856 KB Time limit exceeded
55 Execution timed out 1062 ms 105836 KB Time limit exceeded
56 Execution timed out 1087 ms 105792 KB Time limit exceeded
57 Execution timed out 1044 ms 105772 KB Time limit exceeded
58 Execution timed out 1084 ms 105568 KB Time limit exceeded
59 Execution timed out 1077 ms 105660 KB Time limit exceeded
60 Execution timed out 1068 ms 105588 KB Time limit exceeded
61 Execution timed out 1041 ms 105672 KB Time limit exceeded
62 Execution timed out 1081 ms 105624 KB Time limit exceeded
63 Execution timed out 1018 ms 105660 KB Time limit exceeded
64 Execution timed out 1012 ms 105576 KB Time limit exceeded
65 Execution timed out 1008 ms 105660 KB Time limit exceeded
66 Execution timed out 1074 ms 105668 KB Time limit exceeded
67 Execution timed out 1014 ms 105568 KB Time limit exceeded
68 Execution timed out 1012 ms 105500 KB Time limit exceeded
69 Execution timed out 1076 ms 105432 KB Time limit exceeded
70 Execution timed out 1022 ms 84216 KB Time limit exceeded
71 Execution timed out 1034 ms 105416 KB Time limit exceeded
72 Execution timed out 1016 ms 105408 KB Time limit exceeded
73 Execution timed out 1042 ms 105564 KB Time limit exceeded
74 Execution timed out 1041 ms 105512 KB Time limit exceeded
75 Execution timed out 1025 ms 105440 KB Time limit exceeded
76 Execution timed out 1031 ms 105488 KB Time limit exceeded
77 Execution timed out 1066 ms 105300 KB Time limit exceeded
78 Execution timed out 1018 ms 105340 KB Time limit exceeded
79 Execution timed out 1030 ms 105340 KB Time limit exceeded
80 Execution timed out 1088 ms 105284 KB Time limit exceeded
81 Execution timed out 1044 ms 105332 KB Time limit exceeded
82 Execution timed out 1067 ms 105236 KB Time limit exceeded
83 Execution timed out 1072 ms 105336 KB Time limit exceeded
84 Execution timed out 1042 ms 105188 KB Time limit exceeded
85 Execution timed out 1051 ms 105132 KB Time limit exceeded
86 Execution timed out 1058 ms 105544 KB Time limit exceeded
87 Execution timed out 1062 ms 105796 KB Time limit exceeded
88 Execution timed out 1072 ms 105804 KB Time limit exceeded
89 Execution timed out 1043 ms 105704 KB Time limit exceeded
90 Execution timed out 1086 ms 84324 KB Time limit exceeded
91 Execution timed out 1102 ms 105768 KB Time limit exceeded
92 Execution timed out 1102 ms 105532 KB Time limit exceeded
93 Execution timed out 1056 ms 105636 KB Time limit exceeded
94 Execution timed out 1058 ms 105540 KB Time limit exceeded
95 Execution timed out 1010 ms 105676 KB Time limit exceeded
96 Execution timed out 1012 ms 105700 KB Time limit exceeded
97 Execution timed out 1087 ms 105768 KB Time limit exceeded
98 Execution timed out 1054 ms 105708 KB Time limit exceeded
99 Execution timed out 1090 ms 105908 KB Time limit exceeded
100 Execution timed out 1040 ms 105956 KB Time limit exceeded