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. Day01_JAVA语言基础第一天

    1.计算机基础知识(理解) 1.计算机硬件 软件的基础设施,就是载体,计算机的硬件由五大组成部件:运算器,控制器,存储器,输入设备和输出设备 2.计算机软件 系统软件:windows,MAC,LINU ...

  2. 如何在dede栏目设置中添加自定义字段(dede二次开发-纯抄贴)

    如何在dede栏目设置中添加自定义字段 这个说法以前没有见到到,很少有客户会提出这样的二次要求,今天织梦者在网上转了一下看到了这样的一篇文章转过来与大家分享 鉴于这个教程没人发过,网上搜索的人也比较多 ...

  3. Think Python - Chapter 10 - Lists

    10.1 A list is a sequenceLike a string, a list is a sequence of values. In a string, the values are ...

  4. easyui accordion—手风琴格子始终展开和多个格子展开

    来源:http://www.cnblogs.com/tylerdonet/p/3531844.html 始终打开有时候可能会很管用,其实就是一个设置问题.这里就不再介绍引用的资源了,这里只看看html ...

  5. 关于时间的util类,以后方便查阅

    public static int lastDayOfMonth(int year, int month)  {    if (month == 2)    {      if (isLeapYear ...

  6. yii点击上传图片后立即显示

    结合yii上传做的图片上传后立即显示,自己琢磨的,有点low <script type="text/javascript">//下面用于图片上传预览功能function ...

  7. 使用GoodFeaturesToTrack进行关键点检测---29

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 关键点:是多个方向上亮度变化强的区域. opencv:版本是2.4. 侦测器:opencv有大量的关键点 ...

  8. HDU-1542 Atlantis(离散化+扫描线)

    题目大意:给n个矩形,可能重叠,求面积. 题目分析:线段树维护扫描线. 代码如下: # include<bits/stdc++.h> using namespace std; # defi ...

  9. Understanding virtualxid && transactionid

    对pg_locks视图中的virtualxid和transactionid字段感到困惑,经查阅资料,特此在此整理一下学习内容: pg_locks Columns Name Type Reference ...

  10. why does txid_current() assign new transaction-id?

    Naoya: Hi,hackers! I have a question about txid_current(). it is "Why does txid_current() assig ...