首页 > 代码库 > 【hdu1024】简单dp

【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 << 114 #define rson m + 1, r, rt << 1 | 115 #define eps 0.000000116 #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 __int6420 #define DB double21 #define pi 3.1415926535922 #define MD 1000000723 #define INF (int)1e924 #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 }
View Code

 

【hdu1024】简单dp