http://usaco.org/index.php?page=viewproblem2&cpid=1256
Hi! The idea for my solution is that to figure out the next number x in a range, you only need the previous two distinct sequenced numbers and their ranges with x’s index. It’s similar to Danny’s solution, but I was trying to implement the “find previous two distinct sequenced numbers” differently. However, I can’t get the last 6 test cases to pass, and I can’t figure out what’s wrong with my code. Could I please get some help?
import java.util.;
import java.io.;
public class rangerecon {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
long[][] steps = new long[N][N];
for(int i = 0; i < N-1; i++) {
long a = s.nextLong();
for(int k = 0; k < N-1-i; k++) {
steps[i][k] = s.nextLong();
}
}
long[] ans = new long[N];
ans[0] = 0;
for(int a = 0; a < N-1; a++) {
ans[a+1] = ans[a] + steps[a][0];
int row = a;
int col = 0;
while(row > 0) {
row--;
col++;
if(Math.max(steps[a][0], Math.abs(ans[a+1]-ans[row])) != steps[row][col]) {
ans[a+1] = ans[a]-steps[a][0];
break;
}
if(steps[row][col-1] != 0) break;
}
}
for(int i = 0; i< N-1; i++) {
System.out.print(ans[i] + " ");
}
System.out.print(ans[N-1]);
}
}