Video Game ZCO('14) (Its URGENT)

Can anyone help me with this problem. Its urgent. Got the olympiad tomorrow.

http://www.iarcs.org.in/inoi/2014/zco2014/zco2014-1a.php

My code runs fine for the test cases.But I’m exceeding time limit when the online judge tests it.!

Please post your code.

Superty , i have not submitted any questions , what is the compilation commands written there , i could not understand it . Have just adapted to codechef . Please help

i used the simple conditions that are imposed. I think my approach is quite inefficient!
so here’s my code that submitted!

#include<iostream>

#include
#include
using namespace std;

int main()
{
long long int n, h,i;
scanf("%lld %lld",&n, &h);
vector h_l(n);
for(i=0; i<n; i++)
scanf("%lld",&h_l[i]);

vector c;
int tst, p,d;
scanf("%d",&tst);

while(tst !=0)
{c.push_back(tst);
scanf("%d",&tst);
}
c.push_back(0);

int pos=0;
p=0; d=1;
for(i=0; c[i] !=0; i++)
{
if(c[i]==1)
{ if(pos !=0)
pos–;
}

if(c[i]==2)
{ if(pos !=(n-1) )
  pos++;
}

if (c[i]==3)
{ if((p==1) && (d==0) )
    p=1;

   else if(d!=0)
   { if(h_l[pos] !=0)
     {h_l[pos]= (h_l[pos] - 1 );
     p=1;
      }
      else
      p=0;
   }

else
    p=0;
}


if( c[i]==4)
{    if(p!=0)
     {   if(h_l[pos]!=h)
         {h_l[pos]= (h_l[pos] + 1 );
          d=1;
         }
         else
            d=0;
     }

     else
      d=0;

}

}

for(i=0; i<n; i++)
printf("%lld ",h_l[i]);

int z=0;

return 0;}
enter code here

Kshitij whats with the compilation command . whats use of it ?

You need not bother with those commands. Just submit as you usually would.

What is vector

That didn’t even compile for me. I changed it to vector and submitted. I got wrong answer, not time limit exceeded.

wait wait…
i post the code in plain text… sorry for that!

enter code here


 #include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

int main()
{
long long int n, h,i;
scanf("%lld %lld",&n, &h);
vector<long long int> h_l(n);
for(i=0; i<n; i++)
scanf("%lld",&h_l[i]);


vector<int> c;
int tst, p,d;
scanf("%d",&tst);

while(tst !=0)
{c.push_back(tst);
scanf("%d",&tst);
}
c.push_back(0);

int pos=0;
    p=0; d=1;
for(i=0; c[i] !=0; i++)
{
    if(c[i]==1)
    { if(pos !=0)
      pos--;
    }

    if(c[i]==2)
    { if(pos !=(n-1) )
      pos++;
    }

    if (c[i]==3)
    { if((p==1) && (d==0) )
        p=1;

       else if(d!=0)
       { if(h_l[pos] !=0)
         {h_l[pos]= (h_l[pos] - 1 );
         p=1;
          }
          else
          p=0;
       }

    else
        p=0;
    }


    if( c[i]==4)
    {    if(p!=0)
         {   if(h_l[pos]!=h)
             {h_l[pos]= (h_l[pos] + 1 );
              d=1;
             }
             else
                d=0;
         }

         else
          d=0;

    }

}

for(i=0; i<n; i++)
printf("%lld ",h_l[i]);

int z=0;

return 0;}

See that it runs for the test cases given in the problem!
But fails when the online judge tests it

Yeah but it’s not time limit exceeded. It’s wrong answer.

In your program what are the variables p and d?

p stores that pick up command is executed or not while d stores that of drop command.
I am sure that the code works fine. I think that the judge is reporting 0 score because of time limit exceeding as there is only one sub task of 100 marks.

by the where did u run and test the code?

Why are you using long long ? All integers are within 32 bit int limits. Follow KISS. Keep it simple stupid. Why are you first reading commands storing it as a vector and then following them. Read it and follow it. Give you variables long names. Just long enough that you are never confused. I just did the problem minutes ago so here’s the strategy. You should use only 1 array to store value of stack height, 4 ints for crane position, max height, no of stacks of boxes & current command & a boolean for whether the crane holds the box. Try it again. Do you want the solution or help to solve?

It’s simple use an array(boxes). It must contain the height of the boxes.Here’s the Pseudo-code:

slider=0
picked=false
while(Not exit){
    Input command
    if command==exit break
    else if command==left AND slider>0 
           slider--
    else if command==right AND slider<N-1
           slider++
    else if command==drop AND picked AND boxes[slider]< H
           boxes[slider]++
           picked=false
    else if command==pick AND !picked boxes[slider]>0
           boxes[slider]--
           picked=true
}
Now print all the elements in the array.

Hope this helps.

Your logic seems to be incorrect. try to explain your logic and type your code with proper indentation. if possible try to give a url from ideone.com or code.hackerearth.com

swargatochatt thanks for this but can u please post the suitable code so that i could get it checked by the online judge… I think it’ll be much more helpful

@aaha97 whom are u referring to?

@kshitijkmr10 Someone has taken the time and effort to give you a pseudocode and you want the entire code just to get it accepted by the judge? :open_mouth: It’s less helpful if you want it that way.

@sandy999 is right, answers in themselves are meaningless its the journey from the question to the answer which holds value. Anyways this is the code. Hope this helps. I still suggest writing the code on your own. Work on the problem for another hour. You’ll get it.

#include <iostream>
#include <algorithm>
 
using namespace std;
 
int main()
{
 int N, H; //N = no of stacks, H = max height of stack;
 cin >> N >> H;
 
 int boxes[N];
 
 for(int i = 0; i < N; i++)
 cin >> boxes[i];
 
 int command;
 cin >> command;
 
 
 int cranepos = 0;
 bool cranebox = 0;
 while(command != 0)
 {
    if(command == 1 & cranepos != 0)
        cranepos--;
        else if(command == 2 & cranepos != N-1)
        cranepos++;
        else if(command == 3 & cranebox == 0 & boxes[cranepos] != 0){
        cranebox = 1;
        boxes[cranepos]--;
        }
        else if(command == 4 & cranebox == 1 & boxes[cranepos] < H){
        cranebox = 0;
        boxes[cranepos]++;
        }
        cin >> command;
 }
 for(int i = 0; i < N; i++)
 cout << boxes[i] << " ";
}

thanks a lot; i m still trying n i promise i’ll not see your code till the time i am unable to run my own code!. thanks a lot once again @organic shilling n i think i 've got to know the faults . M trying the way u told earlier.

n guess what finally my code ran successfully! 100/100 thanks to your advice :-D… @Organic-Shilling … By the way r u participating in zco?