USACO 1.5 Number Triangles 数塔问题

来源:互联网 发布:最近火的网络用语 编辑:程序博客网 时间:2024/05/22 01:54

虽然是简单的数塔问题,

f[i][j] = a[i][j] + max(a[i-1][j], a[i][j]); 输出max (f[i][j])即可。

但是直接开a[1000][1000]占用内存太大,所以使用滚动数组。当然取余的方法是可行的~但是我还是更喜欢用指针的方法,用交换指针的方式来解决数组少的情况…… 具体程序见下~


/*TASK:numtriLANG:C++*/#include <iostream>#include <cstdio>int n,a[1001]={0}, b[1001]={0};int *x = a, *y = b, *t, ans(0);int main(){freopen("numtri.in", "r", stdin);freopen("numtri.out", "w", stdout);std::ios::sync_with_stdio(false);std::cin >> n >> a[1];for (int i = 2; i <= n; ++i){for (int j = 1; j <= i; ++ j){std::cin >> y[j];ans = std::max(y[j] = y[j] + std::max(x[j], x[j - 1]), ans);}t = x; x = y; y = t;}std::cout<<ans<<std::endl;return 0;}


0 0
原创粉丝点击