ZCO14001 - Editorial

PROBLEM SETTER -
ZCO 2014

DIFFICULTY -
Beginner

PREREQUISITES -
Implementation

SOLUTION IN SHORT -
There are no techniques or optimizations. Do as directed!!!

EXPLANATION -
There is a while loop which has a condition of breaking to be when input numer is 0. You can maintain a variable i denoting the current position of crane. You also have stored the stack sizes in an array a. For drop command, check if a[i]+1 is greater than h, if not, increment a[i]. For pick command, check if a[i] is positive, if yes, decrement a[i]. For moving left, do i-- and for moving right, do i++ and accordingly check if i > 1 (for moving left) and i < N (for moving right). Outside the while loop, after it has ended, output the array a.

TIME COMPLEXITY -
O(number of queries/operations)

#include
using namespace std;
int main()
{
int n=0,i=0,h=0,a[100000],ch,j=0;
cin>>n;
cin>>h;
for(i=0;i<n;i++)
cin>>a[i];
i=0;
while(ch !=0)
{
cin>>ch;
switch(ch)
{
case 1:
{
if(i>0 && i<=n )
{
i–;ch=-1;
}
}
break;
case 2:
{
if(i>=0&&i<n)
{
i++;ch=-1;
}
}
break;
case 3:
{
if(a[i]>0)
{
a[i]=a[i]-1;ch=-1;
}
}
break;
case 4:
{
if(a[i]<h)
{
a[i]=a[i]+1;ch=-1;
}
}
break;
case 0:
break;
default:
{
ch=-1;
}
break;
}
}
for(i=0;i<n;i++)
{
cout<<a[i];
}
return 0;
}

In Python 3.6:

l, lim = [int(i) for i in input().split()]
a = [int(i) for i in  input().split()]
commands = list(input())
s, crane = 1, False
for c in commands:
    if c == "1" and s != 1:
        s -= 1
    elif c == "2" and s != l:
        s += 1
    elif c == "3" and not crane and a[s-1] != 0:
        a[s-1] -= 1
        crane = True
    elif c == "4" and a[s-1] < lim and crane:
        a[s-1] += 1
        crane = False
    elif c == "0":
        break
o = ""
for i in a:
    o += " " + str(i)
print(o[1:])