【hdu1024】简单dp

http://acm.hdu.edu.cn/showproblem.php?pid=1024 最大m字段和,题目就不多说了,经典dp

这题坑爹。。。首先不说明m的范围(n<=1000000),还以为有O(n)的算法,吓得不敢做。其次,按照题目给的范围完全可能超int,但是数据事实上int远远够了,害得我__int64读比读int慢两倍,直接TLE。就是因为TLE的原因,害得我数组初始化没有考虑全(其实应该怪自己没有考虑清楚->_->),但事实上,完全可以每次memset一遍,因为超时,不敢==。各种原因,几分钟敲完的水题调了1个多小时!我也是醉了。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <map>
 7 #include <stack>
 8 #include <string>
 9 #include <ctime>
10 #include <queue>
11 #define mem0(a) memset(a, 0, sizeof(a))
12 #define mem(a, b) memset(a, b, sizeof(a))
13 #define lson l, m, rt << 1
14 #define rson m + 1, r, rt << 1 | 1
15 #define eps 0.0000001
16 #define lowbit(x) ((x) & -(x))
17 #define memc(a, b) memcpy(a, b, sizeof(b))
18 #define x_x(a) ((a) * (a))
19 #define LL __int64
20 #define DB double
21 #define pi 3.14159265359
22 #define MD 10000007
23 #define INF (int)1e9
24 #define max(a, b) ((a) > (b)? (a) : (b))
25 using namespace std;
26 int f[1000010][2][2];
27 int a[1000010];
28 int main()
29 {
30         //freopen("input.txt", "r", stdin);
31         //freopen("output.txt", "w", stdout);
32         int n, m;
33         while(~scanf("%d%d", &m, &n))  {
34                 for(int i = 1; i <= n; i++) {
35                         scanf("%d", a + i);
36                 }
37                 for(int i = 0; i <= n; i++) {
38                         f[i][0][0] = 0;
39                         f[i][0][1] = -INF;
40                 }
41                 int now = 1;
42                 for(int j = 1; j <= m; j++) {
43                         f[0][now][0] = f[0][now][1] = - INF;
44                         for(int i = 1; i <= n; i++) {
45                                 if(i < j) {
46                                         f[i][now][1] = f[i][now][0]= -INF;
47                                         continue;
48                                 }
49                                 f[i][now][1] = max(max(f[i - 1][now ^ 1][0], f[i - 1][now ^ 1][1]), f[i - 1][now][1]) + a[i];
50                                 f[i][now][0] = max(f[i - 1][now][0], f[i - 1][now][1]);
51                         }
52                         now ^= 1;
53                 }
54                 printf("%d\n", max(f[n][now ^ 1][0], f[n][now ^ 1][1]));
55         }
56         return 0;
57 }

时间: 2024-08-25 00:30:39

【hdu1024】简单dp的相关文章

POJ 3250 Bad Hair Day 简单DP 好题

Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a sp

hdu1207 汉诺塔II 简单dp

本文出自:http://blog.csdn.net/svitter 题意:汉诺塔,多了一根柱子,问你寻找最快的移动次数. dp [ n ] = dp [ n - j ] * 2 + pow( 2, j ) - 1; 就是把j个汉诺塔移到一根上dp[ n - j ],然后就是普通的汉诺塔问题,即2^n - 1次移动, 然后把剩下的移动过去dp [ n - j ]. 注意pow(2, j )可能超出long long int范围.写二的次方的时候也可用移位算法. #include <iostream

POJ1088:滑雪(简单dp)

题目链接:  http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一下序,因为只能高度递减才能滑行.之后就很简单了,就是简单DP. 即:要求的滑坡是一条节点递减并依次相邻的最长路径,可以先根据高度将所有的点进行排序,在i点的时候,遍历0~i-1个点(升序排序,i前面的点的高度一定小于等于i),取相邻点间的大的路径长度 代码如下: #include <iostream

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

hdu1087 简单DP

I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HD

2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题

听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到100即可 算最大GPA的时候,首先每个科目分配到60分,然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到85即可,如果还有REMAIN POINT,就FOR循环下来加到100上限即可 不会DP 阿 QAQ 过段时间得好好看DP了  =  = 于是默默的把这题标记为<水题集> //

Codeforces 41D Pawn 简单dp

题目链接:点击打开链接 给定n*m 的矩阵 常数k 下面一个n*m的矩阵,每个位置由 0-9的一个整数表示 问: 从最后一行开始向上走到第一行使得路径上的和 % (k+1) == 0 每个格子只能向或走一步 求:最大的路径和 最后一行的哪个位置作为起点 从下到上的路径 思路: 简单dp #include <cstdio> #include <algorithm> #include<iostream> #include<string.h> #include &

HDU 4826 简单DP

Problem Description 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,且只能向上向下向右走以前没有走过的格子,每一个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币可以为负,需要给强盗写欠条),度度熊刚开始时身上金币数为0,问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T(T < 200),表示共有T组数据.每组数据的

UVA - 11584 划分字符串的回文串子串; 简单dp

/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单dp 题目大意: 给一个字符串, 要求把它分割成若干个子串,使得每个子串都是回文串.问最少可以分割成多少个. 定义:dp[i]表示前0~i内的字符串划分成的最小回文串个数: dp[i] = min(dp[j]+1 | j+1~i是回文串); 先预处理flag[i][j]表示以i~j内的字符串为回文串

poj 2193 Lenny&#39;s Lucky Lotto Lists 简单dp

//poj 2193 //sep9 #include <iostream> using namespace std; typedef __int64 INT; INT dp[16][2048]; int n,m; int main() { int cases,t=0; scanf("%d",&cases); while(cases--){ scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp));