POJ1163 The Triangle用贪心方法和交替使用二维数组实现内存和时间优化
2017-01-21 11:32
501 查看
第一次写博客,如有不妥,请见谅。
啥也不说,先贴代码。
The Triangle
Description
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
Input
Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle,
all integers, are between 0 and 99.
Output
Your program is to write to standard output. The highest sum is written as an integer.
Sample Input
Sample Output
再帖思路和伪代码。
首解释题意,本题是要求从N*N的对角三角形从高位到低位,每个高位对应着每个低位和地位右侧的值,本题如果进行暴力的话,本题的空间复杂度大于或者接近N^2,但是本题每次进行运算时,实际有效的的行数,只有进行运算的行数和进行运算的上一行,也就是说,最大只有2*N处的内存,进行实际有效的运算,所以不如我们定义一个2*N的二位数组,通过量程for循环进行输入(每次输入的过程中,通过去与相应的行数与2进行取余数进行输入而实现数据在两行间的输入),并且,(在这里我要分析一下贪心的使用方法护着说原理:我们每次在本行输入的数,对应的相应的加法位都为另一行的对应列数和左侧数进行想加,所以,我们需要使用贪心查看左右的加法中的值中较大的值,来进行相加)使用贪心算法,进行加法与储存本次加法的最大值。在循环输入和贪心处理之后,我们会在本2*N的数组中取得所有行列中,相应加法的最大值,我们只需要进行排序或者遍历一遍数组,就可以获得所需要的最大值。
此处应该有代码
啥也不说,先贴代码。
The Triangle
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 46937 | Accepted: 28425 |
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
Input
Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle,
all integers, are between 0 and 99.
Output
Your program is to write to standard output. The highest sum is written as an integer.
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
再帖思路和伪代码。
首解释题意,本题是要求从N*N的对角三角形从高位到低位,每个高位对应着每个低位和地位右侧的值,本题如果进行暴力的话,本题的空间复杂度大于或者接近N^2,但是本题每次进行运算时,实际有效的的行数,只有进行运算的行数和进行运算的上一行,也就是说,最大只有2*N处的内存,进行实际有效的运算,所以不如我们定义一个2*N的二位数组,通过量程for循环进行输入(每次输入的过程中,通过去与相应的行数与2进行取余数进行输入而实现数据在两行间的输入),并且,(在这里我要分析一下贪心的使用方法护着说原理:我们每次在本行输入的数,对应的相应的加法位都为另一行的对应列数和左侧数进行想加,所以,我们需要使用贪心查看左右的加法中的值中较大的值,来进行相加)使用贪心算法,进行加法与储存本次加法的最大值。在循环输入和贪心处理之后,我们会在本2*N的数组中取得所有行列中,相应加法的最大值,我们只需要进行排序或者遍历一遍数组,就可以获得所需要的最大值。
此处应该有代码
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #define maxn 105; int a[2][maxn]; using namespace std; int main(){ int n; while(scnaf("%d",&n)==1){ memset(a,0,sizeof(a)); int k=0; for(int h=1;h<=n;h++){ k=h%2; for(int l=1;l<=n;l++){ scanf("%d",&a[k][l]); a[k][l]+=max(a[k^1][l-1],a[k^1][l]);//本处为贪心算法 } } int fndMax=0; for(int i=1;i<=n;i++){ fndMax=max(fndMax,a[n%2][i]); } printf("%d\n",fndMax); } return 0; }
相关文章推荐
- 使用ASM4.0实现AOP的功能,监控每个方法的执行时间
- 转载:自定义浏览DWG控件的实现方法, 有时间研究一下(AcGsView使用)
- 使用ntpdate工具校正linux服务器时间(实现方法)
- CUDA入门4.1——使用CUDA实现鱼眼畸变的方法优化(读取外部信息)
- 使用ASM4.0实现AOP的功能,监控每个方法的执行时间
- Python使用logging结合decorator模式实现优化日志输出的方法
- Android编程实现悬浮窗获取并显示当前内存使用量的方法
- 使用ASM4.0实现AOP的功能,监控每个方法的执行时间
- 编写查找一个单链表特定元素的程序。分别使用递归和非递归方法实现,并比较它们的运行时间。
- Delphi实现限定软件使用时间的方法
- Android编程使用加速度传感器实现摇一摇功能及优化的方法详解
- 使用MSVC进行SSE2优化时内存对齐的方法
- 安卓性能调优:内存使用分析和方法调用优化
- 安卓性能调优:内存使用分析和方法调用优化
- 使用DateTime的ParseExact方法实现特殊日期时间的方法详解
- 使用Spring的AOP实现接口方法执行时间记录
- android app性能优化-有哪些方法可以减少内存使用:
- Python使用logging结合decorator模式实现优化日志输出的方法
- 使用内存回流的方法来实现将image的内容转换为 byte[]
- 字符数据在内存中的存储形式及其使用方法(C实现)