You are not logged in. Please login at www.codechef.com to post your questions!

×

ZCO12004 - Round Table

My code fails some test cases. Could someone please point out the flaw or provide test cases which fail...

My approach uses memorization..


int minCost(int cost[] , int N , int i , bool first , int memo[][2])

{

bool prev = first;

if(i == N-1)
{
    if(prev == false)
        return cost[i];

    if(first == true)
        return 0;

    if(first == false)
        return cost[i];
}

if(prev)
{
    if(memo[i][0])
        return memo[i][0];

    else
        memo[i][0] = min ( cost[i] + minCost(cost,N,i+1,prev,memo) ,
                           minCost(cost,N,i+1,!prev,memo));

    return memo[i][0];
}

else
{
    if(memo[i][1])
        return memo[i][1];

    else
        memo[i][1] = cost[i] + minCost(cost,N,i+1,!prev,memo);

    return memo[i][1];
}

}

int main()

{

int N;
cin>>N;

int cost[N];

for(int i=0 ; i<N ; i++)
    cin>>cost[i];

int memo[N][2];

for(int i=0 ; i<N ; i++)
    memo[i][0] = memo[i][1] = 0;

cout<<min( minCost(cost,N,0,false,memo) ,
           minCost(cost,N,0,true,memo));

}

asked 27 Nov '18, 21:42

priyanshul's gravatar image

3★priyanshul
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,093
×1,901
×423
×311
×74
×1

question asked: 27 Nov '18, 21:42

question was seen: 68 times

last updated: 27 Nov '18, 21:42