[kuangbin带你飞]专题十二 基础DP 题解

专题十二 基础DP

  • HDU 1024 Max Sum Plus Plus
  • HDU 1029 Ignatius and the Princess IV
  • HDU 1069 Monkey and Banana
  • HDU 1074 Doing Homework
  • HDU 1087 Super Jumping! Jumping! Jumping!
  • HDU 1114 Piggy-Bank
  • HDU 1176 免费馅饼
  • HDU 1260 Tickets
  • HDU 1257 最少拦截系统
  • HDU 1160 FatMouse's Speed
  • POJ 1015 Jury Compromise
  • POJ 1458 Common Subsequence
  • POJ 1661 Help Jimmy
  • POJ 2533 Longest Ordered Subsequence
  • POJ 3186 Treats for the Cows
  • HDU 1078 FatMouse and Cheese
  • HDU 2859 Phalanx
  • POJ 3616 Milking Time
  • POJ 3666 Making the Grade

 

 

HDU 1024 Max Sum Plus Plus

Problem Description

Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.

Given a consecutive number sequence S1, S2, S3, S4 ... Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + ... + Sj (1 ≤ i ≤ j ≤ n).

Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i1, j1) + sum(i2, j2) + sum(i3, j3) + ... + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is not allowed).

But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(ix, jx)(1 ≤ x ≤ m) instead. ^_^

Input

Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 ... Sn.
Process to the end of file.

Output

Output the maximal summation described above in one line.

Sample Input

1 3 1 2 3

2 6 -1 4 -2 3 -2 3

Sample Output

6

8

Hint

Huge input, scanf and dynamic programming is recommended.

 题意,m个最大字段和

 

 

 

 

 

 

HDU 1029 Ignatius and the Princess IV

问题描述

“好吧,你不是太糟糕,他们......但你永远无法通过下一次测试。” feng5166说。

“我会告诉你一个奇数N,然后是N个整数。它们之间会有一个特殊的整数,你告诉我在告诉你所有整数之后哪个整数是特殊的整数。” feng5166说。

“但特殊整数的特征是什么?” 伊格内修斯问道。

“整数至少会出现(N + 1)/ 2次。如果你找不到合适的整数,我会杀死公主,你也将成为我的晚餐。哈哈哈哈......”feng5166说。

你能找到Ignatius的特殊整数吗?

输入

输入包含几个测试用例。每个测试用例包含两行。第一行由一个奇数N(1 <= N <= 999999)组成,表示feng5166将告诉我们的英雄的整数。第二行包含N个整数。输入由文件末尾终止。

产量

对于每个测试用例,您只需输出一行,其中包含您找到的特殊编号。

样本输入

5

1 3 2 3 3

11

1 1 1 1 1 5 5 5 5 5 5

7

1 1 1 1 1 1 1

样本输出

3

5

1

 

#include
#include
using namespace std;

const int MAX = 1e6+5;

int main(){
    int n,cnt,ans,a;

    while(~scanf("%d",&n)){
        ans=0,cnt=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a);
            if(cnt==0){
                ans = a;
                cnt++;
            }else if(ans==a)
                cnt++;
            else
                cnt--;
        }
        printf("%d\n",ans);
    }

	return 0;
}

 

 

你可能感兴趣的:(树形dp)