[usaco] Number Triangles

简介: <center><h1>Number Triangles</h1></center><p>Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the to

Number Triangles

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be 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.

          7

        3   8

      8   1   0

    2   7   4   4

  4   5   2   6   5

In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.

PROGRAM NAME: numtri

INPUT FORMAT

The first line contains R (1 <= R <= 1000), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers are non-negative and no larger than 100.

SAMPLE INPUT (file numtri.in)

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

OUTPUT FORMAT

A single line containing the largest sum using the traversal specified.

SAMPLE OUTPUT (file numtri.out)

30

 

解体的要点在于,把数据转化成二叉树。然后自底向上计算每个节点的高度。但是注意,由于每个节点同时作为两个节点的子节点(一个是左子节点,一个是右子节点)。

这样可能会造成每个节点计算两次,于是设计每个几点的高度初始值为-1. 当判断高度不会-1时,就不必在计算该节点的高度了。之所以初始值不为0,是因为某个test case全为0.

这样还是会造成重复的递归。

我的解法:

/*
ID: yunleis2
PROG: numtri
LANG: C++
*/

#include<iostream>
#include<fstream>
#include<queue>
using namespace std;

#define max(a,b) (a>b?a:b)

class node
{
public:
 node * left;
 node * right;
 int value;
 int length;
 node(node * l,node * r)
 {
  left=l;
  right=r;
 }
 node ()
 {left=NULL;right=NULL;value=0;length=-1;}

};
int  search( node *root);
void print(node *);
int main()
{
 fstream fin("numtri.in",ios::in);
 int line;
 fin>>line;
 int size=(line+1)*line/2;
 node * root=new node();

 queue<node *> q;
 fin>>root->value;
 q.push(root);
 for(int i=1;i<line;i++)
 {
  node * last=NULL;
  for(int j=1;j<=i;j++)
  {
   node * ptr=q.front();
   q.pop();
   if(j==1)
   {
    ptr->left=new node();
    fin>>ptr->left->value;
    q.push(ptr->left);
   }
   else
   {
    ptr->left=last->right;
   }
   ptr->right=new node();
   fin>>(ptr->right)->value;
   last=ptr;
   
   q.push(ptr->right);
  }
 }
 while(!q.empty())
  q.pop();
 //print(root);
 int length=search(root);
 fstream fout("numtri.out",ios::out);
 fout<<root->length<<endl;
 //system("pause");
 
}
void print(node * root)
{
 if(root!=NULL)
 {
  cout<<root->value;
  print(root->left);
  print(root->right);
 }
}
int  search( node *root)
{
 if(root==NULL)
  return 0;
 if(root->length!=-1)
  return root->length;
 root->length=max(search(root->right),search(root->left))+root->value;
 return root->length;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

usaco的解法 Number Triangles
Russ Cox

We keep track (in the "best" array) of total for the best path ending in a given column of the triangle. Viewing the input, a path through the triangle always goes down or down and to the right. To process a new row, the best path total ending at a given column is the maximum of the best path total ending at that column or the one to its left, plus the number in the new row at that column. We keep only the best totals for the current row (in "best") and the previous row (in "oldbest").

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAXR 1000
int
max(int a, int b)
{
	return a > b ? a : b;
}
void
main(void){
	int best[MAXR], oldbest[MAXR];
	int i, j, r, n, m;
	FILE *fin, *fout;
	fin = fopen("numtri.in", "r");
	assert(fin != NULL);
	fout = fopen("numtri.out", "w");
	assert(fout != NULL);
	fscanf(fin, "%d", &r);

	for(i=0; i<MAXR; i++)
		best[i] = 0;

	for(i=1; i<=r; i++) {
		memmove(oldbest, best, sizeof oldbest);
		for(j=0; j<i; j++) {
			fscanf(fin, "%d", &n);
			if(j == 0)
				best[j] = oldbest[j] + n;
			else
				best[j] = max(oldbest[j], oldbest[j-1]) + n;
		}
	}
	m = 0;
	for(i=0; i<r; i++)
		if(best[i] > m)
			m = best[i];
 
	fprintf(fout, "%d\n", m);
	exit(0);
}

 

 

 

目录
相关文章
P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
洛谷P1216-[USACO1.5][IOI1994]数字三角形 Number Triangles(DP)
洛谷P1216-[USACO1.5][IOI1994]数字三角形 Number Triangles(DP)
洛谷P1216-[USACO1.5][IOI1994]数字三角形 Number Triangles(DP)
|
9月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
83 1
|
2月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
3月前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
27 0
|
9月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
30 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
81 0
LeetCode 414. Third Maximum Number
|
存储
LeetCode 313. Super Ugly Number
编写一段程序来查找第 n 个超级丑数。 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。
84 0
LeetCode 313. Super Ugly Number
|
算法
LeetCode 306. Additive Number
累加数是一个字符串,组成它的数字可以形成累加序列。 一个有效的累加序列必须至少包含 3 个数。除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和。 给定一个只包含数字 '0'-'9' 的字符串,编写一个算法来判断给定输入是否是累加数。 说明: 累加序列里的数不会以 0 开头,所以不会出现 1, 2, 03 或者 1, 02, 3 的情况。
108 0
LeetCode 306. Additive Number
|
算法
LeetCode 268. Missing Number
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
77 0
LeetCode 268. Missing Number