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.
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.
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?
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
@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?
It’s less helpful if you want it that way.
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?
yes! It would be wrong to print only box[i] without adding any space between them!
might help if u tell your approach/algo. I am unable to understand the algo you used.