The 51Nod first-level algorithm has a number of 1002 data towers, and the 51nod algorithm has a number of 1002 data towers.

Source: Internet
Author: User

The 51Nod first-level algorithm has a number of 1002 data towers, and the 51nod algorithm has a number of 1002 data towers.

--- Restore content start ---

1 # include <stdio. h> 2 # include <stdlib. h> 3 # define max (x, y) (x)> (y )? (X) :( y) 4 int main () {5 int n; 6 int I, j, k; 7 scanf ("% d", & n ); // Layer 8 k = (n + 1) * n/2; // total number of all nodes 9 int * a = (int *) malloc (sizeof (int) * k ); // dynamic array, record the number of each node 10 for (I = 0; I <k; I ++) {// enter the number of each node 11 scanf ("% d ", & a [I]); 12} 13 k-= n; // The first node of the penultimate layer 14 for (I = K-1, j = 0; I> = 0; I --) {15 a [I] = a [I] + max (a [I + n], a [I + n-1]); // greedy, add the maximum value of the Left or Right node of the lower layer to itself 16 if (+ + j = N-1) {// After traversing the layer, n-117 n --; 18 j = 0; 19} 20} 21 printf ("% d \ n", a [0]); 22 return 0; 23}

Idea: Starting from the penultimate row, add the value of each node to the maximum value of the left and right nodes of the next layer, and traverse the node layer by layer until the vertex ends, and the vertex content is output.

--- Restore content end ---

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.