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

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

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

@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] << " ";
}

My code for it
Hopefully short & sweet

Does it matter if I give space between output elements. The codechef judge seems to tell my answer wrong if I just write- cout << box[i];

instead of- cout << box[i] << ’ ';

I would very much appreciate if you could upvote my answer as I wished to ask a question related to the olympiad too.

Here’s my code that passes all the test cases.

http://pastebin.com/TAzsvK1y

Please help me to find out whats wromg in my code…Getting CA for three tests but WA for rest…Cant find whats wrong in it
#include
#include
using namespace std;
int main()
{
long long N;
long long H;
cin>>N;
cin>>H;
long long n=1;
long long b=0;
long long a[N];
for(long long i=1;i<=N;i++)
{
cin>>a[i];
}
long long com[100000];
long long m=0;
while(1) {
cin>>com[m];
if(com[m]!=0)
{

m++;
}
else if(com[m]==0)
break;

}

 for(long long t=0;t<m;t++)
 {
 switch(com[t])
 {
 case 1:
 {
    
   if(n!= 1)
   n--;
  
     else
     {}
     
      break;
 }
 
 case 2:
 { 
   if(n!= N)
   n++;
   
     else{}
      break;
 }
 
 case 3:
 {
     if(b==1)
     {
     }
     else if(b==0)
     {
         if(a[n]!=0)
         {
         a[n]=a[n]-1;
         b=1;
         }
         else{}
     }
        
     
      break; 
 }   
         
         
         
    
     
    
 case 4:
 { 
     if(b==0)
     {
     }
     else if(b==1)
     {
         if(a[n]!=H)
        { a[n]=a[n]+1;
         b=0;}
     
         else
    
    {
    }
         
     }    
         
      break;   
         
     }

}

 }

for(long long i=1;i<=N;i++)
{
cout<<a[i];

}

}

can anyone tell me what is wrong in my code…please help

include < stdio.h >

include< string.h >

char * buff;

int main(){

int n, h;

scanf("%d %d", &n, &h);

    int flag = 0;

char comm[100000];

long long int i = 0, j = 0, k = 0, s = 0;

long long int *a = (long long int *)malloc(sizeof(long long int)*n);

for(j = 0; j < n; j++)
scanf("%lld", &a[j]);

k = 0;
scanf("%s", buff);
for(j = 0; buff[j] != '\0'; j++)
{
    if(buff[j] != ' ')
    {
        comm[k] = buff[j];
        k++;
    }
}
i = k;
k = 0;
flag  = 0;
int crane = 0;
for(j = 0; j < i; j++){
    switch(comm[j]){
        case '1'://move left
                    if(k != 0)
                    k--;
                    break;

        case '2'://move right
                    if(k != i-1)
                    k++;
                    break;

        case '3'://pick box
                    if(crane == 0 && a[k] > 0)
                    {   
                        crane++;
                        a[k]--;
                    }
                    break;

        case '4'://drop box
                    if(crane == 1 && a[k] < h)
                    {
                        crane--;
                        a[k]++;
                    }
                    break;

        case '0':   flag = 1;
                    break;
    }
    if(flag == 1)
    break;
}

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

return 0;

}

my code is giving wrong answer for 15 cases out of 18 cases.
what is wrong in the code?
please help.

import java.util.*;

class PP2 {

public static boolean b = false;
public static int c = 0;

public static int[] finale(int type, int h, int r[], int w) {
	if (type == 1 && c != 0) {
		c--;
	} else if (type == 2 && c != w - 1) {
		c++;
	} else if (type == 3 && r[c] != 0 && b == false) {
		r[c]--;
		b = true;
	} else if (type == 4 && r[c] != h && b == true) {
		r[c]++;
		b = false;
	} else if (type == 0) {
		return r;
	}
	return r;
}

public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	String str = sc.nextLine();
	String x[] =str.split(" ");
	int w = Integer.parseInt(x[0]);
	int h = Integer.parseInt(x[1]);
	str = " ";
	str = sc.nextLine();
	int r[] = new int[w];
	String y[] = str.split(" ");
	for (int i = 0; i < w; i++) {
		r[i] = Integer.parseInt(y[i]);
	}
	str = " ";
	str = sc.nextLine();
	String z[] = str.split(" ");
	int l = z.length;
	int m[] = new int[l];
	for (int i = 0; i < l; i++) {
		m[i] = Integer.parseInt(z[i]);
	}
	long start = System.currentTimeMillis();
	int ans[] = new int[w]; 
	for(int i = 0 ; i < l ;i++){
		ans= finale(m[i] , h , r , w);
		
	}
	for(int j = 0 ; j < w ; j ++){
		System.out.print(ans[j] + " ");
	}
	System.out.println(" ");
	long end = System.currentTimeMillis();
	long diff = end - start;
	System.out.println(diff);
	

}

}

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.

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.