SPOJ #453. Sums in a Triangle (tutorial)
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)的更多相关文章
- codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...
- 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( ...
- SPOJ Triple Sums(FFT+容斥原理)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...
- SPOJ - Triple Sums
[传送门] FFT第一题! 构造多项式 $A(x) = \sum x ^ {s_i}$. 不考虑题目中 $i < j < k$ 的条件,那么 $A^3(x)$ 每一项对应的系数就是答案了. ...
- SPOJ 74. Divisor Summation 分解数字的因子
本题有两个难点: 1 大量的数据输入.没处理好就超时 - 这里使用buffer解决 2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的. 数值N因子 ...
- 多项式相关&&生成函数相关&&一些题目(updating...)
文章目录 多项式的运算 多项式的加减法,数乘 多项式乘法 多项式求逆 多项式求导 多项式积分 多项式取对 多项式取exp 多项式开方 多项式的除法/取模 分治FFT 生成函数 相关题目 多项式的运算 ...
- SPOJ TSUM Triple Sums(FFT + 容斥)
题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...
- SPOJ:Triple Sums(母函数+FFT)
You're given a sequence s of N distinct integers.Consider all the possible sums of three integers fr ...
- spoj TSUM - Triple Sums fft+容斥
题目链接 首先忽略 i < j < k这个条件.那么我们构造多项式$$A(x) = \sum_{1现在我们考虑容斥:1. $ (\sum_{}x)^3 = \sum_{}x^3 + 3\s ...
随机推荐
- 神逸之作:国产快速启动软件神品ALTRun
http://xbeta.info/altrun.htm 作者: ET民工和塞壬 日期: 2010-09-15 分类: windows 标签: quick-launch <神逸之作:国产快速启动 ...
- JavaScript的一些认识
最近看了一下<nodejs开发指南>发现nodejs在某些特定的领域由他自己的长处,适合密集计算但是业务逻辑比较简单的场景,如果做网站还是选择php吧,呵呵,这本书我除了第5章<用n ...
- 关于获取目录的N种方法 的汇总
前段时间在Global.asax.cs中的Session_End中使用Server.MapPath() 出现"服务器操作在此上下文中不可用"异常. 网络上给出的解决方案:Syste ...
- html部分---样式属性;
<!--大小--> width:宽度 height:高度 <!--背景与前景--> "background-color:#0F0; 背景颜色 background-i ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- JavaWeb学习记录(十九)——jstl自定义标签之简单标签
一.简单标签共定义了5个方法: setJspContext方法 setParent和getParent方法 setJspBody方法 doTag方法 二.方法介绍 osetJspContext方法 用 ...
- window7资源管理器一直重启(百度知道找到可用)
今天我的机器也出现这种问题:我的解决方式是,在开机时选择系统修复选项中的进入命令行方式(尝试过用安全模式,文件被占用,现象一样),然后cd C:\Users\Administrator\AppData ...
- Mac下的利器们介绍
先说说一些快捷键吧,从windows下过来还不很习惯: ctrl + 开关 关机等提示 ctrl+shift+开关 关闭显示器 cmd+option+v 相当于剪贴 cmd+tab,对于最小化了的窗口 ...
- sellect、poll、epoll
http://www.cnblogs.com/alex3714/p/4372426.html select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多 ...
- AWR分析。(shared_pool,sga_size大小设置)
Execute to Parse 指标反映了执行解析比 其公式为 1-(parse/execute) , 目标为100% 及接近于只 执行而不解析 在oracle中解析往往是执行的先提工作,但是通过游 ...