492B - Vanya and Lanterns - CodeForces Solution


binary search implementation math sortings *1200

Please click on ads to support us..

Python Code:

def result(n):
    n=str(n)
    m=n.find(".")
    if m==-1:
        n+="."
        m=0
    for i in range(m,10):
        n+="0"
    return n
def count(a ,arr):
    n=0
    for i in range(len(arr)):
        if arr[i]==a:
            n+=1
        if arr[i]>a:
            return n
    return n
inp=input()
inp=inp.split(" ")
n=int(inp[0])
l=int(inp[1])
inp2=input()
inp2=inp2.split(" ")
inp2=[int(x) for x in inp2]
inp2.sort()
max=0
i_f=0
i_l=0
if inp2[0]>l-inp2[n-1]:
    max=inp2[0]*2
else:
    max=(l-inp2[n-1])*2
for i in range(n-1):
    
    if int(inp2[i+1])-int(inp2[i])>max:
        max=int(inp2[i+1])-int(inp2[i])
        i_f=inp2[i]
        i_l=inp2[i+1]
if n==1:
    if inp2[0]==0 or inp2[0]==l:
        max=l*2
    else:
        if inp2[0]>l-inp2[0]:
            max=inp2[0]*2
        else:
            max=(l-inp2[0])*2
    i_f=inp2[0]
    i_l=inp2[0]
if l-int(inp2[n-1])>max/2 and n!=1:
    if n==2:
        if int(inp2[1])-int(inp2[0])>0:
            max=(l-int(inp2[n-1]))*2
m=(count(i_f,inp2))
k=count(i_l,inp2)

print(result(max/(2)))

C++ Code:

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

#define PB push_back
#define INF (int)1e9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ll long long
#define F first
#define S second
#define All(x) x.begin(), x.end()
#define Sort(x) sort(All(x))

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n, l;
	cin>>n>>l;
	vector<double> a(n);
	for(ll i=0; i<n; i++)
		cin>>a[i];
	Sort(a);
	double md=0;
	for(ll i=0; i<n-1; i++)
		md=max(md, a[i+1]-a[i]);
	double x=0, y=0;
	if(a[0]!=0)
		x=a[0]-0;
	if(a[n-1]!=l)
		y=l-a[n-1];
	cout<<fixed << setprecision(9)<<max(md/2, max(x, y))<<endl;
	return 0;
}


Comments

Submit
0 Comments
More Questions

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
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal