709B - Checkpoints - CodeForces Solution


greedy implementation sortings *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e6+100;
ll n,a,ans=1e18+100;
ll x[N];
int main()
{
	cin>>n>>a;
	for(int i=1;i<=n;i++) cin>>x[i];
	sort(x+1,x+n+1);
	if(n==1){
		cout<<0<<endl;
		return 0;
	}
	if(x[n]<=a) cout<<a-x[2]<<endl;
	else if(x[1]>=a) cout<<x[n-1]-a<<endl;
	else{
		ans=min(ans,abs(a-x[n])+abs(x[n]-x[2]));
		ans=min(ans,abs(a-x[1])+abs(x[1]-x[n-1]));
		ans=min(ans,abs(a-x[2])+abs(x[2]-x[n]));
		ans=min(ans,abs(a-x[n-1])+abs(x[1]-x[n-1]));
		cout<<ans<<endl;
	}
}
/*
6 -9
-10 -7 4 -7 0 3
*/
	   	 	 	 	 		   				  		


Comments

Submit
0 Comments
More Questions

Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String