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. druid 源码分析与学习(含详细监控设计思路的彩蛋)(转)

    原文路径:http://herman-liu76.iteye.com/blog/2308563  Druid是阿里巴巴公司的数据库连接池工具,昨天突然想学习一下阿里的druid源码,于是下载下来分析了 ...

  2. html中的元素和节点

    元素(Element)和结点(Node)的区别, 元素是一个小范围的定义,必须是含有完整信息的结点才是一个元素,例如<div>...</div>. 但是一个结点不一定是一个元素 ...

  3. 学习iOS的一些网站收藏

    1,CocoaChina:http://www.cocoachina.com/ 2,Code4App:http://code4app.com/ 3,梦维:http://www.dreamingwish ...

  4. meta 标签 关键字 用处

    您的个人网站即使做得再精彩,在“浩瀚如海”的网络空间中,也如一叶扁舟不易为人发现,如何推广个人网站, 人们首先想到的方法无外乎以下几种: l 在搜索引擎中登录自己的个人网站 l 在知名网站加入你个人网 ...

  5. TFTP网络协议分析---15

    TFTP网络协议分析 周学伟 文档说明:所有函数都依托与两个出口,发送和接收. 1:作为发送时,要完成基于TFTP协议下的文件传输,但前提是知道木的PC机的MAC地址,因为当发送TFTP请求包时必须提 ...

  6. MySQL日志功能

    1.查询日志 log={ON|OFF}:是否记录所有语句的日志信息于一般查询日志文件(general_log); log_output={TABLE|FILE|NONE},TABLE和FILE可以同时 ...

  7. psp0

    周活动总结表 姓名:苗堃                                                                                         ...

  8. UVALive-7303 Aquarium (最小生成树)

    题目大意:在nxm的方格中,每一个1x1的小方格中都有一堵沿对角线的墙,并且每堵墙都有一个坚固程度,这些墙将nxm的方格分割成了若干个区域.现在要拆除一些墙,使其变成一个区域. 题目分析:将区域视作点 ...

  9. [JSOI2008] 完美的对称

    题目描述 在峰会期间,必须使用许多保镖保卫参加会议的各国代表.代表们除了由他自己的随身保镖保护外,组委会还指派了一些其他的特工和阻击手保护他们.为了使他们的工作卓有成效,使被保卫的人的安全尽可能得到保 ...

  10. PHP学习——数据类型

    PHP的数据是存在类型的概念的,弱类型指的是变量可以存储任何类型!一共8种,分别是:整型.浮点型.布尔型.字符串(标量类型) 数组.对象(符合类型) null.资源(特殊类型) 分成三大类: 标量类型 ...