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. Azure 云助手正式发布

    Azure云服务在中国市场风生水起,越来越多的用户选择Azure作为平台将业务转向云端.随着移动互联网在中国的蓬勃发展,手机应用的体验深入人们的生活及工作.用户管控云服务也不应该只局限于电脑前,而是可 ...

  2. 解决load 函数无法赋予变量名的问题

    以前非常喜欢使用load函数,因为简单,而且存储相对较大的matrix.list文件更为方便.但是load函数有一个问题是在使用其过程中无法对其载入的data赋予变量名: # save data x ...

  3. matplotlib example

    # This file generates an old version of the matplotlib logofrom __future__ import print_function# Ab ...

  4. banner轮播图js

    例子1: if(!$('.side_ul ul').is(":animated")){            var wli = $('.side_ul li').width()+ ...

  5. 求10000以内n的阶乘

    总时间限制:  5000ms 内存限制:  655360kB 描述 求10000以内n的阶乘. 输入 只有一行输入,整数n(0<=n<=10000). 输出 一行,即n!的值. 样例输入 ...

  6. hdu1811 并查集+拓扑序

    题意:现在有一个排名系统,有一系列信息,分别是 > < = 的比较,而如果最终相等,就会将这些相等的按照序号从小到大排,问给出的信息是否可以确定完整的排序. 由于如果很多点相等,他们肯定能 ...

  7. 【BZOJ1010】【HNOI2008】玩具装箱

    继续看黄学长代码 原题: P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1.. ...

  8. 黑马程序员——JAVA基础之IO流FileReader,FileWriter

    ------- android培训.java培训.期待与您交流! ---------- IO(Input Output)流  IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 J ...

  9. VIM进阶学习之几种模式和按键映射

    Map是Vim强大的一个重要原因,可以自定义各种快捷键,用起来自然得心应手. vim里最基本的map用法也就是 :map c a 这里把c映射成了a,在map生效的情况下,按下c就等同于按下了a 当然 ...

  10. 无shell情况下的mysql远程mof提权利用方法详解

    扫到一个站的注入<ignore_js_op> 在havij中得到mysql数据库中mysql库保存的数据库密码:<ignore_js_op> 有时候发现1.15版的还是最好用, ...