답안 #565546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
565546 2022-05-21T05:49:25 Z shmad 과수원 (NOI14_orchard) C++17
25 / 25
177 ms 13584 KB
#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt") 

#include <bits/stdc++.h>

#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bit(x, i) (x >> i & 1)

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vvt = vt< vt<int> >;

const int N = 1e5 + 5, mod = 1e9 + 7, inf = 1e18 + 7, B = 500, LIM = (1ll << 60);
const double eps = 1e-6;

int n, m, p[151][N], v[N];

int get (int l, int r) {
	int mn = 0, ans = inf;
	int len = r - l + 1;
	for (int i = 1; i <= m; i++) {
		mn = min(mn, v[i] * 2 - (i * len));
		ans = min(ans, (i * len) - v[i] * 2 + mn);
	}
	return ans;
}

void solve () {
	cin >> n >> m;
	bool a[n + 1][m + 1];
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			cnt += a[i][j];
			p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j];
		}
	}
	int ans = inf;
	for (int i = 1; i <= n; i++) {
		for (int j = i; j <= n; j++) {
			for (int k = 1; k <= m; k++) v[k] = p[j][k] - p[i - 1][k];
			ans = min(ans, get(i, j));
		}
	}
	cout << cnt + ans;
	cout << '\n';
}

bool testcases = 0;

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    int test = 1;
    if (testcases) cin >> test;
    for (int cs = 1; cs <= test; cs++) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 13544 KB Output is correct
2 Correct 79 ms 13560 KB Output is correct
3 Correct 79 ms 13584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 3284 KB Output is correct
2 Correct 15 ms 3304 KB Output is correct
3 Correct 15 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 980 KB Output is correct
2 Correct 8 ms 1364 KB Output is correct
3 Correct 7 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 9240 KB Output is correct
2 Correct 172 ms 9240 KB Output is correct
3 Correct 153 ms 9200 KB Output is correct