답안 #125083

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
125083 2019-07-04T14:54:11 Z MoNsTeR_CuBe 과수원 (NOI14_orchard) C++17
17 / 25
1000 ms 57160 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
 
const int INF = 1e18;
 
int answer(int m, vector< int > one, vector< int > zero){
	vector< int > dp(m+1);
		
	dp[0] = -INF;
		
	int mini = INF;
		
	for(int i = 1; i <= m; i++){
		if(mini + zero[i] < one[i-1] + zero[i]-zero[i-1]){
			dp[i] = mini + zero[i];
			if(dp[i]-zero[i] <= mini){
				mini = dp[i]-zero[i];
			}
		}else{
			dp[i] = one[i-1] + zero[i] - zero[i-1];
			if(dp[i]-zero[i] <= mini){
				mini = dp[i]-zero[i];
			}
		}
	}
	int ans = INF;
		
	for(int i = 1; i <= m; i++){
		ans = min(ans, one[m]-one[i] + dp[i]);
	}
	return ans;
}
 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n, m;
	cin >> n >> m;
 
	vector< vector< int > > v(n+1, vector< int >(m+1));
 
	int totZero = 0;
	int totOne = 0;
 
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> v[i][j];
			totZero += (v[i][j] == 0);
			totOne += (v[i][j] == 1);
		}
	}
 
	if(n == 1){
		vector< int > one(m+1);
		vector< int > zero(m+1);
		
		for(int i = 1; i <= m; i++){
			one[i] += one[i-1];
			one[i] += (v[1][i] == 1);
		
			zero[i] += zero[i-1];
			zero[i] += v[1][i] == 0;
		}
		cout << answer(m, one, zero) << endl;
		return 0;
	}
 
	vector< vector< int > > one(n+1, vector< int > (m+1));
	vector< vector< int > > zero(n+1, vector< int > (m+1));
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			one[i][j] = one[i][j-1] + one[i-1][j] - one[i-1][j-1] + (v[i][j] == 1);
			zero[i][j] = zero[i][j-1] + zero[i-1][j] - zero[i-1][j-1] + (v[i][j] == 0);
		}
	}
	
	int ans = INF;
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j<= m; j++){
			
			for(int k = i; k <= n; k++){
				for(int l = j; l <= m; l++){
					ans = min(ans,(zero[k][l] - zero[i-1][l] - zero[k][j-1] + zero[i-1][j-1]) + (totOne - (one[k][l] - one[i-1][l] - one[k][j-1] + one[i-1][j-1])));
				}
			}
			
		}
	}
	
	cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1148 KB Output is correct
2 Correct 4 ms 1172 KB Output is correct
3 Correct 4 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 132 ms 57056 KB Output is correct
2 Correct 132 ms 57136 KB Output is correct
3 Correct 140 ms 57160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1067 ms 8552 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 632 KB Output is correct
2 Correct 299 ms 988 KB Output is correct
3 Correct 301 ms 976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1070 ms 19624 KB Time limit exceeded
2 Halted 0 ms 0 KB -