USACO Home on the Range 解题报告
2013-04-12 14:04
441 查看
只想到了暴力的方法,时间复杂度是O(n^3),最后一个测试点过不去。
网上看到了动态规划的方法,见http://www.byvoid.com/blog/usaco-334-home-on-the-range/。
感觉递推关系式还是很不容易想到的。当然,想到就很简单了。
代码如下:
网上看到了动态规划的方法,见http://www.byvoid.com/blog/usaco-334-home-on-the-range/。
感觉递推关系式还是很不容易想到的。当然,想到就很简单了。
代码如下:
/* ID: thestor1 LANG: C++ TASK: range */ #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <climits> #include <cassert> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; const int N = 250; int square ; int main() { FILE *fin = fopen ("range.in", "r"); FILE *fout = fopen ("range.out", "w"); //freopen("log.txt", "w", stdout); int n; fscanf(fin, "%d\n", &n); char c; for(int i = 0; i < n; ++i) { for(int j = 0; j < n;) { fscanf(fin, "%c", &c); if(c == '0' || c == '1') { square[i][j] = c - '0'; j++; } } } for(int i = n - 2; i >= 0; --i) { for(int j = n - 2; j >= 0; --j) { if(square[i][j]) { square[i][j] = 1 + min(min(square[i + 1][j], square[i][j + 1]), square[i + 1][j + 1]); } } } int cnt[N + 1] = {0}; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { cnt[square[i][j]]++; } } for(int i = N - 1; i >= 2; --i) { cnt[i] += cnt[i + 1]; } for(int i = 2; i <= N; ++i) { if(!cnt[i]) { break; } fprintf(fout, "%d %d\n", i, cnt[i]); } return 0; }
相关文章推荐
- USACO Home on the Range
- USACO--3.3Home on the Range+DP
- USACO 3.3 Home on the Range
- USACO home on the range C 语言实现
- Home On The Range_usaco3.3_dp
- USACO-Section3.3 Home on the Range【完全背包】
- USACO Home on the Range
- usaco Home on the Range
- USACO Section 3.3 Home on the Range
- usaco Home on the Range java ---DP
- USACO / Home on the Range (DP统计正方形个数)
- USACO 3.3 Home on the Range(最大子矩阵问题)
- usaco Home on the Range(dp+容斥原理)
- USACO Section 3.3 Home on the Range - 优化的BFS..
- [USACO]Home on the Range
- USACO-Section 3.3- PROB Home on the Range
- USACO-Section 3.3 Home on the Range (树状数组||DP)
- USACO3.3.4 Home on the Range (range)
- [USACO3.3.4]Home on the Range
- USACO 3.3 Home on the Range (range)