BFRIEND - Editorial

What is wrong with this code?
It shows TLE error.

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

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin>>t;

while(t--){
  long long time=0;
  long long min=INT_MAX;
  long long n,a,b,c;
  cin>>n>>a>>b>>c;
  if(b-a>=0){
    time=b-a+c;}
  else if(b-a<0){
    time=a-b+c;
  }
  long long i=0;
  while(n--){
    
    long long a[n];
    cin>>a[i];
    time=time+2*a[i];
    if(time<=min){
      min=time;
    }
    else if(time>INT_MAX){
      min=time;
    }
    time=time-2*a[i];
    i++;
    
  }
  cout<<min-2*b<<endl;

}
}

All the outputs are correct but why iam getting Time limit exceeded problem…Pls help…

#include

using namespace std;
int main()
{
long long int x,uu,t,n,a,i,b,c;
long long int f[100];
long long p[100];

cin>>t;
while(t–)
{
cin>>n>>a>>b>>c;
for(i=0;i<n;i++)
{
long long int element;
cin>>element;
p[i]=element;
int distance=abs(element-b);
f[i]=distance;
}
sort(f,f+n);
long long int fd=f[0];
int flag=0;
uu=0;
for(int it=0;it<n;i++)
{

        if(abs((p[it]-fd)==b))
        {
            uu=p[it];
            flag=1;
            break;
        }
        if(flag==1)
        break;
    }
    x=0;
    x=fd+c+(uu-a);

    cout<<x<<endl;


}



return 0;

}

why this is showing wrong ans??

#include<bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define vi vector
#define vii vector
#define ll long long
#define INF 1000000000
#define endl ‘\n’

using namespace std;

int main()
{
fastio;
cin.tie(0);
cout.tie(0);
short t;
int n,a,b,c;
int ma;
ll ans;
cin>>t;
while(t–)
{

    cin>>n>>a>>b>>c;
    int f[n];
    ans=abs(b-a)+c;

    ma=INT_MAX;
    a=max(a,b);
    b=min(a,b);
    for(int i=0;i<n;i++)
    {
        cin>>f[i];

    }
    for(int i=0;i<n;i++)
    {
        if(f[i]<=a && f[i]>=b){ ma=0; break;  }
      
          if(f[i]<b)
            {
                ma=min(ma,b-f[i]);
            }
            else
            {
               
                    ma=min(ma,f[i]-a);
                
            }
    }
    ans=ans+(2*ma);
    cout<<ans<<endl;




}




return 0;
}

tried to prform the operation all friends while taking input as well but that was showing TLE?

Here’s my code- It’s working perfecting fine for the given constraints in my machine but codechef says wrong answer(WA). Can anyone explain what’s wrong with my code?

Edited*

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

here’s the link of my submission, can you explain please?

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

Why are you using arr[0] but no other indices of arr?

1 Like

I’ve edited my previous comment now, please help.

        long long int arr[n];
        cin >> n >> a >> b >> c;

You’re allocating a dynamic array of size n before you’ve read in the value of n.

1 Like

Then why it is working completely fine on my machine and even on codechef when I provide custom input(same inputs given in the question) and even answer is correct.

There’s Undefined Behaviour for you :man_shrugging:

2 Likes

Sorry, I didn’t get it. :grimacing:

Which one exactly? Would appreciate if you can share😅

https://codeforces.com/contest/1293/problem/A
This one

#include
using namespace std;

int main() {
// your code goes here
int T;
cin>>T;
while(T–)
{
long long int N,a,b,c;
cin>>N>>a>>b>>c;
long long int F[N],mi=99999999999999999,k=0;
for(int i=0;i<N;i++)
{
cin>>F[i];
if(abs(F[i]-b)<mi)
{
k=F[i];mi=abs(F[i]-b);
}
}long long int re=mi+abs(k-a)+c;
cout<<re<<endl;

}
return 0;

}
whats wrong with this code

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

it has passed the first test case
but giving a WA

Why my code is showing RUN TIME ERROR?

#include <stdio.h>

#include <stdlib.h>

int main()

{

long int choice;

scanf("%d", &choice); // number of testcases

long long int n, alice, bob, time;

while (choice)

{

    scanf("%ld", &n);

    scanf("%lld", &alice);

    scanf("%lld", &bob);

    scanf("%lld", &time);

    long long int friends[n];

    for (int i = 0; i < n; i++)

    {

        scanf("%lld", &friends[i]);

    }

    long long int min = friends[0];

    for (int i = 1; i < n; i++)

    {

        if (friends[i] < min)

        {

            min = friends[i];

        }

    }

    long long int total_time = 2 * (min - alice) + time + (bob - alice);

    printf("%lld", total_time);

        choice--;

}



return 0;

}

@ssjgz Please look at my code and correct me the given test cases are passed but when i’m trying to submit it i’m getting WA :frowning:

public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
while(test-- > 0){
String str = br.readLine();
String[] arr = str.split("\s+");
int n = Integer.parseInt(arr[0]);
int a = Integer.parseInt(arr[1]);
int b = Integer.parseInt(arr[2]);
int c = Integer.parseInt(arr[3]);
str = br.readLine();
arr = str.split("\s+");
long result = Integer.MAX_VALUE;
long UP = Integer.MAX_VALUE;
long DOWN = Integer.MIN_VALUE;
while(n > 0){
int l = Integer.parseInt(arr[–n]);
UP= (Math.abs(l-a))+(Math.abs(l-b));
DOWN = (Math.abs(a-l))+(Math.abs(b-l));
result = Math.min((UP+c),(DOWN+c));
}
System.out.println(result);
}
}

Your solution: https://www.codechef.com/viewsolution/29266241 fails on the testcase:

1
3 17 67 4
72 52 78
1 Like

Please check this code

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

int main()
{
int t;
long long int N,a,b,c,x;
cin>>t;
for(long long int i=0;i<t;i++)
{
cin>>N>>a>>b>>c;
long long int A[N],x;
for(long long int k=0;k<N;k++)
{
cin>>A[k];
}
long long int min = abs(b - A[0]);
long long int f = A[0];
for(long long int m=1;m<N;m++)
{
x=abs(b-A[m]);
if(min>x)
{
f=A[m];
}
}
long long int y = abs(f - b) + c + abs(f - a);
cout<<y<<endl;
}
return 0;
}