forked from singh-shreya6/Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagnet-array-problem.cpp
More file actions
51 lines (49 loc) · 985 Bytes
/
magnet-array-problem.cpp
File metadata and controls
51 lines (49 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
ld getforce(ld a[],ld mid,ld n)
{
ld f=0;
for(ll i=0;i<n;i++)
{
if(i<=mid)
f=f-(1/(mid-a[i]));
else
f=f+(1/(a[i]-mid));
}
return f;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ld a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
for(ll i=0;i<n-1;i++)
{
ld left=a[i],right=a[i+1];
while(left<=right)
{
ld mid=(left+right)/2;
ld f=getforce(a,mid,n);
if(abs(f)<0.0000000000001)
{
cout<<fixed<<setprecision(2)<<mid<<" ";
break;
}
else if(f>0.0000000000001)
right=mid;
else
left=mid;
}
}
cout<<endl;
}
return 0;
}