icpc:problems:usaco21feb_just_green_enough_s
problems | |
---|---|
名称 | Just Green Enough S |
题目编号 | USACO21FEB_S1 |
题目链接 | luogu.com.cn/… |
来源 | USACO |
算法分类 | 枚举 |
难易程度 | 容易 |
Just Green Enough S
想法
统计思路:先统计由>=100组成的子矩阵数量,再统计>100组成的子矩阵数量,相减即可得出包含了100的子矩阵数量。
统计实现:枚举子矩阵的左上角,向下扩展的同时,计算向右扩展的最小值,对于每个左上角统计由该点向下扩展的向右扩展的最小值。
代码实现
#include<cstdio> #include<algorithm> using namespace std; const int N = 510; int maps[N][N], cnt[N][N], n; bool vis[N][N]; long long int s() { for(int i=1; i<=n; i++) { int temp = 0; for(int j=n; j>=1; j--) { if(vis[i][j])temp++; else temp = 0; cnt[i][j] = temp; } } long long int sum = 0; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { int temp = 0, mi = cnt[i][j]; while(vis[i+temp][j]) { sum += mi; temp++; mi = min(mi, cnt[i+temp][j]); } } return sum; } int main() { scanf("%d", &n); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) scanf("%d", &maps[i][j]); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) vis[i][j] = (maps[i][j] >= 100); long long int count1 = s(); for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) vis[i][j] = (maps[i][j] > 100); long long int count2 = s(); printf("%lld", count1 - count2); return 0; }
/app/www/public/data/pages/icpc/problems/usaco21feb_just_green_enough_s.txt · 最后更改: 2023/03/25 10:23 由 温婕莺