## 10003 - Cutting Sticks

Moderator: Board moderators

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
you used O(L^2*n), L can be 1000 at most, and n only 50
L*L*n = 1000*1000*50 = 50000000 and
n*n*n = 50 * 50 * 50 = 125000
the original matrix chain multiplication algorithm takes only O(n^3) time, that should be used for this problem
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

shihabrc
New poster
Posts: 49
Joined: Sun Sep 18, 2005 10:20 am
Location: CSE,BUET
Contact:
Thanx a lt. I took too much redaundant states. Now, i've changed my soln. and got AC. Thanx again.

-Shihab
Shihab
CSE,BUET

MAK
New poster
Posts: 28
Joined: Sun Oct 23, 2005 3:44 pm

### 10003 Cutting Sticks, why TLE?

I am using a straightforward recursion+memoization based attack at this problem. Worked for all DP problems I've tried before, but I'm getting TLE on this one. My recurrrence relation is

Code: Select all

``cost(start, end)=min(cost(start,k)+cost(k,end)+(end-start))``
where k is a cut. Is there a better/faster way?

debajyoti
New poster
Posts: 1
Joined: Sat Dec 02, 2006 9:20 am

### hopeless

i was looking for a solution.
amazingl thing is that i have tried as the same method like you and found wrong answer.
if you get any help would u please forward me?

debajyoti
debajyoti_mondal_cse@yahoo.com

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am
I use DP method to solve the problem,but still got WA.
Who can give me test data to test my program?

Code: Select all

``````#include <stdio.h>
#include <limits.h>

int table={0};
int profit={0};
int data;
int n;
int len;

void process(){
int i,j,k,min=INT_MAX;
for(i=n-1;i>=1;i--)
for(j=i+1;j<=n;j++){
min=INT_MAX;
for(k=1;k<=j-i;k++){
if( table[i][j-k]+table[j+1-k][j]<min )
min=table[i][j-k]+table[j+1-k][j];
}
table[i][j]=min;
}

}

void profit_process(){
int i,j,k,min=INT_MAX;
for(i=n-1;i>=1;i--)
for(j=i+1;j<=n;j++){
min=INT_MAX;
for(k=1;k<=j-i;k++){
if( i==j-k && j+1-k==j ){
if( table[i][j-k]+table[j+1-k][j]<min )
min=table[i][j-k]+table[j+1-k][j];
}else{
if( i==j-k ){
if( profit[j+1-k][j]+table[i][j]<min )
min=profit[j+1-k][j]+table[i][j];

}else if( j+1-k==j ){
if( profit[i][j-k]+table[i][j]<min )
min=profit[i][j-k]+table[i][j];
}else{
if( profit[i][j-k]+profit[j+1-k][j]+table[i][j]<min )
min=profit[i][j-k]+profit[j+1-k][j]+table[i][j];
}
}
}
profit[i][j]=min;
}
printf("The minimum cutting is %d.\n",profit[n]);
}

int main(){

int i,pre,temp;
while( scanf("%d",&len)==1 && len ){
scanf("%d",&n);
n=n+1;
pre=0;
for(i=1;i<n;i++){
scanf("%d",&temp);
profit[i][i]=table[i][i]=temp-pre;
pre=temp;
}
profit[i][i]=table[i][i]=len-pre;
process();
profit_process();
}
return 0;
}``````

sumantbhardvaj
New poster
Posts: 19
Joined: Sun Jun 18, 2006 4:07 pm
Contact:

### 10003 TLE

This problem is based upon Matrix Multiplication .
I wrote a top down DP solution to it .which is being timed out. Can't we have top down solution to this problem get accepted ??

Or if there is any mistake in optimization in solution then plz point it out. My code goes as follows.

Code: Select all

``````#include<string>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int a;
int data;

class sumant
{
public:
int count;
int input()
{
int l,x,n;
cin>>l;
while(l!=0)
{
count=0;
for(int i=0;i<=l;i++)
for(int j=0;j<=l;j++)
data[i][j]=-1;

cin>>n;
for(int i=0;i<=l;i++)
a[i]=0;

for(int i=0;i<n;i++)
{
cin>>x;
a[x]=1;
}
int z=memo(0,l);
cout<<"The minimum cutting is "<<z<<".\n";
//cout<<count<<"\n";
cin>>l;
}
}
int memo(int p,int q)
{
if(data[p][q]==-1)
{
int z=doit(p,q);
data[p][q]=z;
return z;
}
else
{
return data[p][q];
}
}

int doit(int p,int q)
{
int min=100000000,m;
bool flag=false;

for(int i=p+1;i<q;i++)
{
if(a[i]!=0)
{
flag=true;break;
}
}
if(!flag)
return 0;

for(int i=p+1;i<q;i++)
{
if(a[i]==1)
{
m=q-p+memo(p,i)+memo(i,q);
if(min>m)
min=m;
flag=true;
}
}

return min;
}
};
int main()
{
sumant s;
s.input();
return 0;
}
``````

rio
A great helper
Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan
Top - down DP is enough to solve this problem.

Code: Select all

``````int a;
int data;
``````
Find a way with

Code: Select all

``````int a;
int data;
``````

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia
Hello all,

I'm a dynamic programming newbie.

I'm trying to solve this problem with a recursive function with memoization. However, I'm getting Time limit exceeded. I calculate the cost of cutting stick which starts at position i and ends and position j by finding the minimum cost of summing (j-i) plus the cost of cutting the stick from i to P[k] and from P[k] to j, where P[k] is a given partition point.

I read there's a faster algorithm, but I can't understand it.

Thanks for helping a newbie!

If you are interested, here's my (slow) code:

Code: Select all

``````I got accepted thanks to the help of emotional blind.
``````
Thanks again!
Last edited by andmej on Wed Mar 12, 2008 4:32 pm, edited 1 time in total.
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Contact:

Code: Select all

``memset(cache, -1, sizeof(cache));``
This part is costly. Try using O(n^2) memory. Here n<50, which is much smaller.

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Contact:
Another approach:

remove memset part
and
insert

Code: Select all

``````	for(int i=0;i<=n+1;++i){
for(int j=i;j<=n+1;++j){
cache[p[i]][p[j]]=-1;
}
}``````
after this block

Code: Select all

``````for (int i=1; i<=n; ++i){
cin >> p[i];
} ``````

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia
Thanks a lot, emotional blind. That made the trick and got my program accepted.

I thought memset was the fastest way to initialize a block of memory. However, you made my notice that I'm wasting a lot of space when using a matrix of 1001 * 1001 integers. I think it's possible to solve it using a 52 * 52 integers matrix, I will try to do it.

Thanks again, emotional blind!
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

BUET
New poster
Posts: 22
Joined: Sun Jun 13, 2010 8:38 am

### 10003 - Cutting Sticks

why Runtime error?

Code: Select all

``````#include<iostream>

using namespace std;

#define INF 4294967295
int num,l;
int length;
bool cut;

unsigned long s = {0};

// will print the ordering of cutting
//this part is optional to UVA 10003 problem

void print(int i,int j)
{
if( s[i][j] != 0)
printf("%d ",s[i][j]);
else return;
print(i,s[i][j]);
print(s[i][j],j);

}

void matrix_chain()
{
unsigned long m = {0};
//int **m;
unsigned int q;

int l,j,i,k;
int check = 0;

memset(s,0,sizeof(s));
//memset(m,0,sizeof(m));

for( l = 2; l <= length; l++)
{
for( i = 0; i <= length-l; i++)
{
j = i+l;
m[i][j] = INF;
for( k = i + 1; k < j;k++)
{
if( cut[k] == true )
{
check = 1;
q = m[i][k] + m[k][j] + (j - i);

if( q < m[i][j])
{
m[i][j] = q;
s[i][j] = k;

}
}

}

if( check == 0)
{
m[i][j] = 0;
}
check = 0;
}
}

cout << m[length] << ".\n" ;

print(0,length);

cout << endl;

}

int main(void)
{

int size,i,k,m,n,c = 1;
int p = {0};

while(1)
{
cin >> l;
length = l;
if(!l) break;
cin >> n;

memset(cut,false,sizeof(cut));

for(i = 1; i <= n; i++)
{
cin >> m;
cut[m] = true;

}

cout << "The minimum cutting is ";
matrix_chain();

}

return 0;
}
``````

sir. manuel
New poster
Posts: 18
Joined: Sat Nov 20, 2010 7:44 pm

### Re: 10003 - Cutting Sticks

libreiries...you need "string.h" to memset,,,too "stdio.h"

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

### Re: 10003 - Cutting Sticks

Try to find a O(n*n) algorithm...O(n*n*l) fails.
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

bazocc4
New poster
Posts: 4
Joined: Fri May 31, 2013 5:57 am

### Re: 10003 - Cutting Sticks

HELPPP... i really don't get to understand the meaning of this problem +_+
Thanks in advande 