武大oj的题。题目传送门。
以我看,我的代码最大就开两个数组a[1000],b[1000],怎么会用到553728KB的内存。提交了好多次,都是这个数量级的内存消耗。
求大师们拉小弟一把呀:)
Problem id: 1383Memory: 553728KB Time: 6610msLanguage: Java Result: Memory Limit Exceeded
code:
1 import java.util.*;
2 import java.io.*;
3
4 public class Main
5 {
6 public static final int MAXN = 1000;
7 //public static final String INPUT = "in.txt";
8 static int max3(int x, int y, int z)
9 {
10 return (x>y?x:y)>z?(x>y?x:y):z;
11 }
12 public static void main(String[] args)
13 {
14 /*
15 try
16 {
17 FileInputStream instream = new FileInputStream(INPUT);
18 System.setIn(instream);
19 }
20 catch(Exception e)
21 {
22 System.err.println("input error!");
23 }
24 */
25 int[] a = new int[MAXN];
26 int[] b = new int[MAXN];
27 int t,r,c,res;
28 Scanner cin = new Scanner(System.in);
29 t=cin.nextInt();
30 for(int cas=1; cas<=t; cas++)
31 {
32 r=cin.nextInt();
33 c=cin.nextInt();
34
35 //initialize
36 Arrays.fill(a, 0);
37 Arrays.fill(b, 0);
38
39 if(r==1 && c==1) res=cin.nextInt();
40 else if((r==1 && c>1) || (r>1 && c==1))
41 {
42 int tmp = (r>c?r:c);
43 for(int i=0; i<tmp; ++i)
44 a[0]+=cin.nextInt();
45 res=a[0];
46 }
47 else//r,c>=2
48 {
49 for(int i=0; i<c; ++i)
50 a[i]=cin.nextInt();
51 for(int i=0; i<c; ++i)
52 b[i]=cin.nextInt();
53
54 for(int i=1; i<c; ++i)
55 a[i] += a[i-1];
56 b[0]+=a[0];
57
58 for(int i=1; i<c; ++i)
59 b[i] += max3(b[i-1], a[i-1], a[i]);
60
61 r -= 2;
62 while(r-- > 0)
63 {
64 for(int i=0; i<c; ++i)
65 a[i]=cin.nextInt();
66
67 a[0]+=b[0];
68 for(int i=1; i<c; ++i)
69 a[i] += max3(a[i-1], b[i-1], b[i]);
70 for(int i=1; i<c; ++i)
71 b[i]=a[i];
72 }
73 res=b[c-1];
74 }
75 System.out.println("Case #"+cas+": " + res);
76 }
77 }
78 }
貌似也没有哪里有内存泄漏啊……
添加回答
举报
0/150
提交
取消