Luogu3444:[POI2006]ORK-Ploughing

时间:2021-07-23 16:56:49

题意

见luogu

Sol

贪心+枚举

如果知道最后一次是消除一行

那么一定消了\(n\)行

此时只要消的列最小就好了

枚举每列从上往下消到哪里,那么下面消的越小越好

那么就有了贪心策略:

先消左右的列,再消上面,再消下面

最后一次消列的情况是一样的

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2005); IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int k, m, n, s1[_][_], s2[_][_], ans = 2e9; IL int Calc1(RG int x){
RG int l1 = 1, r1 = n, l2 = 1, r2 = m, ret = 0;
while(l1 <= r1 && l2 <= r2){
++ret;
if(s2[r1][l2] - s2[l1 - 1][l2] <= k) ++l2;
else if(s2[r1][r2] - s2[l1 - 1][r2] <= k) --r2;
else if(s1[l1][r2] - s1[l1][l2 - 1] <= k && l1 < x) ++l1;
else if(s1[r1][r2] - s1[r1][l2 - 1] <= k) --r1;
else return 2e9;
}
return ret;
} IL int Calc2(RG int x){
RG int l1 = 1, r1 = n, l2 = 1, r2 = m, ret = 0;
while(l1 <= r1 && l2 <= r2){
++ret;
if(s1[l1][r2] - s1[l1][l2 - 1] <= k) ++l1;
else if(s1[r1][r2] - s1[r1][l2 - 1] <= k) --r1;
else if(s2[r1][l2] - s2[l1 - 1][l2] <= k && l2 < x) ++l2;
else if(s2[r1][r2] - s2[l1 - 1][r2] <= k) --r2;
else return 2e9;
}
return ret;
} int main(RG int argc, RG char* argv[]){
k = Input(), m = Input(), n = Input();
for(RG int i = 1; i <= n; ++i)
for(RG int j = 1; j <= m; ++j)
s1[i][j] = s2[i][j] = Input();
for(RG int i = 1; i <= n; ++i)
for(RG int j = 2; j <= m; ++j)
s1[i][j] += s1[i][j - 1];
for(RG int j = 1; j <= m; ++j)
for(RG int i = 2; i <= n; ++i)
s2[i][j] += s2[i - 1][j];
for(RG int i = 1; i <= n + 1; ++i) ans = min(ans, Calc1(i));
for(RG int i = 1; i <= m + 1; ++i) ans = min(ans, Calc2(i));
printf("%d\n", ans);
return 0;
}