USACO section 1.5.1 Number Triangles
来源:互联网 发布:软件需求文档ppt 编辑:程序博客网 时间:2024/05/19 20:40
1. 动态规划,f[i,j]=Max{f[i+1,j],f[i+1,j+1]}+a[i,j] (1<=i<=n-1,1<=j<=i)复杂度是o(n^2)。
2. 以下是代码:
/*ID: dollar4PROG: numtriLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <algorithm>#include <cstring>using namespace std;long long num[1010][1010];long max(long a, long b){ if (a > b) return a; else return b;}int main(){ ofstream fout ("numtri.out"); ifstream fin ("numtri.in"); int n, i, j; fin >> n; for (i = 0; i < n; i++) for (j = 0; j <= i; j++) fin >> num[i][j]; for (i = n - 2; i >= 0; i--) for (j = 0; j <= i; j++) num[i][j] += max(num[i+1][j], num[i+1][j+1]); fout << num[0][0] << endl; return 0;}
3. 第一次用了BFS做,果断超时,下边是BFS代码:
/*ID: dollar4PROG: numtriLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <algorithm>#include <cstring>#include <queue>using namespace std;int num[1010][1010], ans[1010][1010];int n;struct Node{ int x, y;} node;queue<Node> q;void bfs(Node nod){ q.push(nod); Node next1, next2, temp; while (!q.empty()) { temp = q.front(); q.pop(); if (temp.y + 1 < n) { next1.x = temp.x + 1; next1.y = temp.y; next2.x = temp.x + 1; next2.y = temp.y + 1; q.push(next1); q.push(next2); if (ans[next1.x][next1.y] < ans[temp.x][temp.y] + num[next1.x][next1.y]) ans[next1.x][next1.y] = ans[temp.x][temp.y] + num[next1.x][next1.y]; if (ans[next2.x][next2.y] = ans[temp.x][temp.y] + num[next2.x][next2.y]) ans[next2.x][next2.y] = ans[temp.x][temp.y] + num[next2.x][next2.y]; } else break; } return;}int main(){ ofstream fout ("numtri.out"); ifstream fin ("numtri.in"); memset(num, 0, sizeof(num)); int i, j; fin >> n; for (i = 0; i < n; i++) for (j = 0; j <= i; j++) fin >> num[i][j]; Node start; start.x = 0; start.y = 0; ans[0][0] = num[0][0]; bfs(start); for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) cout << ans[i][j] << ' '; cout << endl; } int output = ans[0][0]; for (i = 0; i < n; i++) for (j = 0; j <= i; j++) { if (output < ans[i][j]) output = ans[i][j]; } fout << output << endl; return 0;}
4. 官方参考代码
We keep track (in the "best" array) of total for the best path ending in a given column of the triangle. Viewing the input, a path through the triangle always goes down or down and to the right. To process a new row, the best path total ending at a given column is the maximum of the best path total ending at that column or the one to its left, plus the number in the new row at that column. We keep only the best totals for the current row (in "best") and the previous row (in "oldbest").
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#define MAXR 1000intmax(int a, int b){return a > b ? a : b;}voidmain(void){int best[MAXR], oldbest[MAXR];int i, j, r, n, m;FILE *fin, *fout;fin = fopen("numtri.in", "r");assert(fin != NULL);fout = fopen("numtri.out", "w");assert(fout != NULL);fscanf(fin, "%d", &r);for(i=0; i<MAXR; i++)best[i] = 0;for(i=1; i<=r; i++) {memmove(oldbest, best, sizeof oldbest);for(j=0; j<i; j++) {fscanf(fin, "%d", &n);if(j == 0)best[j] = oldbest[j] + n;elsebest[j] = max(oldbest[j], oldbest[j-1]) + n;}}m = 0;for(i=0; i<r; i++)if(best[i] > m)m = best[i];fprintf(fout, "%d\n", m);exit(0);}
- USACO section 1.5.1 Number Triangles
- USACO Section 1.5 Number Triangles
- USACO Section 1.5 Number Triangles
- USACO Section 1.5 Number Triangles
- USACO-Section 1.5 Number Triangles(DP)
- USACO-Section 1.5 Number Triangles[贪心]
- [USACO 1.5.1] Number Triangles
- USACO 1.5.1 Number Triangles
- Section 1.5 Number Triangles
- USACO 1.5 Number Triangles
- USACO 1.5-Number Triangles
- USACO 1.5 Number Triangles
- USACO:1.5.1 Number Triangles 数字金字塔
- USCAO-Section 1.5 Number Triangles
- USACO 1.5 Number Triangles (numtri)
- USACO-1.5.2-Number triangles
- USACO Section 1.5 Number Triangles - 最最简单最最经典的DP..
- Usaco 1.5.1 数字金字塔(Number Triangles)
- VB实验报告
- VC6.0中如何修改工程的名字
- js 函数一览表
- SIP协议及与Freeswitch的关系
- 解决Ubuntu环境变量错误导致无法正常登录
- USACO section 1.5.1 Number Triangles
- 构建高性能的web站点学习笔记四------浏览器缓存
- ORA-15186: ASMLIB error function = [asm_open], error = [1], mesg = [Operation not permitted]
- 图像二值化
- VB作业题
- md5整理
- html select标签的selectIndex用法
- mysql常用操作
- 第一次编程序的感想