看了一眼题目,简单dp问题,但超时了一晚上,试了各种方法无法解决,最终放弃java,改用C直接过,我哭了。。。。
#include<stdio.h> #include<string.h> #define maxn 200010 int dp[maxn],ans[maxn],map[maxn]; int max(int x,int y) { return x>y?x:y; } int main() { int i,j; int n,m; int xx; while(scanf("%d%d",&n,&m)!=EOF) { int cc,dd; int aa,bb; int res; for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&xx); if (j==0) { aa = xx; bb = xx; }else if (j==1) { bb = max(aa, xx); }else { int tmp = bb; bb = max(bb, aa+xx); aa = tmp; } } if (i==0) { cc = bb; dd = bb; }else if (i==1) { dd = max(dd, bb); }else { int tmp1 = dd; dd = max(dd, cc+bb); cc = tmp1; } res = dd; } printf("%d\n",res); } }
下面是永远超时的java代码:
import java.util.Scanner; public class hdu2845_2 { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); int res = 0; int cc = 0; int dd = 0; for (int i = 0; i < n; i++) { int aa = 0; int bb = 0; for (int j = 0; j < m; j++) { int xx = sc.nextInt(); if (j==0) { aa = xx; bb = xx; }else if (j==1) { bb = Math.max(aa, xx); }else { int tmp = bb; bb = Math.max(bb, aa+xx); aa = tmp; } } if (i==0) { cc = bb; dd = bb; }else if (i==1) { dd = Math.max(dd, bb); }else { int tmp1 = dd; dd = Math.max(dd, cc+bb); cc = tmp1; } res = dd; } System.out.println(res); } } }
标签:aa,bb,int,dd,问题,cc,xx,hdu2845dp From: https://www.cnblogs.com/xiaohuangTX/p/18257808