Posted: Fri May 12, 2006 1:30 pm
Sorry i dont got ur +-15 & +-1 for values 8(eight)
what is this?? plz explain me
what is this?? plz explain me
My AC's output for your input istan_Yui wrote:I tried to solve with DP solution, but got WrongAnswer...
Here is my input/output. Is my output correct?
Output :Thank you.Sorry, I found my stupid mistake after this post ....
Code: Select all
0
4
0
5
12
50
Have you read all other threads on this problem? (see: http://online-judge.uva.es/board/viewtopic.php?t=10128 and http://online-judge.uva.es/board/viewtopic.php?t=10164)StatujaLeha wrote:I get TLE on this problem. I think it can be because I use for memoization std::map<>. Please tell about how do you do memoization for this problem?
Yes, of course. I have only understood that there are various kinds of memoization's implementation. I want to know if there is a faster implementation than with using std::map<> as cache.Have you read all other threads on this problem?
due to the problem constraints you can easily use boolean array , instead of set , and it works fasterDarko wrote:Well, why not ask the question in that thread then? This is how we end up with 10 threads on each problem.
In my Java solution I used a set (as suggested by misof in yet another thread on this problem) to determine different sums in each row, so I could build the table (which is not needed, but it was easier for me). I didn't use memoization (unless you count adding an element to a set that already contains it as memoization).
I think that STL set would do just fine (I wrote my own and it's not that good, but it worked).
Code: Select all
In my Java solution I used a set (as suggested by misof in yet another thread on this problem) to determine different sums in each row, so I could build the table (which is not needed, but it was easier for me). I didn't use memoization (unless you count adding an element to a set that already contains it as memoization).
I think that STL set would do just fine (I wrote my own and it's not that good, but it worked).
Code: Select all
#include <stdio.h>
#include <string.h>
int NO=-10000;
const int P=500;
const int N=1000;
bool usd[70][70][1000]={false};
int tab[70][70];
int main(){
int n;
while(1){
memset(usd,false,sizeof(usd));
scanf("%d", &n);
if(n==0)
return 0;
int i,j,k;
for(i=0;i<70;i++)
for(j=0;j<70;j++)
tab[i][j]=NO;
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
scanf("%d", &tab[i][j]);
for(i=n;i>0;i--)
for(j=0;j<i-1;j++)
scanf("%d", &tab[n+(n-i)][j]);
usd[0][0][tab[0][0]+P]=true;
for(i=0;i<n-1;i++)
for(j=0;j<=i;j++)
if(tab[i][j]!=NO)
for(k=0;k<N;k++)
if(usd[i][j][k]==true){
usd[i+1][j][k+tab[i+1][j]]=true;
usd[i+1][j][k-tab[i+1][j]]=true;
usd[i+1][j+1][k+tab[i+1][j+1]]=true;
usd[i+1][j+1][k-tab[i+1][j+1]]=true;
}
for(;i<(2*n)-2;i++)
for(j=0;j<=i;j++)
if(tab[i][j]!=NO)
for(k=0;k<N;k++)
if(usd[i][j][k]==true)
if(j==0){
usd[i+1][j][k+tab[i+1][j]]=true;
usd[i+1][j][k-tab[i+1][j]]=true;
}
else{
usd[i+1][j][k+tab[i+1][j]]=true;
usd[i+1][j][k-tab[i+1][j]]=true;
usd[i+1][j-1][k+tab[i+1][j-1]]=true;
usd[i+1][j-1][k-tab[i+1][j-1]]=true;
}
for(k=0;k<P;k++)
if(usd[i][0][P+k]==true || usd[i][0][P-k]==true){
printf("%d\n", k);
break;
}
}
return 0;
}