【POJ2479】Maximum sum(动态规划,DP)
2011-05-03 15:22
309 查看
跟之前的求子段最大和差不多,不知道请看http://blog.pureisle.net/?p=266
这里求的是两个子段最大和,注意看数据,两个子段均不能为空(偶在这里多两次WA)。。。。另附POJ,discuss里边别人粘贴出来了一层循环写法。。。道理都差不多,时间也差不多。
做人厚道啊,转载请注明出处:三江小渡的博客
这里求的是两个子段最大和,注意看数据,两个子段均不能为空(偶在这里多两次WA)。。。。另附POJ,discuss里边别人粘贴出来了一层循环写法。。。道理都差不多,时间也差不多。
#include <cstdio> #include <algorithm> using namespace std; int a[1000001]; int res[1000001]; int main(){ freopen("1.txt","r",stdin); int n,x; scanf("%d",&n); while(n--){ scanf("%d",&x); int ans=-9999999,mymax=-9999999,temp=0; for (int i=1;i<=x;i++){ scanf("%d",&a[i]); res[i]=max(res[i-1]+a[i],a[i]); } for (int i=x;i>=2;i--){ temp +=a[i]; if(temp>mymax) mymax=temp; if(ans<res[i-1]+mymax) ans=res[i-1]+mymax; if(temp<0) temp=0; } printf("%d/n",ans); } }
#include<cstdio> int max(int x, int y ) {return x>y? x : y; } int main() { int cas,n; scanf("%d",&cas); while ( cas -- ) { scanf("%d",&n); int a, b , ans, a_max,k; a = b = a_max = 0; ans = -99999999; scanf("%d",&k); b = a_max = k; a = max(k , 0 ); for (int i = 1; i<n; i++) { scanf("%d",&k); a = a + k; if ( a < 0 ) a = 0; b = max( b+k , a_max+k ); ans = max( ans , b); a_max = max( a, a_max); b = max( b , a_max); } printf("%d/n",ans); } }
做人厚道啊,转载请注明出处:三江小渡的博客
相关文章推荐
- hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++
- Maximum Subsequence Sum 【DP】
- 【树形dp】Binary Tree Maximum Path Sum
- POJ 2479 Maximum sum & POJ 2573 Max Sequence (DP,最大连续子串和)
- 用动态规划解决(maximum sum subarray and maximum product subarray)
- UVA108 Maximum Sum dp/暴力+简化
- Problem 18 Maximum path sum I (dp)
- PAT (Advanced Level) 1007 - Maximum Subsequence Sum(DP)
- patest_1007_Maximum Subsequence Sum_(dp)(思维)
- Maximum Subsequence Sum[PAT1007][PTA01-复杂度2]——动态规划
- Problem 67 Maximum path sum II (dp)
- Binary Tree Maximum Path Sum [leetcode] dp
- 杭电ACM OJ 1024 Max Sum Plus Plus 动态规划 二维dp+滚动数组dp优化
- leetcode 327. Count of Range Sum 字段和问题 + 动态规划DP解决 + 字段数 + 暴力求解真好
- poj 2479 dp Maximum sum求两段连续不重叠子段的最大和
- POJ 3132 Sum of Different Primes 动态规划 DP 0-1背包问题
- kuangbin求带飞DP1 Max Sum Plus Plus(动态规划+滚动数组)
- 杭电HDOJ 5586 SUM(dp)(动态规划)
- POJ 3132 Sum of Different Primes 动态规划 DP 0-1背包问题
- uva 10827 - Maximum sum on a torus(dp)