It is a small fun problem to solve. Since only a max sum is required (no need to print path), we can only keep track of the max value at each line. Basically it is still a human labor simulation work. To be more specifically, we need keep track of a line of max sums.

But there are 1000*100 input, so special optimization is required. The naive solution would copy data between 2 arrays, and actually that's not necessary. Logically, we only have 2 arrays - result array and working array. After one line is processed, working array can be result array for next line, so we can only switch pointers to these 2 arrays, to avoid expensive memory copy.

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; int aret[] = {};
int atmp[] = {}; int proc_line(int r, int *aret, int *atmp)
{ if(r == )
{
int in = ; cin >>in;
aret[] = in;
atmp[] = in;
return in;
} // Get current line and calc
int rmax = -;
for(int i = ; i < r; i ++)
{
int tmp = ; scanf("%d", &tmp);
int prevInx = i == ? : i - ;
int prevVal = aret[prevInx];
int currMax = (i + ) == r ? tmp + prevVal : max(tmp + aret[i], tmp + prevVal);
atmp[i] = currMax;
rmax = currMax > rmax ? currMax :rmax;
} return rmax;
} int main()
{ int runcnt = ;
cin >> runcnt;
while(runcnt --)
{
int rcnt = ; cin >> rcnt;
int ret = ;
for(int i = ; i <= rcnt; i ++)
{
bool evenCase = i % == ;
ret = proc_line(i, !evenCase? aret:atmp, !evenCase?atmp:aret);
}
cout << ret << endl;
}
return ;
}

SPOJ #453. Sums in a Triangle (tutorial)的更多相关文章

  1. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

  2. 2018.11.18 spoj Triple Sums(容斥原理+fft)

    传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai​,aj​,ak​( ...

  3. SPOJ Triple Sums(FFT+容斥原理)

    # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...

  4. SPOJ - Triple Sums

    [传送门] FFT第一题! 构造多项式 $A(x) = \sum x ^ {s_i}$. 不考虑题目中 $i < j < k$ 的条件,那么 $A^3(x)$ 每一项对应的系数就是答案了. ...

  5. SPOJ 74. Divisor Summation 分解数字的因子

    本题有两个难点: 1 大量的数据输入.没处理好就超时 - 这里使用buffer解决 2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的. 数值N因子 ...

  6. 多项式相关&&生成函数相关&&一些题目(updating...)

    文章目录 多项式的运算 多项式的加减法,数乘 多项式乘法 多项式求逆 多项式求导 多项式积分 多项式取对 多项式取exp 多项式开方 多项式的除法/取模 分治FFT 生成函数 相关题目 多项式的运算 ...

  7. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...

  8. SPOJ:Triple Sums(母函数+FFT)

    You're given a sequence s of N distinct integers.Consider all the possible sums of three integers fr ...

  9. spoj TSUM - Triple Sums fft+容斥

    题目链接 首先忽略 i < j < k这个条件.那么我们构造多项式$$A(x) = \sum_{1现在我们考虑容斥:1. $ (\sum_{}x)^3 = \sum_{}x^3 + 3\s ...

随机推荐

  1. 去除reads中的pcr 重复,fastquniq

    改编: python ~/tools2assemble/run_fastuniq.py SHT-3K-1_1.fq.gz SHT-3K-1_2.fq.gz 好像不支持gz文件,要先解压 http:// ...

  2. Linux下Find命令的使用

    Find命令的一般形式为: findpathname-options[-print-exec-ok] pathname:find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录. - ...

  3. ZOJ 1088 System Overload

    原题链接 题目大意:浙大的破网络容量有限,太多人同时使用BBS就会系统崩溃.为了保持系统正常工作,过载时就需要切断部分用户.规则是把玉泉所有的建筑从1到n编号,设定一个常数m.从1开始数,第m幢建筑的 ...

  4. ZOJ 1002 Fire Net

    题目大意:有一个4*4的城市,其中一些格子有墙(X表示墙),在剩余的区域放置碉堡.子弹不能穿透墙壁.问最多可以放置几个碉堡,保证它们不会相互误伤. 解法:从左上的顶点开始遍历,如果这个点不是墙,做深度 ...

  5. 在web.xml中配置error-page

    在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一.   通过错误码来配置error ...

  6. codeforces 192 c

    link: http://codeforces.com/contest/330/problem/C broute force but you must be careful about some tr ...

  7. Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明

    Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明   开发环境:STM32F103RB(128K Flash 20K RAM)+MDK3.50+JLINK V7(v ...

  8. (转) Written Memories: Understanding, Deriving and Extending the LSTM

    R2RT   Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...

  9. JSBinding + SharpKit / 生成JavaScript绑定

    将 UnityEngine 的代码导出到 JavaScript.就可以在 JavaScript 中使用 Unity 的功能. 如何导出? 将需要导出的类添加到 JSBindingSetting.cla ...

  10. 前端优化:DNS预解析提升页面速度

    在网页体验中我们常会遇到这种情况,即在调用百度联盟.谷歌联盟以及当前网页所在域名外的域名文件时会遇到请求延时非常严重的情况.那么有没有方法去解决这种请求严重延时的现象呢? 一般来说这种延时的原因不会是 ...