LIFTME - Editorial

ain’t they both same for python 3?

using namespace std;
#define  ll long long int
int main(void) {
	 int T;
	scanf("%d",&T);
	while(T--)
	{
	    ll s=0,k=0,p=0,N,Q,i;
	    scanf("%lld%lld",&N,&Q);
	  long long int f[Q],d[Q];
	    for(i=0;i<Q;i++)
	    scanf("%lld%lld",&f[i],&d[i]);

	    for(i=0;i<Q-1;i++)
	    {
	        k+=abs((d[i]-f[i+1]));

	    }
	    for(i=0;i<Q;i++)
	    {
	      if(i==0)
	      s+=d[i];
	    else{
	    s+=abs((f[i]-d[i]));}
	    }
         p=s+k;
	    printf("%lld\n",p);
	}
	return 0;
} ```

what is wrong in this?

I new to codechef and programming for this problem i am trying the most basic approach yet i am getting wrong answer though previous sucessful submissions had this same solution

long long n,q, sum=0,i=0;
cin>>n>>q;
while(q–)
{
long long f,d;
cin>>f>>d;
sum+=abs(f-i)+abs(d-f);
i=d;
}
cout<<sum<<’\n’;

Yes, they are the same. There is no special ‘long’ type in Python 3.

Bro, in Python, this is a very short and crisp solution. Have a look at mine below and tell if you don’t understand it:

https://www.codechef.com/viewsolution/32036034

Thank u so much!

Why is this wrong?

#include
#include
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int p=1;p<=t;p++)
{
int n,q;
long long int sum=0;
scanf("%d %d",&n,&q);
int arr[q][2];
for(int i=0;i<q;i++)
{
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
int start;
for(int i=0;i<q;i++)
{
start=arr[i][0];
if(i!=q-1)
sum=sum+abs(start-arr[i][1])+abs(arr[i][1]-arr[i+1][0]);
else
sum=sum+abs(start-arr[i][1]);
}
printf("%d\n",sum+arr[0][0]);
}
return 0;
}

can someone tell me why am i getting wrong answer with this approach???

#include <bits/stdc++.h>
using namespace std;

long int N , Q ;

void solve(){
cin>>N>>Q;
long int a[N+1];
long int initial_floor[Q] , final_floor[Q];
long int count = 0;
for(long int i = 0 ; i<N+1 ; i++)
a[i] = i;

for(long int i = 0 ; i<Q ; i++)
   cin>>initial_floor[i]>>final_floor[i];
   
	   
for(long int i = 0 ; i<Q ; i++){
	
	if(i == 0){
		count = count + final_floor[i];
	}
	
	else{
		if(initial_floor[i] == final_floor[i-1]){
			count = count + abs(final_floor[i] - initial_floor[i]);
		}
		
		else
		{
	count = count + abs(initial_floor[i] - final_floor[i-1]) + abs(final_floor[i] - initial_floor[i]);
		}
	}
}
   
cout<<count<<'\n';   

}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

int T;
cin>>T;
while(T--){
	solve();
}
return 0;

}

Can anyone Please suggest me what is wrong with my approach.For sample input it is giving correct answer (6) but on submitting it gives WA.Please tell my error

Here is my solution
https://www.codechef.com/viewsolution/33277696

I’m also thinking…But it just for confusion :grinning:

1 Like

the solution of setter fails for f=3,d=2