![:(](./images/smilies/icon_frown.gif)
![:(](./images/smilies/icon_frown.gif)
Moderator: Board moderators
All cases I've tried your code on were solved correctly. Possibly the problem might be in the really big arrayjoemmanuel wrote:Well, i've thought about that and modified, including the range [-3000,3000] on my bool array, but still WA
I don't know what could it be... may be a tricky case that im not testing...
bool usd[70][70][6000];
Trying to debug my program, I encountered a case where your program gives a faulty output:joemmanuel wrote:Hi.
I've tried to use some kind of memoizaton, using a bool array for each square in the board ( usd ) to know all sums that can be reached to that square.
I've tried all test cases but still WA... can u help me?
.......
Code: Select all
...........
Try this:Igloo wrote:My program still does not work correctly, keep getting WA
I am aware that this piece of code is very large, but it is my first part of code that I've written for here.
Code: Select all
27
49
49 49
49 50 50
50 49 49 49
49 49 49 50 49
50 49 49 49 49 49
49 50 50 49 49 50 49
49 49 49 50 50 49 50 49
50 49 49 49 50 49 49 50 49
49 49 49 50 49 49 50 50 50 49
50 50 50 50 49 49 50 50 50 49 49
50 50 50 49 50 49 49 49 50 49 49 50
49 50 50 49 49 49 49 49 49 50 49 50 50
49 50 49 50 50 50 49 49 49 49 49 49 50 49
50 50 49 49 49 49 50 49 49 49 49 50 49 49 50
50 50 50 49 49 49 50 50 49 49 50 50 49 50 49 49
49 50 49 49 50 49 50 49 50 50 49 49 50 49 50 50 49
49 50 49 49 50 50 49 50 49 50 50 50 50 50 50 50 50 50
49 49 49 49 50 49 50 50 50 50 49 49 50 49 49 50 49 50 49
49 49 49 49 50 50 50 49 49 49 50 50 50 50 49 50 49 49 49 50
50 49 50 49 50 50 49 50 50 50 50 49 50 49 49 49 50 50 49 49 49
50 50 50 50 50 49 50 50 50 50 50 50 49 50 49 49 50 50 50 49 50 50
50 50 50 50 49 50 50 49 50 49 49 49 50 50 49 50 50 50 49 49 49 49 50
50 49 49 49 49 49 50 50 50 49 50 49 50 49 50 50 50 50 50 50 50 50 50 49
49 50 49 49 50 49 50 49 50 50 50 50 50 49 49 49 50 50 49 49 50 50 50 49 49
50 50 50 49 50 50 49 49 50 49 50 49 50 49 50 49 50 49 50 50 49 50 49 49 50 49
50 49 49 49 49 50 50 49 50 49 50 50 49 50 50 49 50 49 49 49 49 50 49 50 49 49 49
50 49 50 50 49 50 50 49 50 49 50 50 50 49 50 49 49 49 50 49 50 50 49 50 50 50
50 49 49 50 49 50 49 50 49 49 49 49 49 49 49 50 50 49 50 49 49 49 49 49 49
50 50 50 49 50 49 50 50 49 50 49 50 50 50 49 50 49 49 50 49 49 49 50 49
49 49 50 49 49 50 49 49 49 50 49 50 49 49 50 49 50 50 49 49 49 49 50
49 49 49 49 49 49 49 50 49 49 49 49 49 50 50 49 50 49 50 50 49 50
49 50 49 50 50 49 50 50 50 49 50 50 49 50 50 49 49 49 49 49 49
50 50 50 50 50 49 49 49 49 50 49 50 50 50 49 50 49 50 49 49
49 50 50 49 50 50 49 50 49 50 50 50 49 50 49 50 50 50 50
50 49 50 50 49 49 50 50 50 50 49 50 50 50 49 50 49 49
49 50 49 50 50 50 50 49 50 50 50 49 49 49 50 49 50
50 49 50 49 50 49 49 50 50 49 50 50 49 50 50 49
50 49 50 49 50 50 49 49 49 49 50 49 50 50 49
49 50 50 50 50 50 50 49 49 50 50 50 49 50
49 49 49 49 50 50 49 49 50 49 49 50 50
49 50 49 49 50 49 50 49 50 49 49 50
50 50 49 49 50 50 50 50 49 50 49
50 50 49 49 49 49 50 50 50 49
49 50 49 49 50 49 50 50 49
49 49 49 49 50 50 49 49
50 49 50 50 50 50 49
50 50 49 49 49 50
50 49 50 50 49
49 50 50 50
49 50 49
49 49
50
22
49
50 49
49 49 49
50 50 49 49
49 49 49 50 49
50 50 50 49 49 50
49 50 49 49 50 49 49
50 50 50 50 49 49 50 50
49 49 49 49 49 50 49 49 49
49 49 50 50 49 50 49 49 50 50
49 49 50 49 50 50 49 49 50 49 50
49 49 50 50 49 50 49 49 50 49 50 50
50 49 49 50 50 49 49 49 50 49 50 50 50
49 50 50 49 50 49 49 50 50 50 50 49 50 49
50 50 50 50 50 50 50 49 49 50 49 49 49 49 49
49 50 49 50 49 49 50 49 50 49 50 49 49 50 49 49
49 50 50 50 49 49 49 49 50 49 49 50 49 49 50 49 50
49 49 50 49 50 50 50 50 49 49 50 50 49 50 49 50 49 50
49 50 49 49 49 49 50 50 49 50 50 50 50 50 50 49 50 49 49
50 50 49 50 50 49 50 49 49 49 50 50 49 49 50 50 49 50 49 50
49 50 49 50 49 50 49 50 50 49 50 49 50 50 50 49 50 49 50 50 49
49 50 50 49 49 49 49 49 49 50 49 50 49 50 49 50 50 50 49 50 49 49
49 50 50 50 50 50 49 49 49 49 50 50 49 49 50 49 49 50 50 49 49
50 50 49 50 49 50 50 50 50 49 50 50 50 49 49 50 49 49 50 49
49 49 49 49 50 49 49 49 49 49 49 50 50 50 49 50 49 49 49
49 49 50 50 50 49 50 49 49 50 50 50 50 50 50 50 49 49
50 49 49 50 50 50 49 49 49 50 49 49 50 49 49 50 50
50 50 49 49 50 49 50 49 50 50 49 50 50 49 49 49
49 49 50 50 49 50 50 49 50 50 50 49 50 49 50
50 50 49 50 50 49 49 50 50 50 50 49 50 50
50 50 50 50 49 50 50 50 49 50 49 49 50
49 50 50 49 49 50 49 50 49 49 49 50
50 50 50 49 49 49 50 50 49 49 50
50 50 49 50 50 50 50 49 50 50
50 50 50 49 50 50 49 50 50
49 50 49 50 50 50 50 49
49 50 49 49 49 50 49
49 49 50 50 49 50
49 49 49 49 49
49 50 50 50
49 50 49
49 49
50
0
Code: Select all
23
28
Code: Select all
23
0
Joemmanuel, your code isn't working on the input I've just found for Igloo, too.Martin Macko wrote:All cases I've tried your code on were solved correctly. Possibly the problem might be in the really big arrayjoemmanuel wrote:Well, i've thought about that and modified, including the range [-3000,3000] on my bool array, but still WA
I don't know what could it be... may be a tricky case that im not testing...
Thanks a bunch, it seems that I did not set the complete array to zero..Martin Macko wrote: Try this:My AC's output:Code: Select all
.........................
Your output:Code: Select all
23 28
Code: Select all
23 0
Code: Select all
r[(2*n)-2][0]=a[(2*n)-2][0];
for(int i=(2*n)-3,k=1;i>=0;i--){
if(i>=n-1)k++;
else k--;
for(int j=0;j<k;j++){
if(j==0)r[i][j]=closest(r[i+1][j]+a[i][j],r[i+1][j]-a[i][j]);
else if(j==k-1)r[i][j]=closest(r[i+1][j-1]+a[i][j],r[i+1][j-1]-a[i][j]);
else r[i][j]=closest(closest(r[i+1][j]+a[i][j],r[i+1][j-1]+a[i][j]),closest(r[i+1][j]-a[i][j],r[i+1][j-1]-a[i][j]))-a[i][j];
}
}
Code: Select all
inline int closest(int a,int b){
if(abs(a) < abs(b))return a;
else return b;
}
Code: Select all
30
50
50 50
50 50 50
50 50 50 50
50 50 50 50 50
50 50 50 50 50 50
50 50 50 50 50 50 50
50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49 49
49 49 49 49 49 49 49 49
49 49 49 49 49 49 49
49 49 49 49 49 49
49 49 49 49 49
49 49 49 49
49 49 49
49 49
29
0
Code: Select all
0
Code: Select all
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<26
#define UNVISITED -1
int offset;
int input[59][30];
int max_val;
int arr [59][30][3001];
// bool visited[59][30][3001];
int N;
int f(int i, int j, int sum){
// printf("%d %d %d %d\n", i,j,sum,input[i][j]);
if(i == 2*N-1-1){
return abs(sum);
}
// if(visited[i][j][sum+offset]){
// return arr[i][j][sum+offset];
// }
if(arr[i][j][sum+offset] != UNVISITED){
return arr[i][j][sum+offset];
}
int ret = INF;
if(i<N-1){
// lower pyramid, go up
for(int j = 0; j<i+1; j++){
// left
int lu_plus = f(i+1, j, sum+input[i+1][j]);
ret = min(ret, lu_plus);
int lu_minus = f(i+1, j, sum-input[i+1][j]);
ret = min(ret, lu_minus);
// right
int ru_plus = f(i+1, j+1, sum+input[i+1][j+1]);
ret = min(ret, ru_plus);
int ru_minus = f(i+1, j+1, sum-input[i+1][j+1]);
ret = min(ret, ru_minus);
}
} else {
int numCols = N - (i-(N-1));
// printf("I is %d, numCols is %d\n", i, numCols);
for(int j = 0; j<numCols; j++){
// left
if(j){
int lu_plus = f(i+1, j-1, sum+input[i+1][j-1]);
ret = min(ret, lu_plus);
int lu_minus = f(i+1, j-1, sum-input[i+1][j-1]);
ret = min(ret, lu_minus);
}
// right
if(j < numCols-1){
int ru_plus = f(i+1, j, sum+input[i+1][j]);
ret = min(ret, ru_plus);
int ru_minus = f(i+1, j, sum-input[i+1][j]);
ret = min(ret, ru_minus);
}
}
}
// visited[i][j][sum+offset] = true;
return arr[i][j][sum+offset] = ret;
}
int main(){
while(scanf("%d", &N)!=EOF){
if(!N){
return 0;
}
max_val = (2*N-1)*50;
offset = max_val;
for(int i = 1; i<N; i++){
for(int j=0; j<i; j++){
scanf("%d", &input[2*N-1-i][j]);
for(int val = -max_val; val<=max_val; val++){
// arr[i][j][val+offset]'
// visited[2*N-1-i][j][val+offset] = false;
arr[2*N-1-i][j][val+offset] = UNVISITED;
}
}
}
for(int j = 0; j<N; j++){
scanf("%d", &input[N-1][j]);
for(int val = -max_val; val<=max_val; val++){
// visited[N-1][j][val+offset] = false;
arr[N-1][j][val+offset] = UNVISITED;
}
}
for(int i = N-1; i>=1; i--){
for(int j = 0; j<i; j++){
scanf("%d", &input[i-1][j]);
for(int val = -max_val; val<=max_val; val++){
// visited[i-1][j][val+offset] = false;
arr[i-1][j][val+offset] = UNVISITED;
}
}
}
int ret = f(0,0, input[0][0]);
// printf("%d %d %d %d\n", input[0][0], input[1][0], input[1][1], input[2][0]);
printf("%d\n", ret);
}
return 0;
}