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;
}
yes! It would be wrong to print only box[i] without adding any space between them!
my code is giving wrong answer for 15 cases out of 18 cases.
what is wrong in the code?
please help.
might help if u tell your approach/algo. I am unable to understand the algo you used.
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);
}
}