赞
踩
https://codeforces.com/contest/1581/problem/C
题目意思是问在给定的
01
01
01矩阵中,横向长度至少为
5
5
5,纵向长度至少为
4
4
4,把这样一个矩形的四周除了四个顶点其他部分都变成
1
1
1,矩形内部全都变成
0
0
0,问最少需要多少次操作,每次操作可以把一个位置的
1
1
1变成
0
0
0,或者把一个位置的
0
0
0变成
1
1
1
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500;
char mp[MAXN][MAXN];
int f[MAXN][MAXN];
int cal(int a, int b, int c, int d){
return f[a][b] + f[c - 1][d - 1] - f[a][d - 1] - f[c - 1][b];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t, n, m;
cin >> t;
while(t--){
cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> mp[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + mp[i][j] - '0';
}
}
int ans = 20;
for(int x=1;x<=n;x++){
for(int y=1;y<=m;y++){
for(int i=x+4;i<=n;i++){
for(int j=y+3;j<=m;j++){
// cout << x << ' ' << y << ' ' << i << ' ' << j << '\n';
int jia_l = mp[x][y] + mp[i][y] - '0' - '0';
int jia_r = mp[i][j] + mp[x][j] - '0' - '0';
int wai = cal(i, j, x, y);
int nei = cal(i - 1, j - 1, x + 1, y + 1);
int len = i - x - 1;
int width = j - y - 1;
int r = cal(i, j, x, j) - jia_r;
int num = nei + 2 * width + len - (wai - nei - jia_l - jia_r - r);
if(ans < num) break;
ans = min(ans, nei + 2 * (len + width) - (wai - nei - jia_l - jia_r));
// cout << wai << ' ' << nei << ' ' << jia_l << ' ' << jia_r << '\n';
}
}
}
}
cout << ans << '\n';
}
return 0;
}
上几道二维前缀和的简单例题
https://www.luogu.com.cn/problem/P1719
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 150;
int f[MAXN][MAXN];
int cal(int a, int b, int c, int d){
return f[c][d] - f[c][b - 1] - f[a - 1][d] + f[a - 1][b - 1];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> f[i][j];
f[i][j] += f[i][j - 1] + f[i - 1][j] - f[i - 1][j - 1];
}
}
int ans = INT_MIN;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=i;k++){
for(int q=1;q<=j;q++){
ans = max(ans, cal(k, q, i, j));
}
}
}
}
cout << ans;
return 0;
}
http://47.110.135.197/problem.php?id=5182
http://47.110.135.197/problem.php?id=5183
例题
https://www.luogu.com.cn/problem/P5542
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<vector<int> > e(1000 + 1, vector<int>(1000 + 1));
for(int i=0;i<n;i++){
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1 += 1;
y1 += 1;
x2 += 1;
y2 += 1;
e[x1][y1] += 1;
e[x2][y2] += 1;
e[x1][y2] -= 1;
e[x2][y1] -= 1;
}
int ans = 0;
for(int i=1;i<=1000;i++){
for(int j=1;j<=1000;j++){
e[i][j] = e[i][j] + e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1];
ans += e[i][j] == k;
}
}
cout << ans << '\n';
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。