[Swust OJ 893]--Blocks
题目链接:http://acm.swust.edu.cn/problem/893/
Josh loves playing with blocks. Currently, he has N blocks, labeled 0 through N-1. The heights of all blocks are positive integers. More precisely, for each i, the height of block i is blockHeights[i]. Josh is interested in making the tallest block tower possible. He likes all his towers to follow three simple rules:
1.The blocks must be stacked in a single column, one atop another. The height of the tower is simply the sum of heights of all its blocks.
2.The labels of blocks used in the tower must increase from the bottom to the top. In other words, whenever Josh places box x on top of box y, we have x > y.
3.Josh will never place a box of an even height on top of a box of an odd height.
you need find out the height of the tallest possible block tower Josh can build.
Each test case starts with a number N (0 < N <= 1000000) -- the total number of blocks,followed by N positive numbers.All the numbers in the input are less than 1000.A test case with N = 0 denotes the end of input. This test case is not to be processed.
Print the tallest possible block tower on a single line for each test case.
1
2
3
4
5
6
|
2
4 7
2
7 4
5
49 2 49 2 49
|
1
2
3
|
11
7
147
|
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 int dp[1000002][2]; 3 #define max(a,b) a>b?a:b 4 int main() 5 { 6 int n, height, i; 7 while (~scanf("%d", &n), n){ 8 for (i = 0; i < n; i++){ 9 scanf("%d", &height); 10 if (height & 1){ 11 if (!i){ 12 dp[i][0] = height; 13 dp[i][1] = 0; 14 } 15 else{ 16 dp[i][0] = max(dp[i - 1][0] + height, dp[i - 1][1] + height); 17 dp[i][1] = dp[i - 1][1]; 18 } 19 } 20 else{ 21 if (!i){ 22 dp[i][1] = height; 23 dp[i][0] = 0; 24 } 25 else{ 26 dp[i][1] = dp[i - 1][1] + height; 27 dp[i][0] = dp[i - 1][0]; 28 } 29 } 30 } 31 printf("%d\n", max(dp[n - 1][0], dp[n - 1][1])); 32 } 33 return 0; 34 }