| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
一直RuntimeError,请大牛门帮忙看下测试以及可以找到的测试数据都测试过,结果是没问题的
但是不明白为什么一直出错,不会是内存溢出吧?
import java.util.Arrays;
import java.util.Scanner;
/**
* 一直runtime error 思路用lastLine保存上一行的数据,用curLine保存正在处理的当前行的数据,
* 并把上一行的中间结果保存在result[] Info保存当前状态信息 lastLineCount保存上一行读入的数据量,以处理只有一行的情况.
* curLineCount保存这一行读入的数据量,以处理最后一行没有填满的情况 当读完一行时,计算出上一行的数据
* flag标示出以下有三行或以上的重复数据,那么从第当行+2行的时候,往下共n行数据的n-1行直接算出结果(绝对值肯定为0)
*
* @author troy
*
*/
public class Main {
private static int[] lastLine;
private static int[] curLine;
private static int[] result;
public static void main(String[] args) {
compute();
}
public static void compute() {
Scanner scan = new Scanner(System.in);
int width = scan.nextInt();
while (width != 0) {
System.out.println(width);
int pixel = scan.nextInt();
int num = scan.nextInt();
lastLine = new int[width];
curLine = new int[width];
result = new int[width];
int curLineCount = 0;
int lastLineCount = 0;
Info info = new Info();
Arrays.fill(result, 0);
// read firstLine
int i = 0;
while (num-- > 0) {
lastLine[i++] = pixel;
lastLineCount++;
if (num == 0) {
pixel = scan.nextInt();
num = scan.nextInt();
}
if (i == width)
break;
}
i = 0;
int flag = 0;
while (num-- > 0) {
curLine[i++] = pixel;
curLineCount++;
if (i == width) {
result = compare(lastLine, lastLineCount, curLine,
curLineCount, result, info);
int[] t = lastLine;
lastLine = curLine;
curLine = t;
lastLineCount = curLineCount;
curLineCount = 0;
i = 0;
if (flag == 1) {
flag++;
} else if (flag == 2) {
if (info.pre != 0 && info.count > 0) {
System.out.println(info.pre + " " + info.count);
info.count = 0;
info.pre = 0;
}
int c = num / width;
num -= (c - 1) * width;
info.count += (c - 1) * width;
flag = 0;
}
if (flag == 0 && num >= width * 3) {
flag = 1;
}
}
if (num == 0) {
pixel = scan.nextInt();
num = scan.nextInt();
if (pixel == 0 && num == 0)
break;
}
}
result = compare(lastLine, lastLineCount, curLine, curLineCount,
result, info);
if (curLineCount > 0) {
compare(curLine, curLineCount, lastLine, 0, result, info);
}
if (info.count > 0) {
System.out.println(info.pre + " " + info.count);
}
System.out.println("0 0");
width = scan.nextInt();
}
System.out.println("0");
}
public static int[] compare(int[] lastLine, int lastLineCount,
int[] curLine, int curLineCount, int result[], Info info) {
int[] nextResult = new int[curLineCount];
Arrays.fill(nextResult, 0);
for (int i = 0; i < lastLineCount; i++) {
int max = result[i];
if (i < lastLineCount - 1) {
int left = Math.abs(lastLine[i + 1] - lastLine[i]);
result[i + 1] = Math.max(result[i + 1], left);
max = Math.max(max, left);
}
if (i > 0 && i <= curLineCount) {
int leftLow = Math.abs(lastLine[i] - curLine[i - 1]);
nextResult[i - 1] = Math.max(nextResult[i - 1], leftLow);
max = Math.max(max, leftLow);
}
if (i <= curLineCount - 1) {
int low = Math.abs(lastLine[i] - curLine[i]);
nextResult[i] = Math.max(nextResult[i], low);
max = Math.max(max, low);
}
if (i < lastLineCount - 1 && i + 1 <= curLineCount - 1) {
int rightlow = Math.abs(lastLine[i] - curLine[i + 1]);
nextResult[i + 1] = Math.max(nextResult[i + 1], rightlow);
max = Math.max(max, rightlow);
}
stat(info, max);
}
return nextResult;
}
private static void stat(Info info, int value) {
if (info.pre != -1 && value != info.pre) {
System.out.println(info.pre + " " + info.count);
info.count = 1;
info.pre = value;
} else {
info.pre = value;
info.count++;
}
}
static class Info {
private int pre = -1;
private int count = 0;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator