Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = int(raw_input())
x = map(int,raw_input().split())
f = lambda l,r:(l,r) if l<=r else (r,l)
for i in range(n-1):
(l,r) = f(x[i],x[i+1])
for j in range(i+1,n-1):
(u,v) = f(x[j],x[j+1])
if not(r<=u or v<=l or u<=l<=r<=v or l<=u<=v<=r):
print "yes"
exit(0)
print "no"
| PYTHON |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | def check(a,b):
if min(a)<min(b)<max(a)<max(b) or min(b)<min(a)<max(b)<max(a):
return True
else:
return False
lo = False
n = int(input())
l = list(map(int, input().split()))
coppie = []
for cont in range(0,n-1):
coppie.append(list((l[cont],l[cont+1])))
for c in range(0,len(coppie)-1):
for d in range(c+1,len(coppie)):
if check(coppie[c],coppie[d]):
print('yes')
lo = True
break
if lo:
break
if lo is False:
print('no') | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
if (n <= 3) {
cout << "no" << endl;
return 0;
} else {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (min(v[i], v[i + 1]) < min(v[j], v[j + 1]) and
max(v[i], v[i + 1]) > min(v[j], v[j + 1]) and
max(v[i], v[i + 1]) < max(v[j], v[j + 1])) {
cout << "yes" << endl;
return 0;
}
if (max(v[i], v[i + 1]) > max(v[j], v[j + 1]) and
min(v[i], v[i + 1]) < max(v[j], v[j + 1]) and
min(v[j], v[j + 1]) < min(v[i], v[i + 1])) {
cout << "yes" << endl;
return 0;
}
}
}
cout << "no" << endl;
}
}
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
long x;
cin >> x;
long long *arr = new long long[x];
for (long i = 0; i < x; i++) {
cin >> arr[i];
}
int c = 0;
for (long i = 0; i < x - 1; i++) {
for (long j = 0; j < x - 1; j++) {
if (max(arr[i], arr[i + 1]) < max(arr[j], arr[j + 1]) &&
min(arr[i], arr[i + 1]) < min(arr[j], arr[j + 1]) &&
max(arr[i], arr[i + 1]) > min(arr[j], arr[j + 1]))
c++;
}
}
if (c == 0)
cout << "no";
else
cout << "yes";
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 4;
int a[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int x = 0;
int y = 0;
for (int j = i + 2; j < n; j++)
if ((a[j] > a[i] && a[j] > a[i + 1]) || (a[j] < a[i] && a[j] < a[i + 1]))
x++;
else
y++;
if (x && y) {
cout << "yes";
return 0;
}
}
cout << "no";
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = int(input())
arr = list(map(int, input().split()))
for i in range(n-2):
for j in range(i+1, n-1):
a = sorted([sorted([arr[i], arr[i + 1]]), sorted([arr[j], arr[j + 1]])])
if a[1][0] > a[0][0] and a[1][0] < a[0][1] and a[1][1] > a[0][1]:
print('yes')
exit()
print('no')
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = int(input())
cord = list(map(int,input().split()))
check = 0
for i in range(n-1):
a,b = cord[i],cord[i+1]
if a>b:
a,b = b,a
for j in range(n-1):
x,y = cord[j],cord[j+1]
if x>y:
x,y = y,x
if (a<x and b>x and b<y) or (a>x and a<y and b>y):
print('yes')
check = 1
break
if check==1:
break
if check==0:
print('no') | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | from math import sqrt
def mi():return map(int,input().split())
def li():return list(mi())
def ii():return int(input())
def si():return input()
n=ii()
a=li()
f=0
for i in range(1,n-1):
x=min(a[i],a[i+1])
y=max(a[i],a[i+1])
for j in range(i):
if(a[j]>x and a[j]<y):
if(j-1>=0):
if(a[j-1]<a[j] and a[j-1]<x):
f=1
break
if(a[j-1]>a[j] and a[j-1]>y):
f=1
break
if(j+1<n):
if(a[j+1]<a[j] and a[j+1]<x):
f=1
break
if(a[j+1]>a[j] and a[j+1]>y):
f=1
break
if(f==0):
print('no')
else:
print('yes')
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n=int(input())
a=list(map(int,input().split()))
b=a[:]
b.sort()
ok=0
c={}
g={}
for i in range(n-1):
l,r=b.index(a[i]),b.index(a[i+1])
c[b[l]]=r
g[r]=l
for i in range(n-1):
if not b[i] in c:continue
if i>c[b[i]]:l,r=c[b[i]],i
else:l,r=i,c[b[i]]
for j in range(l+1,r):
if b[j] in c:
if c.get(b[j])>r or c.get(b[j])<l:
ok=1
break
if j in g:
if g[j]>r or g[j]<l:
ok=1
break
if ok:print('yes')
else:print('no')
| PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
struct bla {
int comeca;
int fim;
bla() {
comeca = 0;
fim = 0;
}
};
vector<bla> v;
bool mysort(bla a, bla b) {
if (a.comeca != b.comeca) {
return a.comeca < b.comeca;
}
return a.fim < b.fim;
}
int main() {
bool pode;
int n, s, e, m;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
bla w = *(new bla());
v.push_back(w);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &m);
v[i].comeca = m;
}
for (int i = 1; i < n; ++i) {
v[i - 1].fim = v[i].comeca;
if (v[i - 1].fim < v[i - 1].comeca) {
v[i - 1].fim = v[i - 1].comeca;
v[i - 1].comeca = v[i].comeca;
}
}
v.pop_back();
sort(v.begin(), v.end(), mysort);
for (int i = 1; i < n; ++i) {
}
pode = false;
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
if (v[j].comeca < v[i].fim) {
if (v[j].comeca > v[i].comeca) {
if (v[j].fim > v[i].fim) {
pode = true;
goto final;
}
}
} else {
break;
}
}
}
final:
if (pode)
printf("yes\n");
else
printf("no\n");
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | n = int(input())
x = list(map(int, input().split()))
for i in range(n - 1):
a = sorted((x[i], x[i + 1]))
for j in range(i):
b = sorted((x[j], x[j + 1]))
if b[0] < a[0] < b[1] < a[1] or a[0] < b[0] < a[1] < b[1]:
print("yes")
exit()
print("no") | PYTHON3 |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int a[1010];
bool check(int a, int b, int c, int d) {
if (a > b) swap(a, b);
if (c > d) swap(c, d);
if (c > a && c < b && (d < a || d > b)) return 1;
if (d > a && d < b && (c < a || c > b)) return 1;
if (a > c && a < d && (b < c || b > d)) return 1;
if (b > c && b < d && (a < c || a > d)) return 1;
return 0;
}
int main() {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%d", &a[i]);
bool flag = 0;
for (int i = 2; i <= N; ++i)
for (int j = i + 1; j <= N; ++j) {
flag |= check(a[i], a[i - 1], a[j], a[j - 1]);
}
if (flag)
puts("yes");
else
puts("no");
return 0;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean yes = false;
int start [] = new int [n];
int end [] = new int [n];
if(n>1){
int x1 = sc.nextInt(), x2 = sc.nextInt();
start[0] = x1; end [0] = x2;
for(int i = 0; i < n-2; i++){
int y1 = x2, y2 = sc.nextInt();
for(int j = 0; j <= i; j++){
if( (Math.min(y1, y2) < Math.min(start[j], end[j])
&& Math.min(start[j], end[j]) < Math.max(y1, y2)
&& Math.max(y1, y2) < Math.max(start[j], end[j]))
|| (Math.min(start[j], end[j]) < Math.min(y1, y2)
&& Math.min(y1, y2) < Math.max(start[j], end[j])
&& Math.max(start[j], end[j]) < Math.max(y1, y2)) ){
yes = true;
}
}
x1 = y1; x2 = y2;
start[i+1] = x1; end[i+1] = x2;
}
}
sc.close();
System.out.println(yes ? "yes" : "no");
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.*;
import java.io.*;
public class Main implements Runnable {
public void solve() throws IOException {
int N = nextInt();
int[] arr = new int[N];
for(int i = 0; i < N; i++) arr[i] = nextInt();
boolean intersect = false;
for(int i = 0; i < N - 1; i++){
for(int j = 0; j < N - 1; j++){
if(i == j) continue;
int x = Math.min(arr[i], arr[i + 1] );
int xx = Math.max(arr[i], arr[i + 1]);
int y = Math.min(arr[j], arr[j + 1]);
int yy = Math.max(arr[j], arr[j + 1]);
if(x < y && y < xx && xx < yy) intersect = true;
}
}
if(intersect)System.out.println("yes");
else System.out.println("no");
}
//-----------------------------------------------------------
public static void main(String[] args) {
new Main().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
tok = null;
solve();
in.close();
} catch (IOException e) {
System.exit(0);
}
}
public String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
BufferedReader in;
StringTokenizer tok;
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.Scanner;
public class A {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int b = sc.nextInt();
int[] table1 = new int[n-1];
int[] table2 = new int[n-1];
boolean yes = false;
for(int i = 0; i<n-1; i++){
int a = b;
b = sc.nextInt();
table1[i] = a;
table2[i] = b;
}
for(int i = 0; i < n-2;i++){
for(int j = i+1; j<n-1; j++){
int imin = Math.min(table1[i], table2[i]);
int imax = Math.max(table1[i], table2[i]);
int jmin = Math.min(table1[j], table2[j]);
int jmax = Math.max(table1[j], table2[j]);
if(imin > jmin && imax > jmax && jmax > imin){
yes = true;
}
if(imin < jmin && imax < jmax && imax > jmin){
yes = true;
}
}
}
if(yes){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
}
| JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int dr[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dc[] = {0, -1, 0, 1, -1, 1, -1, 1};
const int MAX = 1e6 + 7;
const int MOD = 1e9 + 7;
int arr[MAX];
int main() {
int a, b, c, d, n, f = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
a = arr[0], b = arr[1];
for (int i = 0; i + 1 < n; i++)
for (int l = 0; l + 1 < n; l++) {
a = arr[i], b = arr[i + 1], c = arr[l], d = arr[l + 1];
if (a > b) swap(a, b);
if (c > d) swap(c, d);
if (c > a && b < d && c < b) f = 1;
}
if (f)
cout << "yes";
else
cout << "no\n";
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
vector<long long int> v(n);
if (n == 1) {
cout << "no" << endl;
return 0;
}
for (long long int i = 0; i < n; i++) {
cin >> v[i];
}
vector<pair<long long int, long long int> > p;
for (long long int i = 0; i < (n - 1); i++) {
if (v[i] < v[i + 1]) {
p.push_back(make_pair(v[i], v[i + 1]));
} else {
p.push_back(make_pair(v[i + 1], v[i]));
}
}
for (long long int i = 0; i < p.size() - 1; i++) {
for (long long int j = (i + 1); j < p.size(); j++) {
long long int a1 = p[i].first;
long long int b1 = p[i].second;
long long int a2 = p[j].first;
long long int b2 = p[j].second;
if (a1 < a2) {
if (a2 < b1 && b1 < b2 && a1 < a2 && a2 < b1) {
cout << "yes" << endl;
return 0;
}
} else {
if (a2 < a1 && a1 < b2 && a1 < b2 && b2 < b1) {
cout << "yes" << endl;
return 0;
}
}
}
}
cout << "no" << endl;
}
| CPP |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++)
a[i] = s.nextInt();
int x, xx, y, yy;
for(int i = 0; i + 1 < n; i++){
xx = Math.max(a[i], a[i+1]);
x = Math.min(a[i], a[i+1]);
for(int j = 0; j + 1 <= i; j++){
yy = Math.max(a[j], a[j+1]);
y = Math.min(a[j], a[j+1]);
if(y >= x && y < xx && (yy > xx && x != y)){
System.out.println("yes");
System.exit(0);
} else if(x >= y && x < yy && (xx > yy && x != y)){
System.out.println("yes");
System.exit(0);
}
}
}
System.out.println("no");
}
} | JAVA |
358_A. Dima and Continuous Line | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework.
The teacher gave Seryozha the coordinates of n distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the n-th point. Two points with coordinates (x1, 0) and (x2, 0) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any).
<image>
Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem.
Input
The first line contains a single integer n (1 β€ n β€ 103). The second line contains n distinct integers x1, x2, ..., xn ( - 106 β€ xi β€ 106) β the i-th point has coordinates (xi, 0). The points are not necessarily sorted by their x coordinate.
Output
In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes).
Examples
Input
4
0 10 5 15
Output
yes
Input
4
0 15 5 10
Output
no
Note
The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 2 | 7 | import java.util.*;
import java.io.*;
public class File {
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
List<int[]> ranges = new ArrayList<>();
int prev = sc.nextInt();
for (int i = 1; i < n; i++) {
int x = sc.nextInt();
ranges.add(new int[] {Math.min(prev, x), Math.max(prev, x)});
prev = x;
}
boolean overlaps = false;
for (int i = 0; i < ranges.size(); i++) {
for (int j = i + 1; j < ranges.size(); j++) {
int[] range1 = ranges.get(i);
int[] range2 = ranges.get(j);
if (doesOverlap(range1, range2)) {
overlaps = true;
break;
}
}
}
if (overlaps) {
out.println("yes");
}
else {
out.println("no");
}
out.close();
}
public static boolean doesOverlap(int[] a, int[] b) {
boolean type1 = a[0] < b[1] && a[1] > b[0] && a[1] < b[1] && a[0] < b[0];
boolean type2 = a[0] < b[1] && a[1] > b[0] && a[1] > b[1] && a[0] > b[0];
return type1 || type2;
}
}
| JAVA |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int T = 60, maxn = 3e5 + 5;
struct node {
int id, val;
} a[maxn];
int n;
int pre[maxn], nxt[maxn];
bool cmp(node a, node b) {
return a.val == b.val ? a.id < b.id : a.val < b.val;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].val);
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
nxt[i] = i + 1, pre[i] = i - 1;
}
double ans = 0.0;
for (int i = 1; i <= n; i++) {
int x = a[i].id;
int L = x, R = x;
double l = 0, r = 0, div = 1;
for (int j = 1; j <= T; j++) {
div *= .5;
if (L) {
l += div * (L - pre[L]);
L = pre[L];
}
if (R <= n) {
r += div * (nxt[R] - R);
R = nxt[R];
}
}
ans += 2 * a[i].val * l * r;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
}
printf("%f\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n;
struct Node {
int pos, v;
} a[N];
int pre[N], nxt[N];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].v, a[i].pos = i;
sort(a + 1, a + n + 1, [](Node a, Node b) { return a.v < b.v; });
for (int i = 1; i <= n; i++) pre[i] = i - 1, nxt[i] = i + 1;
double ans = 0;
for (int i = 1; i <= n; i++) {
int pos = a[i].pos, l = pos, r = pos;
double cur = 0.5, lv = 0, rv = 0;
for (int _ = 1; _ <= 60; _++, cur /= 2) {
if (l) lv += cur * (l - pre[l]), l = pre[l];
if (r <= n) rv += cur * (nxt[r] - r), r = nxt[r];
}
ans += lv * rv * a[i].v * 2;
nxt[pre[pos]] = nxt[pos];
pre[nxt[pos]] = pre[pos];
}
printf("%.8lf", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
set<int> S;
vector<int> vr[maxn];
vector<int> vl[maxn];
int a[maxn];
struct NN {
int ind, val;
} nn[maxn];
int cmp(NN a, NN b) { return a.val < b.val; }
long double arr[110];
int main() {
int n, i, j, k;
for (arr[0] = 1, i = 1; i <= 30; i++) arr[i] = arr[i - 1] / 2;
scanf("%d", &n);
for (i = 1; i <= n; i++)
vl[i].push_back(0), vr[i].push_back(0), scanf("%d", &a[i]),
nn[i].ind = i, nn[i].val = a[i];
sort(nn + 1, nn + 1 + n, cmp);
for (i = n; i >= 1; i--) {
set<int>::iterator it;
int cnt = 0;
for (it = S.lower_bound(nn[i].ind); it != S.end(); it++) {
vr[nn[i].ind].push_back((*it) - nn[i].ind);
++cnt;
if (cnt == 30) break;
}
it = S.lower_bound(nn[i].ind);
if (it == S.begin()) {
S.insert(nn[i].ind);
vl[nn[i].ind].push_back(nn[i].ind);
vr[nn[i].ind].push_back(n + 1 - nn[i].ind);
continue;
}
cnt = 0;
it--;
for (;; it--) {
vl[nn[i].ind].push_back(nn[i].ind - (*it));
++cnt;
if (cnt == 30 || it == S.begin()) break;
}
vl[nn[i].ind].push_back(nn[i].ind);
vr[nn[i].ind].push_back(n + 1 - nn[i].ind);
S.insert(nn[i].ind);
}
long double ans = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j < vl[i].size(); j++)
for (k = 1; k < vr[i].size(); k++)
if (j + k - 1 <= 30)
ans += a[i] * arr[j + k - 1] * (vl[i][j] - vl[i][j - 1]) *
(vr[i][k] - vr[i][k - 1]);
else
break;
}
double aa = ans / n / n;
printf("%.12lf\n", aa);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long int N;
double er[300020], fr[300020], bk[300020], ans;
pair<double, int> ar[300020];
set<int> asd;
int main() {
scanf("%lld", &N);
for (int i = 1; i <= N; i++)
scanf("%lf", &er[i]), ar[i] = pair<double, int>(er[i], i);
sort(ar + 1, ar + N + 1);
reverse(ar + 1, ar + N + 1);
asd.insert(0);
asd.insert(N + 1);
for (int i = 1; i <= N; i++) {
asd.insert(ar[i].second);
double a = 0, b = 0;
set<int>::iterator it = asd.find(ar[i].second);
double bol = 1;
int pre = (*it);
it--;
for (int k = 1; k <= 55; it--, k++) {
a += (pre - (*it)) * bol;
pre = (*it);
if (it == asd.begin()) break;
bol /= 2;
}
it = asd.find(ar[i].second);
bol = 1;
pre = (*it);
it++;
for (int k = 1; k <= 55 && it != asd.end(); it++, k++) {
b += ((*it) - pre) * bol;
pre = (*it);
bol /= 2;
}
ans += a * b * er[ar[i].second] / 2.0;
}
double bol = N * N;
printf("%.15lf\n", ans / bol);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
ESerejaAndDividing solver = new ESerejaAndDividing();
solver.solve(1, in, out);
out.close();
}
}
static class ESerejaAndDividing {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.ri();
int[] b = in.ri(n);
int[] indices = IntStream.range(0, n).toArray();
SortUtils.quickSort(indices, (i, j) -> -Integer.compare(b[i], b[j]), 0, n);
IntegerArrayList prev = new IntegerArrayList();
IntegerArrayList next = new IntegerArrayList();
RangeTree rt = new RangeTree(n + 2);
rt.add(0);
rt.add(n + 1);
double ans = 0;
int lim = 60;
for (int index : indices) {
prev.clear();
prev.add(index + 1);
for (int i = 0, x = index + 1; i < lim; i++) {
int floor = rt.floor(x - 1);
if (floor == -1) {
break;
}
x = floor;
prev.add(x);
}
next.clear();
next.add(index + 1);
for (int i = 0, x = index + 1; i < lim; i++) {
int ceil = rt.ceil(x + 1);
if (ceil == -1) {
break;
}
x = ceil;
next.add(x);
}
int[] A = prev.getData();
int[] B = next.getData();
int aSize = prev.size();
int bSize = next.size();
double L = 0;
double R = 0;
for (int i = 0; i + 1 < aSize; i++) {
L += (double) (A[i] - A[i + 1]) / (1L << i);
}
for (int i = 0; i + 1 < bSize; i++) {
R += (double) (B[i + 1] - B[i]) / (1L << i);
}
ans += b[index] * L * R / 2;
rt.add(index + 1);
}
ans /= n;
ans /= n;
out.println(ans);
}
}
static class IntegerArrayList implements Cloneable {
private int size;
private int cap;
private int[] data;
private static final int[] EMPTY = new int[0];
public int[] getData() {
return data;
}
public IntegerArrayList(int cap) {
this.cap = cap;
if (cap == 0) {
data = EMPTY;
} else {
data = new int[cap];
}
}
public IntegerArrayList(int[] data) {
this(0);
addAll(data);
}
public IntegerArrayList(IntegerArrayList list) {
this.size = list.size;
this.cap = list.cap;
this.data = Arrays.copyOf(list.data, size);
}
public IntegerArrayList() {
this(0);
}
public void ensureSpace(int req) {
if (req > cap) {
while (cap < req) {
cap = Math.max(cap + 10, 2 * cap);
}
data = Arrays.copyOf(data, cap);
}
}
public void add(int x) {
ensureSpace(size + 1);
data[size++] = x;
}
public void addAll(int[] x) {
addAll(x, 0, x.length);
}
public void addAll(int[] x, int offset, int len) {
ensureSpace(size + len);
System.arraycopy(x, offset, data, size, len);
size += len;
}
public void addAll(IntegerArrayList list) {
addAll(list.data, 0, list.size);
}
public int size() {
return size;
}
public int[] toArray() {
return Arrays.copyOf(data, size);
}
public void clear() {
size = 0;
}
public String toString() {
return Arrays.toString(toArray());
}
public boolean equals(Object obj) {
if (!(obj instanceof IntegerArrayList)) {
return false;
}
IntegerArrayList other = (IntegerArrayList) obj;
return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
}
public int hashCode() {
int h = 1;
for (int i = 0; i < size; i++) {
h = h * 31 + Integer.hashCode(data[i]);
}
return h;
}
public IntegerArrayList clone() {
IntegerArrayList ans = new IntegerArrayList();
ans.addAll(this);
return ans;
}
}
static class Bits {
private Bits() {
}
public static long set(long x, int i) {
return x | (1L << i);
}
public static int highestOneBitOffset(long x) {
if (x < 0) {
return 63;
}
return 63 - Long.numberOfLeadingZeros(x);
}
public static int lowestOneBitOffset(long x) {
return highestOneBitOffset(x & -x);
}
public static long tailMask(int n) {
if (n == 0) {
return 0;
}
return -1L << (64 - n);
}
public static long headMask(int n) {
if (n == 0) {
return 0;
}
return -1L >>> (64 - n);
}
}
static interface IntegerIterator {
boolean hasNext();
int next();
}
static class RandomWrapper {
private Random random;
public static final RandomWrapper INSTANCE = new RandomWrapper();
public RandomWrapper() {
this(new Random());
}
public RandomWrapper(Random random) {
this.random = random;
}
public RandomWrapper(long seed) {
this(new Random(seed));
}
public int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
static class SequenceUtils {
public static void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
if ((ar - al) != (br - bl)) {
return false;
}
for (int i = al, j = bl; i <= ar; i++, j++) {
if (a[i] != b[j]) {
return false;
}
}
return true;
}
}
static interface IntegerComparator {
public int compare(int a, int b);
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private final Writer os;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(double c) {
cache.append(new BigDecimal(c).toPlainString());
afterWrite();
return this;
}
public FastOutput println(double c) {
return append(c).println();
}
public FastOutput println() {
return append('\n');
}
public FastOutput flush() {
try {
// boolean success = false;
// if (stringBuilderValueField != null) {
// try {
// char[] value = (char[]) stringBuilderValueField.get(cache);
// os.write(value, 0, cache.length());
// success = true;
// } catch (Exception e) {
// }
// }
// if (!success) {
os.append(cache);
// }
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
public void populate(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = readInt();
}
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int ri() {
return readInt();
}
public int[] ri(int n) {
int[] ans = new int[n];
populate(ans);
return ans;
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class RangeTree {
private long[][] data;
private static int bitShift = 6;
private static int bitShiftValue = 1 << bitShift;
private static int bitShiftValueMask = bitShiftValue - 1;
private int size;
private int n;
private int calcLevel(int n) {
int level = 0;
while (n > 64) {
level++;
n = (n + 63) / 64;
}
level++;
return level;
}
public RangeTree(int n) {
this.n = n;
int level = calcLevel(n);
data = new long[level][];
for (int i = 0, m = (n + 63) / 64; i < level; i++, m = (m + 63) / 64) {
data[i] = new long[m];
}
}
public boolean contains(int x) {
return ((data[0][x >> bitShift] >>> (x & bitShiftValueMask)) & 1) == 1;
}
public void add(int x) {
if (contains(x)) {
return;
}
size++;
for (int i = 0; i < data.length; i++) {
int offset = x & bitShiftValueMask;
x >>= bitShift;
data[i][x] = Bits.set(data[i][x], offset);
}
}
private int lastSet(int i, int x) {
assert i < 0 || data[i][x] != 0;
for (; i >= 0; i--) {
int offset = Bits.highestOneBitOffset(data[i][x]);
x = (x << bitShift) | offset;
}
return x;
}
private int firstSet(int i, int x) {
assert i < 0 || data[i][x] != 0;
for (; i >= 0; i--) {
int offset = Bits.lowestOneBitOffset(data[i][x]);
x = (x << bitShift) | offset;
}
return x;
}
public int floor(int x) {
if (x < 0) {
return -1;
}
if (contains(x)) {
return x;
}
for (int i = 0, y = x; i < data.length; i++) {
int offset = y & bitShiftValueMask;
y = y >>> bitShift;
long headMask = Bits.headMask(offset);
if ((data[i][y] & headMask) != 0) {
return lastSet(i - 1, (y << bitShift) | Bits.highestOneBitOffset(data[i][y] & headMask));
}
}
return -1;
}
public int ceil(int x) {
if (x >= n) {
return -1;
}
if (contains(x)) {
return x;
}
for (int i = 0, y = x; i < data.length; i++) {
int offset = y & bitShiftValueMask;
y = y >>> bitShift;
long tailMask = Bits.tailMask(63 - offset);
if ((data[i][y] & tailMask) != 0) {
return firstSet(i - 1, (y << bitShift) | Bits.lowestOneBitOffset(data[i][y] & tailMask));
}
}
return -1;
}
public IntegerIterator iterator() {
return iterator(0, n - 1);
}
public IntegerIterator iterator(int l, int r) {
return new IntegerIterator() {
int cur = ceil(l);
public boolean hasNext() {
return cur <= r && cur != -1;
}
public int next() {
int ans = cur;
if (cur == r) {
cur = -1;
} else {
cur = ceil(cur + 1);
}
assert cur == -1 || cur > ans;
return ans;
}
};
}
public String toString() {
IntegerIterator iterator = iterator();
List<Integer> list = new ArrayList<>(n);
for (; iterator.hasNext(); ) {
list.add(iterator.next());
}
return list.toString();
}
}
static class SortUtils {
private static final int THRESHOLD = 8;
private SortUtils() {
}
public static void insertSort(int[] data, IntegerComparator cmp, int l, int r) {
for (int i = l + 1; i <= r; i++) {
int j = i;
int val = data[i];
while (j > l && cmp.compare(data[j - 1], val) > 0) {
data[j] = data[j - 1];
j--;
}
data[j] = val;
}
}
public static void quickSort(int[] data, IntegerComparator cmp, int f, int t) {
if (t - f <= THRESHOLD) {
insertSort(data, cmp, f, t - 1);
return;
}
SequenceUtils.swap(data, f, RandomWrapper.INSTANCE.nextInt(f, t - 1));
int l = f;
int r = t;
int m = l + 1;
while (m < r) {
int c = cmp.compare(data[m], data[l]);
if (c == 0) {
m++;
} else if (c < 0) {
SequenceUtils.swap(data, l, m);
l++;
m++;
} else {
SequenceUtils.swap(data, m, --r);
}
}
quickSort(data, cmp, f, l);
quickSort(data, cmp, m, t);
}
}
}
| JAVA |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[300010];
void read() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
}
double work() {
static int prev[300010], next[300010];
static pair<int, int> v[300010];
for (int i = 1; i <= n; ++i)
v[i] = make_pair(a[i], i), prev[i] = i - 1, next[i] = i + 1;
sort(v + 1, v + n + 1);
const int L = 40;
static double power[L + 10];
power[0] = 1;
for (int i = 1; i <= L; ++i) power[i] = power[i - 1] / 2;
double ans = 0;
for (int i = 1; i <= n; ++i) {
int idx = v[i].second;
double sumL = 0, sumR = 0;
for (int j = idx, tot = 1; j != 0 && tot <= L; j = prev[j], ++tot)
sumL = sumL + (double)(j - prev[j]) / n * power[tot];
for (int j = idx, tot = 1; j != n + 1 && tot <= L; j = next[j], ++tot)
sumR = sumR + (double)(next[j] - j) / n * power[tot - 1];
ans = ans + v[i].first * sumL * sumR;
next[prev[idx]] = next[idx], prev[next[idx]] = prev[idx];
}
return ans;
}
int main() {
read();
printf("%.20f\n", work());
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
set<int> s;
int a[300300];
pair<int, int> b[300300];
int n;
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
if (a.first > b.first) return (true);
if (a.first < b.first) return (false);
return (a.second < b.second);
}
void init(void) {
scanf("%d", &n);
for (int i = (1); i <= (n); i = i + 1) scanf("%d", &a[i]);
for (int i = (1); i <= (n); i = i + 1) b[i] = pair<int, int>(a[i], i);
sort(b + 1, b + n + 1, cmp);
}
void process(void) {
double S = 0.0;
s.insert(0);
s.insert(n + 1);
for (int i = (1); i <= (n); i = i + 1) {
double L = 0.0;
double R = 0.0;
set<int>::iterator it = s.insert(b[i].second).first;
set<int>::iterator jt = it;
int prev = *it;
for (int j = 0; j < (60); j = j + 1) {
jt--;
L = L + 1.0 * (prev - *jt) / (1LL << j);
if (*jt < 1) break;
prev = *jt;
}
jt = it;
prev = *it;
for (int j = 0; j < (60); j = j + 1) {
jt++;
R = R + 1.0 * (*jt - prev) / (1LL << j);
if (*jt > n) break;
prev = *jt;
}
S = S + b[i].first * L * R / 2.0;
}
printf("%.9lf", S / n / n);
}
int main(void) {
init();
process();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse3", "sse2", "sse")
#pragma GCC target("avx", "sse4", "sse4.1", "sse4.2", "ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline", "fast-math", "unroll-loops", \
"no-stack-protector")
using namespace std;
template <typename Iter>
ostream& _out(ostream& s, Iter b, Iter e) {
s << "[";
for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it;
s << "]";
return s;
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
return in >> p.first >> p.second;
}
template <typename T>
ostream& operator<<(ostream& s, const vector<T>& c) {
return _out(s, c.begin(), c.end());
}
template <typename T, size_t N>
ostream& operator<<(ostream& s, const array<T, N>& c) {
return _out(s, c.begin(), c.end());
}
inline char readchar() {
const int64_t S = 1 << 20;
static char buf[S], *p = buf, *q = buf;
if (p == q && (q = (p = buf) + fread(buf, 1, S, stdin)) == buf) return EOF;
return *p++;
}
inline void input(int64_t& _x) {
_x = 0;
int64_t _tmp = 1;
char _tc = readchar();
while (_tc < '0' || _tc > '9') _tc = readchar();
while (_tc >= '0' && _tc <= '9')
_x = (_x << 1) + (_x << 3) + (_tc - 48), _tc = readchar();
}
inline void output(int64_t _x) {
char _buff[20];
int64_t _f = 0;
if (_x == 0) putchar('0');
while (_x > 0) {
_buff[_f++] = _x % 10 + '0';
_x /= 10;
}
for (_f -= 1; _f >= 0; _f--) putchar(_buff[_f]);
putchar('\n');
}
struct Q {
long double man = 1;
int64_t ex = 0;
Q(long double i, int64_t j = 0) {
assert(abs(i) <= 1e-7 || i >= 1 - 1e-7);
man = i, ex += j;
}
friend Q operator+(const Q a, const Q b) {
if (abs(a.man) <= 1e-7) return b;
if (abs(b.man) <= 1e-7) return a;
return Q(a.man * pow(0.5, a.ex - min(a.ex, b.ex)) +
b.man * pow(0.5, b.ex - min(a.ex, b.ex)),
min(a.ex, b.ex));
}
friend Q operator*(const Q a, const Q b) {
return Q(a.man * b.man, a.ex + b.ex);
}
operator long double() { return man * pow(0.5, ex); }
};
struct node {
node *L, *R;
int64_t l, r, mid;
Q sum = 1;
int64_t lt = 0;
node(int64_t a, int64_t b) {
l = a, r = b, mid = l + r >> 1;
if (l != r) L = new node(l, mid), R = new node(mid + 1, r), up();
}
void upd(int64_t x) { lt += x, sum.ex += x; }
void down() { L->upd(lt), R->upd(lt), lt = 0; }
void up() { sum = L->sum + R->sum; }
void update(int64_t ll, int64_t rr) {
if (ll <= l && rr >= r) return upd(1);
if (ll > r || rr < l) return;
down();
L->update(ll, rr);
R->update(ll, rr);
up();
}
Q query(int64_t ll, int64_t rr) {
if (ll <= l && rr >= r) return sum;
if (ll > r || rr < l) return 0;
down();
return L->query(ll, rr) + R->query(ll, rr);
}
int64_t getLT(int64_t i) {
if (l == r) return lt;
return down(), (i <= mid ? L : R)->getLT(i);
}
} * rootl, *rootr;
vector<int64_t> v;
vector<pair<int64_t, int64_t> > evts;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
;
int64_t n;
cin >> n;
v.resize(n + 1);
for (int64_t i = 1; i <= n; i++) cin >> v[i], evts.push_back({v[i], i});
rootl = new node(1, n);
rootr = new node(1, n);
sort((evts).begin(), (evts).end());
reverse((evts).begin(), (evts).end());
long double ans = 0;
for (auto [x, pos] : evts) {
ans += rootl->query(1, pos) * rootr->query(pos, n) *
Q(1, -rootl->getLT(pos) - rootr->getLT(pos) + 1) / n / n * x;
rootl->update(1, pos), rootr->update(pos, n);
}
cout << setprecision(20) << fixed << ans << endl;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
const int S = 21;
int n, b[MAXN];
int lp[MAXN], rp[MAXN], x;
vector<pair<int, int> > q;
double ans = 0;
void init() {
for (int i = 0; i <= n; i++) {
lp[i + 1] = i;
rp[i] = i + 1;
}
b[0] = MAXN;
b[n + 1] = MAXN;
}
int findL(int i) {
if (b[i] >= x) return i;
lp[i] = findL(lp[i]);
return lp[i];
}
int findR(int i) {
if (b[i] > x) return i;
rp[i] = findR(rp[i]);
return rp[i];
}
double p2(int a) {
double res = 1.;
for (int i = 0; i < a; i++) res /= 2.;
return res;
}
void proc(int i) {
x = b[i];
int ll = i, rr = i;
vector<int> lg, rg;
while (ll >= 1 && lg.size() < S) {
lg.push_back(ll);
ll = findL(lp[ll]);
}
while (rr <= n && rg.size() < S) {
rg.push_back(rr);
rr = findR(rp[rr]);
}
if (lg.size() < S) lg.push_back(ll);
if (rg.size() < S) rg.push_back(rr);
double cur = 0;
for (int a = 0; a + 1 < lg.size(); a++) {
for (int b = 0; b + 1 < rg.size(); b++) {
cur += p2(a + b + 1) * (double)(rg[b + 1] - rg[b]) *
(double)(lg[a] - lg[a + 1]);
}
}
ans += cur * b[i];
}
int main() {
scanf("%d", &n);
init();
for (int i = 1; i <= n; i++) {
scanf("%d", b + i);
q.push_back(make_pair(b[i], i));
}
sort(q.begin(), q.end());
for (pair<int, int> qq : q) {
proc(qq.second);
}
printf("%.8lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Comparator;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
static class Item {
int pos;
int what;
Item(int pos, int what) {
this.pos = pos;
this.what = what;
}
}
static final double SMALL = Math.pow(0.5, 30);
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
Item[] b = new Item[n];
for (int i = 0; i < n; ++i) b[i] = new Item(i + 1, in.nextInt());
Arrays.sort(b, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o2.what - o1.what;
}
});
double res = 0;
int[] next = new int[n + 2];
int[] prev = new int[n + 2];
int[] first = new int[4 * n + 20];
initFirst(first, 0, 0, n + 1);
setFirst(first, 0, 0, n + 1, 0);
setFirst(first, 0, 0, n + 1, n + 1);
int[] last = new int[4 * n + 20];
initFirst(last, 0, 0, n + 1);
setFirst(last, 0, 0, n + 1, 0);
setFirst(last, 0, 0, n + 1, n + 1);
Arrays.fill(next, -1);
Arrays.fill(prev, -1);
next[0] = n + 1;
prev[n + 1] = 0;
for (Item cur : b) {
int after = findFirst(first, 0, 0, n + 1, cur.pos);
int before = n + 1 - findFirst(last, 0, 0, n + 1, n + 1 - cur.pos);
int left = cur.pos;
int right = cur.pos;
double leftSum = cur.pos - before;
int tmp = before;
double mult = 1.0;
while (tmp > 0 && mult > SMALL) {
mult *= 0.5;
leftSum += mult * (tmp - prev[tmp]);
tmp = prev[tmp];
}
double rightSum = after - cur.pos;
tmp = after;
mult = 1.0;
while (tmp < n + 1 && mult > SMALL) {
mult *= 0.5;
rightSum += mult * (next[tmp] - tmp);
tmp = next[tmp];
}
res += 0.5 * cur.what * leftSum * rightSum;
prev[cur.pos] = before;
next[cur.pos] = after;
next[before] = cur.pos;
prev[after] = cur.pos;
setFirst(first, 0, 0, n + 1, cur.pos);
setFirst(last, 0, 0, n + 1, n + 1 - cur.pos);
}
out.printf(String.format("%.15f", res / n / n));
}
private void setFirst(int[] first, int root, int rl, int rr, int at) {
if (at < first[root])
first[root] = at;
if (rl == rr) {
return;
}
int rm = (rl + rr) / 2;
if (at <= rm)
setFirst(first, root * 2 + 1, rl, rm, at);
else
setFirst(first, root * 2 + 2, rm + 1, rr, at);
}
private void initFirst(int[] first, int root, int rl, int rr) {
first[root] = rr + 1;
if (rl == rr) {
return;
}
int rm = (rl + rr) / 2;
initFirst(first, root * 2 + 1, rl, rm);
initFirst(first, root * 2 + 2, rm + 1, rr);
}
private int findFirst(int[] first, int root, int rl, int rr, int atleast) {
if (atleast == rl) {
return first[root];
}
int rm = (rl + rr) / 2;
if (rm < atleast) {
return findFirst(first, root * 2 + 2, rm + 1, rr, atleast);
} else {
int x = findFirst(first, root * 2 + 1, rl, rm, atleast);
if (x > rm) {
x = findFirst(first, root * 2 + 2, rm + 1, rr, rm + 1);
}
return x;
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| JAVA |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-6;
const int INF = 0x3fffffff;
const long long LINF = INF * 1ll * INF;
const double PI = acos(-1.0);
using namespace std;
int faL[300005], faR[300005];
int a[300005];
pair<int, int> p[300005];
int findL(int u) { return u == faL[u] ? u : faL[u] = findL(faL[u]); }
int findR(int u) { return u == faR[u] ? u : faR[u] = findR(faR[u]); }
int main(void) {
int n;
scanf("%d", &n);
for (int i = 0; i <= (n + 1); ++i) faL[i] = i, faR[i] = i;
for (int i = 0; i < (n); ++i) {
scanf("%d", &a[i]);
p[i] = make_pair(a[i], i + 1);
}
sort(p, p + n);
double ans = 0;
for (int i = 0; i < (n); ++i) {
int pos = p[i].second;
double L = 0, R = 0;
double now = 0.5;
int last = pos;
for (int u = findR(pos + 1), cnt = 0; cnt <= 60; u = findR(u + 1), cnt++) {
L += (u - last) * now;
if (u > n) break;
last = u;
now /= 2;
}
last = pos;
now = 0.5;
for (int u = findL(pos - 1), cnt = 0; cnt <= 60; u = findL(u - 1), cnt++) {
R += (last - u) * now;
if (u == 0) break;
now /= 2;
last = u;
}
faR[pos] = pos + 1;
faL[pos] = pos - 1;
ans += L * R * p[i].first;
}
printf("%.15lf\n", ans * 2 / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
int n, a[maxn], r[maxn];
bool cmp(int x, int y) {
if (a[x] != a[y]) return a[x] > a[y];
return x < y;
}
double L[maxn], R[maxn];
double two[maxn];
int main() {
two[0] = 1;
for (int i = 1; i < maxn; i++) two[i] = two[i - 1] / 2;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) r[i] = i;
sort(r + 1, r + 1 + n, cmp);
set<int> S;
S.insert(0);
S.insert(n + 1);
for (int i = 1; i <= n; i++) {
int id = r[i];
S.insert(id);
set<int>::iterator it = S.find(id);
L[id] = 0;
for (int j = 1; j <= 100; j++) {
int xx = *it;
it--;
L[id] += two[j] * (xx - (*it));
if (it == S.begin()) break;
}
it = S.find(id);
for (int j = 0; j <= 100; j++) {
int xx = *it;
it++;
if (it == S.end()) break;
R[id] += two[j] * (*it - xx);
}
}
double ans = 0;
double ss = 0;
for (int i = 1; i <= n; i++) {
ss += L[i] * R[i] * a[i];
if (ss >= 1e10) {
ans += ss / n / n;
ss = 0;
}
}
ans += ss / n / n;
printf("%.15f\n", ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int b[300010], p[300010], l[300010], r[300010];
bool cmp(int i, int j) { return b[i] < b[j]; }
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
p[i] = i;
l[i] = i - 1;
r[i] = i + 1;
}
sort(p + 1, p + n + 1, cmp);
double ans = 0;
for (int k = 1; k <= n; k++) {
int i = p[k], x = i, y = i;
double z = 1, L = 0, R = 0;
for (int j = 1; j <= 45; j++) {
if (x) {
L += (x - l[x]) * z;
x = l[x];
}
if (y <= n) {
R += (r[y] - y) * z;
y = r[y];
}
z /= 2;
}
l[r[i]] = l[i];
r[l[i]] = r[i];
ans += L * R * b[i] / 2;
}
printf("%.6lf\n", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int inf = 1e9 + 7;
int a[N], pos[N];
int pre[N], nxt[N];
inline bool cmp(const int &x, const int &y) { return a[x] < a[y]; }
int main() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); i++)
pos[i] = i, pre[i] = i - 1, nxt[i] = i + 1, scanf("%d", &a[i]);
sort(pos + 1, pos + 1 + n, cmp);
double ans = 0.0;
for (int i = (1); i <= (n); i++) {
int l = pos[i], r = pos[i];
double s1 = 0, s2 = 0, coe = 1.0;
for (int j = (1); j <= (50); j++) {
if (l) s1 += 1.0 * (l - pre[l]) * coe, l = pre[l];
if (r <= n) s2 += 1.0 * (nxt[r] - r) * coe, r = nxt[r];
coe *= 0.5;
}
ans += s1 * s2 * (double)a[pos[i]];
pre[nxt[pos[i]]] = pre[pos[i]];
nxt[pre[pos[i]]] = nxt[pos[i]];
}
printf("%.12lf\n", ans / (double)n / (double)n / 2.0);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int T = 50;
struct Q {
int id, v;
} a[1000010];
int n;
double ans;
int nxt[1000010], pre[1000010];
bool cmp(Q a, Q b) {
if (a.v == b.v)
return a.id < b.id;
else
return a.v < b.v;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].v), a[i].id = i;
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++) nxt[i] = i + 1, pre[i] = i - 1;
for (int i = 1; i <= n; i++) {
int x = a[i].id, lp = x, rp = x;
double l = 0, r = 0, mi = 1;
for (int j = 1; j <= T; j++) {
mi *= 0.5;
if (lp) l += mi * (lp - pre[lp]), lp = pre[lp];
if (rp <= n) r += mi * (nxt[rp] - rp), rp = nxt[rp];
}
ans += 2 * a[i].v * l * r;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
}
printf("%.6f", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int main() {
cout.precision(20);
cout << fixed;
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
vector<double> pow(1000);
pow[0] = 1;
for (int i = 1; i < 1000; i++) {
pow[i] = pow[i - 1] * 2;
}
int n, k = 40;
cin >> n;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first;
v[i].first = -v[i].first;
v[i].second = i;
}
sort(v.begin(), v.end());
set<int> s;
double ans = 0;
vector<int> left(65);
vector<int> right(65);
int leftSize, rightSize;
for (int i = 0; i < n; i++) {
auto x = s.lower_bound(v[i].second);
leftSize = rightSize = 0;
left[leftSize++] = -1;
left[leftSize++] = v[i].second;
right[rightSize++] = n;
right[rightSize++] = v[i].second;
auto y = x;
for (int j = 0; y != s.begin() && j < k; j++) {
y--;
left[leftSize++] = *y;
}
y = x;
for (int j = 0; y != s.end() && j < k; j++) {
right[rightSize++] = *y;
y++;
}
sort(left.begin(), left.begin() + leftSize);
sort(right.begin(), right.begin() + rightSize);
for (int a = 0; a + 1 < leftSize; a++) {
for (int b = 0; b + 1 < rightSize; b++) {
int cnt = leftSize - 2 - a + b + 1;
long long leftLen = left[a + 1] - left[a];
long long rightLen = right[b + 1] - right[b];
ans -= v[i].first * (leftLen * rightLen) / pow[cnt];
}
}
s.insert(v[i].second);
}
cout << ans / n / n;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
const int MAXN = 300000 + 100;
const int MAXP = 50;
std::pair<int, int> lst[MAXN];
int b[MAXN];
int n;
double m2[MAXP];
struct node {
int sum;
double s1, s2;
void merge(node a, node b) {
sum = a.sum + b.sum;
s1 = b.s1;
if (b.sum < MAXP) s1 += a.s1 * m2[b.sum];
s2 = a.s2;
if (a.sum < MAXP) s2 += b.s2 * m2[a.sum];
}
} tr[MAXN * 4];
void build(int x, int s, int t) {
if (s == t) {
tr[x].sum = 0;
tr[x].s1 = 1;
tr[x].s2 = 1;
return;
}
int mid = (s + t) / 2;
build(x * 2, s, mid);
build(x * 2 + 1, mid + 1, t);
tr[x].merge(tr[x * 2], tr[x * 2 + 1]);
}
void add(int x, int s, int t, int ind) {
if (s == t) {
++tr[x].sum;
tr[x].s1 /= 2;
tr[x].s2 /= 2;
return;
}
int mid = (s + t) / 2;
if (ind <= mid)
add(x * 2, s, mid, ind);
else
add(x * 2 + 1, mid + 1, t, ind);
tr[x].merge(tr[x * 2], tr[x * 2 + 1]);
}
node get(int x, int s, int t, int st, int en) {
if (st <= s && t <= en) return tr[x];
int mid = (s + t) / 2;
if (en <= mid) return get(x * 2, s, mid, st, en);
if (mid < st) return get(x * 2 + 1, mid + 1, t, st, en);
node a = get(x * 2, s, mid, st, en);
node b = get(x * 2 + 1, mid + 1, t, st, en);
node c;
c.merge(a, b);
return c;
}
int main() {
m2[0] = 1;
for (int i = 1; i < MAXP; ++i) m2[i] = m2[i - 1] / 2;
std::cin >> n;
for (int i = 1; i <= n; ++i) {
std::cin >> b[i];
lst[i] = std::make_pair(b[i], i);
}
std::sort(lst + 1, lst + n + 1, std::greater<std::pair<int, int> >());
build(1, 1, n);
double ans = 0;
for (int i = 1; i <= n; ++i) {
node a = get(1, 1, n, 1, lst[i].second);
node b = get(1, 1, n, lst[i].second, n);
ans += lst[i].first * a.s1 * b.s2;
add(1, 1, n, lst[i].second);
}
ans /= n;
ans /= n;
ans /= 2;
std::cout << std::setprecision(9) << ans << std::endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1 << 19;
double pw2[8 * N];
struct mynum {
double v;
int sh;
mynum(double _v, int _sh) {
v = _v;
sh = _sh;
}
mynum() {}
mynum shift(int x) { return mynum(v, sh + x); }
friend mynum operator+(mynum a, mynum b) {
if (a.sh < b.sh) swap(a, b);
if (b.v == 0) return a;
if (a.v == 0) return b;
b.v *= pw2[b.sh - a.sh + 4 * N];
return mynum(a.v + b.v, a.sh);
}
};
struct tree {
int sh[2 * N];
mynum T[2 * N];
void push(int v) {
if (v >= N) {
T[v].sh += sh[v];
sh[v] = 0;
} else {
sh[2 * v] += sh[v];
sh[2 * v + 1] += sh[v];
T[v].sh += sh[v];
sh[v] = 0;
}
}
mynum get(int l, int r, int L = 1, int R = N, int v = 1) {
if (r < L || l > R)
return mynum(0, 0);
else if (l <= L && R <= r)
return T[v].shift(sh[v]);
else {
push(v);
return get(l, r, L, (L + R) / 2, 2 * v) +
get(l, r, (L + R) / 2 + 1, R, 2 * v + 1);
}
}
inline int getsh(int x) {
int L = 1, R = N, v = 1;
while (v < N) {
push(v);
if ((L + R) / 2 >= x)
v = 2 * v, R = (L + R) / 2;
else
v = 2 * v + 1, L = (L + R) / 2 + 1;
}
return sh[v] + T[v].sh;
}
void ch(int l, int r, int x, int L = 1, int R = N, int v = 1) {
if (r < L || l > R)
return;
else if (l <= L && R <= r)
sh[v] += x;
else {
push(v);
ch(l, r, x, L, (L + R) / 2, 2 * v);
ch(l, r, x, (L + R) / 2 + 1, R, 2 * v + 1);
T[v] = T[2 * v].shift(sh[2 * v]) + T[2 * v + 1].shift(sh[2 * v + 1]);
}
}
tree() {}
void init() {
for (int i = N; i < 2 * N; i++) sh[i] = 0, T[i] = mynum(1, 0);
for (int i = N - 1; i > 0; i--) sh[i] = 0, T[i] = T[2 * i] + T[2 * i + 1];
}
};
tree TL, TR;
int main() {
pw2[4 * N] = 1;
for (int i = 1; i < 4 * N - 10; i++)
pw2[4 * N + i] = pw2[4 * N + i - 1] * 2.0,
pw2[4 * N - i] = pw2[4 * N - i + 1] / 2.0;
int n;
scanf("%d", &n);
vector<pair<int, int> > V;
TL.init();
TR.init();
for (int i = 1; i <= n; i++) {
int t;
scanf("%d", &t);
V.push_back(make_pair(-t, i));
}
sort(V.begin(), V.end());
double ans = 0;
for (int i = 0; i < V.size(); i++) {
int v = -V[i].first;
int p = V[i].second;
mynum a = TL.get(1, p);
int sa = TL.getsh(p);
mynum b = TR.get(p, n);
int sb = TR.getsh(p);
a = a.shift(-sa);
b = b.shift(-sb);
assert(a.v == a.v);
assert(b.v == b.v);
assert(v == v);
int tp = a.sh + b.sh;
double res = v * a.v * b.v * pw2[4 * N + tp];
assert(pw2[4 * N + tp] == pw2[4 * N + tp]);
assert(res == res);
ans += res;
TL.ch(1, p, -1);
TR.ch(p, n, -1);
}
ans /= 2 * (double)n * (double)n;
printf("%.10lf\n", ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
long long powmod(long long a, long long b) {
long long res = 1;
a %= 1000000007;
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int a[310000], p[310000], l[310000], r[310000];
bool cmp(int i, int j) { return a[i] < a[j]; }
int main() {
int n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
p[i] = i;
l[i] = i - 1;
r[i] = i + 1;
}
sort(p + 1, p + 1 + n, cmp);
double ans = 0;
for (int i = 1; i <= n; i++) {
double L = 0, R = 0;
int x = p[i];
double y = 1;
int ll = x, rr = x;
for (int _ = 0; _ < 50; _++) {
if (ll) L += (ll - l[ll]) * y, ll = l[ll];
if (rr <= n) R += (r[rr] - rr) * y, rr = r[rr];
y /= 2.0;
}
r[l[x]] = r[x];
l[r[x]] = l[x];
ans += L * R * a[x] / 2.0;
}
printf("%.10lf", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int b[300010], p[300010], l[300010], r[300010];
bool cmp(int i, int j) { return b[i] < b[j]; }
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
p[i] = i;
l[i] = i - 1;
r[i] = i + 1;
}
sort(p + 1, p + n + 1, cmp);
double ans = 0;
for (int k = 1; k <= n; k++) {
int i = p[k], x = i, y = i;
double z = 1, L = 0, R = 0;
for (int j = 1; j <= 45; j++) {
if (x) {
L += (x - l[x]) * z;
x = l[x];
}
if (y <= n) {
R += (r[y] - y) * z;
y = r[y];
}
z /= 2;
}
l[r[i]] = l[i];
r[l[i]] = r[i];
ans += L * R * b[i] / 2;
}
printf("%.6lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
const int LG = 20;
const int M = 30;
int mx[N][LG];
int a[N];
int n;
int pre[M], suf[M];
int prv(int v, int val) {
for (int i = LG - 1; i >= 0; i--)
if (v - (1 << i) >= 0 && mx[v - (1 << i)][i] < val) v -= (1 << i);
return v - 1;
}
int nxt(int v, int val) {
v++;
for (int i = LG - 1; i >= 0; i--)
if (v + (1 << i) <= n && mx[v][i] <= val) v += (1 << i);
return v;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
mx[i][0] = a[i];
}
for (int j = 1; j < LG; j++)
for (int i = 0; i < n; i++)
if (i + (1 << (j - 1)) < n)
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
else
mx[i][j] = mx[i][j - 1];
double ans = 0;
for (int i = 0; i < n; i++) {
pre[0] = suf[0] = i;
for (int j = 1; j < M; j++) {
pre[j] = pre[j - 1] == -1 ? -1 : prv(pre[j - 1], a[i]);
suf[j] = suf[j - 1] == n ? n : nxt(suf[j - 1], a[i]);
}
double pp = 0, ps = 0;
for (int j = 1; j < M; j++) pp += 1.0 * (pre[j - 1] - pre[j]) / (1LL << j);
for (int k = 1; k < M; k++) ps += 1.0 * (suf[k] - suf[k - 1]) / (1LL << k);
ans += 1.0 * pp * ps * 2 * a[i] / n / n;
}
cout << setprecision(15) << fixed << ans << "\n";
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct node {
int s, i;
} a[300100];
int l[300100], r[300100], n, i, j;
double ans;
int gi() {
int s = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s;
}
bool operator<(node a, node b) { return a.s < b.s; }
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) a[i].s = gi(), a[i].i = i;
for (i = 1; i <= n; i++) l[i] = i - 1, r[i] = i + 1;
r[n + 1] = n + 1;
sort(a + 1, a + n + 1);
for (i = 1; i <= n; i++) {
int xl = a[i].i, xr = xl;
double now = 1.0, lf = 0, rt = 0;
for (j = 1; j <= 45; j++) {
lf += (xl - l[xl]) / now, rt += (r[xr] - xr) / now;
xl = l[xl], xr = r[xr], now *= 2.0;
}
ans += a[i].s * lf * rt / 2.0;
l[r[a[i].i]] = l[a[i].i];
r[l[a[i].i]] = r[a[i].i];
}
ans /= 1.0 * n * n;
printf("%.8lf", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int ab;
int b[300000];
vector<int> ld[300000], rd[300000];
int main() {
ios_base::sync_with_stdio(false);
cin >> ab;
for (int i = 0; i < ab; i++) {
cin >> b[i];
b[i] = max(b[i], 0);
rd[i].push_back(i);
ld[i].push_back(i);
}
set<pair<int, int> > s;
for (int i = 0; i < ab; i++) {
set<pair<int, int> >::iterator it = s.upper_bound(make_pair(b[i], i));
vector<pair<int, int> > tmp;
for (set<pair<int, int> >::iterator j = s.begin(); j != it; j++) {
rd[j->second].push_back(i);
if (rd[j->second].size() >= 38) tmp.push_back(*j);
}
for (int j = 0; j < tmp.size(); j++) s.erase(tmp[j]);
s.insert(make_pair(b[i], i));
}
s.clear();
for (int i = ab - 1; i >= 0; i--) {
set<pair<int, int> >::iterator it = s.upper_bound(make_pair(b[i], i));
vector<pair<int, int> > tmp;
for (set<pair<int, int> >::iterator j = s.begin(); j != it; j++) {
ld[j->second].push_back(i);
if (ld[j->second].size() >= 38) tmp.push_back(*j);
}
for (int j = 0; j < tmp.size(); j++) s.erase(tmp[j]);
s.insert(make_pair(b[i], i));
}
for (int i = 0; i < ab; i++) {
ld[i].push_back(-1);
rd[i].push_back(ab);
}
long double c_ans = 0;
for (int i = 0; i < ab; i++) {
long double c_accum = 0;
for (int j = 0; j <= 38; j++) {
long long accum = 0;
for (int k = 0; k <= j; k++) {
if (ld[i].size() <= k + 1 || rd[i].size() <= j - k + 1) continue;
long long lsz = ld[i][k] - ld[i][k + 1],
rsz = rd[i][(j - k) + 1] - rd[i][j - k];
;
accum += (lsz * rsz);
}
c_accum += accum / ((long double)(1LL << (j + 1)));
}
c_ans += (c_accum)*b[i];
}
cout.precision(60);
cout << c_ans / (((long long)ab) * ab);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 300010;
const int TINY = 1e-13;
int N;
double b[MAXN];
pair<double, int> first[MAXN];
const int MAXS = 25;
int Ls, Rs;
int L[MAXS], R[MAXS];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(12) << fixed;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> b[i];
b[i] += i * TINY;
}
for (int i = 0; i < N; i++) first[i] = make_pair(b[i], i);
sort(first, first + N);
multiset<int> greater;
double ans = 0;
for (int i = N - 1; i >= 0; i--) {
Ls = Rs = 0;
multiset<int>::iterator it = greater.lower_bound(first[i].second);
while (it != greater.begin() && Ls < MAXS) {
it--;
L[Ls++] = *it;
}
if (Ls < MAXS) L[Ls++] = -1;
for (it = greater.upper_bound(first[i].second);
it != greater.end() && Rs < MAXS; it++)
R[Rs++] = *it;
if (Rs < MAXS) R[Rs++] = N;
int prevl = first[i].second;
double coef = 0.5;
double sa = 0;
for (int j = 0; j < Ls; j++) {
sa += coef * double(prevl - L[j]);
prevl = L[j];
coef *= 0.5;
}
int prevr = first[i].second;
coef = 1;
for (int j = 0; j < Rs; j++) {
ans += first[i].first * coef * double(R[j] - prevr) * sa;
prevr = R[j];
coef *= 0.5;
}
greater.insert(first[i].second);
}
cout << ans / (N * double(N)) << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 400000;
struct T {
int a;
int b;
} V[N];
inline bool cmp(T a, T b) { return a.a < b.a; }
set<int> S;
double p2[41];
int n;
int main() {
p2[0] = 1;
for (int i = (1); i <= (40); i++) p2[i] = p2[i - 1] * 2;
scanf("%d", &n);
for (int i = (1); i <= (n); i++) {
scanf("%d", &V[i].a);
V[i].b = i;
}
sort(V + 1, V + 1 + n, cmp);
S.insert(0);
S.insert(n + 1);
double ans = 0;
for (int i = (n); i >= (1); i--) {
S.insert(V[i].b);
set<int>::iterator p = S.find(V[i].b);
set<int>::iterator p1 = p, p0 = p;
double sum1 = 0, sum2 = 0;
for (int j = (1); j <= (40); j++) {
p0 = p1;
p1++;
if (p1 == S.end()) break;
sum1 += (*p1 - *p0) / p2[j];
}
p1 = p;
p0 = p;
for (int j = (1); j <= (40); j++) {
p0 = p1;
p1--;
sum2 += (*p0 - *p1) / p2[j];
if (p1 == S.begin()) break;
}
ans += sum1 * sum2 * 2 * V[i].a;
}
printf("%.12f", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int n, m, ans;
int a[300005], l[300005], r[300005], fuck[300005], pre[300005];
double p;
bool flag;
struct fuck {
int id, val;
bool operator<(const fuck &ls) const {
return val > ls.val || (val == ls.val && id < ls.id);
}
} v[300005];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x * f;
}
inline void print(int x) {
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
int main() {
n = read();
for (int i = 1; i <= n; i++) v[i].val = a[i] = read(), v[i].id = i;
sort(v + 1, v + n + 1);
a[0] = a[n + 1] = inf;
l[1] = 0;
for (int i = 2; i <= n; i++) {
if (a[i] <= a[i - 1])
l[i] = i - 1;
else {
int x = l[i - 1];
for (; a[x] < a[i]; x = l[x])
;
l[i] = x;
}
}
r[n] = n + 1;
for (int i = n - 1; i; i--) {
if (a[i] < a[i + 1])
r[i] = i + 1;
else {
int x = r[i + 1];
for (; a[x] <= a[i]; x = r[x])
;
r[i] = x;
}
}
for (int i = 1; i <= n; i++) fuck[i] = n + 1, pre[i] = 0;
for (int i = 1; i <= n; i++) {
int x = v[i].id, cnt = 0;
double tmp1 = 0, tmp2 = 0, fuckls = 1;
if (r[x] != n + 1) pre[r[x]] = x;
if (l[x] != 0) fuck[l[x]] = x;
pre[x] = l[x], fuck[x] = r[x];
while (1) {
tmp1 += fuckls * (x - pre[x]), fuckls /= 2, x = pre[x];
if (!x || ++cnt >= 40) break;
}
x = v[i].id, cnt = 0, fuckls = 1;
while (1) {
tmp2 += fuckls * (fuck[x] - x), fuckls /= 2, x = fuck[x];
if (x == n + 1 || ++cnt >= 40) break;
}
p += tmp1 * tmp2 * a[v[i].id] / 2;
}
printf("%.8lf\n", p / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int x[110], y[110];
vector<pair<int, int> > a;
set<int> S;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int ret;
cin >> ret;
a.push_back(make_pair(-ret, i));
}
sort(a.begin(), a.end());
double res = 0;
for (int i = 0; i < n; i++) {
int pos = a[i].second;
S.insert(pos);
set<int>::iterator it;
int nl = 0, nr = 0;
x[nl++] = pos;
y[nr++] = pos;
it = S.find(pos);
for (int j = 0; j < 50; j++)
if (it != S.begin())
x[nl++] = *(--it);
else
break;
it = S.find(pos);
for (int j = 0; j < 50; j++)
if (++it != S.end())
y[nr++] = *it;
else
break;
x[nl++] = -1;
y[nr++] = n;
int w = -a[i].first;
double sum0 = 0, sum1 = 0;
for (int j = 0; j < nl - 1; j++)
sum0 += (double)(x[j] - x[j + 1]) / (1LL << (j + 1));
for (int j = 0; j < nr - 1; j++)
sum1 += (double)(y[j + 1] - y[j]) / (1LL << j);
res += (sum0 * sum1) * w;
}
res /= (long long)n * n;
cout << fixed << setprecision(10) << res << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
int a[300300], id[300300];
set<int> s;
bool cmp(int i, int j) { return a[i] != a[j] ? a[i] > a[j] : i > j; }
double calc(int p) {
s.insert(p);
double A = 0, B = 0;
set<int>::iterator pre, nxt;
pre = nxt = s.find(p);
for (int i = 0; pre != s.begin() && i < 50; i++) {
pre--;
A += (double)(*nxt - *pre) / (1LL << i);
nxt--;
}
pre = nxt = s.find(p);
nxt++;
for (int i = 0; nxt != s.end() && i < 50; i++) {
B += (double)(*nxt - *pre) / (1LL << i);
pre++;
nxt++;
}
return A * B / 2;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
id[i] = i;
}
sort(id, id + n, cmp);
double ans = 0;
s.insert(-1);
s.insert(n);
for (int i = 0; i < n; i++) {
ans += calc(id[i]) * a[id[i]];
}
printf("%.12lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double eps = 1e-9;
const int maxn = 3e5 + 10;
const int maxT = 100;
int a[maxn], nex[maxn], pre[maxn], rk[maxn];
bool cmp(const int &lhs, const int &rhs) {
return a[lhs] == a[rhs] ? lhs < rhs : a[lhs] < a[rhs];
}
inline int read() {
int x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') flag = -1, ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * flag;
}
int main() {
int i, j, k, m, n;
n = read();
for (i = 1; i <= n; i++) a[i] = read();
for (i = 1; i <= n; i++) rk[i] = i;
sort(rk + 1, rk + n + 1, cmp);
for (i = 1; i <= n; i++) nex[i] = i + 1, pre[i] = i - 1;
double ans = 0;
for (i = 1; i <= n; i++) {
int pos = rk[i], l = pos, r = pos;
double sum1 = 0, sum2 = 0, w = 1;
for (j = 1; j <= maxT; j++) {
if (l >= 1) sum1 += w * (l - pre[l]), l = pre[l];
if (r <= n) sum2 += w * (nex[r] - r), r = nex[r];
w *= 0.5;
}
ans += 0.5 * a[pos] * sum1 * sum2;
nex[pre[pos]] = nex[pos];
pre[nex[pos]] = pre[pos];
}
printf("%.10lf\n", ans / (1ll * n * n));
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300300;
const int MAX = 70;
int b[MAXN];
int main(void) {
int n;
scanf("%d", &n);
for (int i = 0; i < (n); ++i) scanf("%d", b + i);
static pair<int, int> a[MAXN];
for (int i = 0; i < (n); ++i) a[i] = {b[i], i};
sort(a, a + n, greater<pair<int, int> >());
static set<int> S;
double ans = 0;
for (int i = 0; i < (n); ++i) {
int x = a[i].first, pos = a[i].second;
auto it = S.lower_bound(pos);
static int l[MAX];
int nl = 0;
auto itl = it;
while (nl < MAX && itl != S.begin()) l[nl++] = *(--itl);
int last = pos;
for (int i = 0; i < (nl); ++i) l[i] = last - l[i], last -= l[i];
if (nl < MAX) l[nl++] = last - 0 + 1;
static int r[MAX];
int nr = 0;
auto itr = it;
while (nr < MAX && itr != S.end()) r[nr++] = *(itr++);
last = pos;
for (int i = 0; i < (nr); ++i) r[i] = r[i] - last, last += r[i];
if (nr < MAX) r[nr++] = n - last;
double suml = 0, p2 = 1;
for (int i = 0; i < (nl); ++i) suml += l[i] / p2, p2 *= 2;
double sumr = 0;
p2 = 1;
for (int i = 0; i < (nr); ++i) sumr += r[i] / p2, p2 *= 2;
ans += suml * sumr * x / 2.0 / n;
S.insert(pos);
}
ans /= n;
printf("%lf\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int cmp(const pair<int, int> &a, const pair<int, int> &b) {
if (a.first != b.first)
return a.first > b.first;
else
return a.second < b.second;
}
double sv[32 + 10], lenl[32 + 10], lenr[32 + 10], lv[32 + 10], rv[32 + 10];
int a[300010];
pair<int, int> d[300010];
void lemon() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
for (int i = (1); i <= (n); i++) d[i] = make_pair(a[i], i);
sort(d + 1, d + n + 1, cmp);
set<int> s;
double final = 0;
for (int i = (1); i <= (n); i++) {
sv[0] = d[i].first;
for (int k = (1); k <= (32 + 1); k++) sv[k] = sv[k - 1] / 2;
pair<set<int>::iterator, bool> rrr = s.insert(d[i].second);
set<int>::iterator it = rrr.first;
set<int>::iterator it2 = it;
int ra = 0, la = 0;
rv[0] = d[i].second;
lv[0] = d[i].second;
for (int k = (1); k <= (32); k++) {
it++;
if (it == s.end()) {
ra++;
rv[ra] = n + 1;
break;
}
ra++;
rv[ra] = (*it);
}
it = it2;
for (int k = (1); k <= (32); k++) {
if (it == s.begin()) {
la++;
lv[la] = 0;
break;
}
it--;
la++;
lv[la] = (*it);
}
for (int k = (1); k <= (ra); k++) lenr[k - 1] = rv[k] - rv[k - 1];
for (int k = (1); k <= (la); k++) lenl[k - 1] = lv[k - 1] - lv[k];
for (int kl = (0); kl <= (la - 1); kl++)
for (int kr = (0); kr <= (min(32 - kl - 1, ra - 1)); kr++)
final += sv[kl + kr + 1] * lenl[kl] * lenr[kr];
}
final /= n;
final /= n;
printf("%.16lf\n", final);
}
int main() {
ios::sync_with_stdio(true);
lemon();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Tedge {
int to, next;
} edge[3333333];
int now, first[333333], data[333333];
multiset<int> Q;
int Rand() { return (rand() << 15) | rand(); }
void Addedge(int x, int y) {
edge[++now].to = y;
edge[now].next = first[x];
first[x] = now;
return;
}
int main() {
int n, i, j, pointer, pos, best;
long double answer, tl, tr, times;
multiset<int>::iterator it;
srand((unsigned)time(0));
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &data[i]);
Addedge(data[i], i);
}
Q.insert(0);
Q.insert(n + 1);
answer = 0;
for (i = 100000; i >= 1; i--) {
for (pointer = first[i]; pointer; pointer = edge[pointer].next) {
pos = edge[pointer].to;
best = pos;
it = Q.lower_bound(best);
it--;
tl = 0;
times = 1;
for (j = 1; j <= 40 && *it != 0; j++) {
tl += times * (best - *it);
best = *it;
it--;
times *= 0.5;
}
tl += times * best;
best = pos;
it = Q.lower_bound(best);
tr = 0;
times = 1;
for (j = 1; j <= 40 && *it != n + 1; j++) {
tr += times * (*it - best);
best = *it;
it++;
times *= 0.5;
}
tr += times * (n + 1 - best);
answer += tl * tr * i;
Q.insert(pos);
pos = edge[pointer].to;
best = pos;
it = Q.upper_bound(best);
it--;
tl = 0;
times = 1;
for (j = 1; j <= 40 && *it != 0; j++) {
tl += times * (best - *it);
best = *it;
it--;
times *= 0.5;
}
tl += times * best;
best = pos;
it = Q.lower_bound(best);
tr = 0;
times = 1;
for (j = 1; j <= 40 && *it != n + 1; j++) {
tr += times * (*it - best);
best = *it;
it++;
times *= 0.5;
}
tr += times * (n + 1 - best);
answer -= 2 * tl * tr * i;
}
}
answer /= n;
answer /= n;
cout << fixed << setprecision(10) << answer << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MaxN = 300000;
int main() {
int n;
static int a[MaxN + 1];
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
static pair<int, int> ap[MaxN];
for (int i = 1; i <= n; i++) ap[i - 1] = pair<int, int>(a[i], i);
sort(ap, ap + n);
static int prev[1 + MaxN + 1];
static int next[1 + MaxN + 1];
for (int i = 0; i <= n + 1; i++) prev[i] = i - 1;
for (int i = 0; i <= n + 1; i++) next[i] = i + 1;
const int L = 40;
static double pre[L + 1];
pre[0] = 1;
for (int i = 1; i <= L; i++) pre[i] = pre[i - 1] / 2;
double res = 0.0;
for (int i = 0; i < n; i++) {
int idx = ap[i].second;
double val = ap[i].first;
double orzL = 0.0;
double orzR = 0.0;
for (int v = idx, j = 1; v != 0 && j <= L; v = prev[v], j++)
orzL += (double)(v - prev[v]) / n * pre[j];
for (int u = idx, k = 1; u != n + 1 && k <= L; u = next[u], k++)
orzR += (double)(next[u] - u) / n * pre[k - 1];
res += val * orzL * orzR;
next[prev[idx]] = next[idx];
prev[next[idx]] = prev[idx];
}
cout << fixed << setprecision(10) << res << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
typedef double db[1200010];
typedef int arr32[300010];
arr32 x, g;
db sz, ur, val1, val2;
double p, q, ura;
int n;
void update(int k, int l, int r) {
sz[k] = sz[(k << 1)] * sz[(k << 1) + 1];
val1[k] = val1[(k << 1)] * sz[(k << 1) + 1] + val1[(k << 1) + 1];
val2[k] = val2[(k << 1) + 1] * sz[(k << 1)] + val2[(k << 1)];
ur[k] = ur[(k << 1)] + ur[(k << 1) + 1] * sz[(k << 1)];
}
void inser(int k, int l, int r, int x) {
if (l == r) {
sz[k] = ur[k] = val1[k] = val2[k] = 0.5;
return;
}
if (x <= ((l + r) >> 1))
inser((k << 1), l, ((l + r) >> 1), x);
else
inser((k << 1) + 1, ((l + r) >> 1) + 1, r, x);
update(k, l, r);
}
void findL(int k, int l, int r, int x) {
if (l > x) return;
if (r <= x) {
p *= sz[k];
p += val1[k];
ura *= sz[k], ura += ur[k];
return;
}
findL((k << 1), l, ((l + r) >> 1), x),
findL((k << 1) + 1, ((l + r) >> 1) + 1, r, x);
}
void findR(int k, int l, int r, int x) {
if (r < x) return;
if (l >= x) {
q *= sz[k];
q += val2[k];
ura *= sz[k], ura += ur[k];
return;
}
findR((k << 1) + 1, ((l + r) >> 1) + 1, r, x),
findR((k << 1), l, ((l + r) >> 1), x);
}
void build(int k, int l, int r) {
if (l == r) {
sz[k] = ur[k] = val1[k] = val2[k] = 1;
return;
}
build((k << 1), l, ((l + r) >> 1)),
build((k << 1) + 1, ((l + r) >> 1) + 1, r);
update(k, l, r);
}
bool cmp(const int &a, const int &b) { return x[a] > x[b]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", x + i), g[i] = i;
build(1, 1, n);
double result = 0;
sort(g + 1, g + n + 1, cmp);
for (int i = 1; i <= n; ++i) {
int z = g[i];
p = 0, ura = 1, findL(1, 1, n, z);
q = 0, ura = 1, findR(1, 1, n, z);
result += p * q * x[z] / n / 2;
inser(1, 1, n, z);
}
printf("%.10lf", result / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
set<int> S;
double r[44], ans;
int n;
pair<int, int> a[333333];
int p[44], q[44];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].first), a[i].second = i;
sort(a + 1, a + 1 + n);
S.insert(0), S.insert(n + 1);
r[0] = 1;
for (int i = 1; i <= 40; ++i) r[i] = r[i - 1] * 2;
for (int z = n; z >= 1; --z) {
int i = a[z].second, v = a[z].first;
S.insert(i);
set<int>::iterator c = S.find(i);
p[0] = q[0] = i;
double L = 0, R = 0;
for (int j = 1; j <= 40; ++j) {
if (c == S.begin()) break;
c--;
L += double(p[j - 1] - (p[j] = *c)) / r[j];
}
c = S.find(i);
for (int j = 1; j <= 40; ++j) {
c++;
if (c == S.end()) break;
R += double((q[j] = *c) - q[j - 1]) / r[j - 1];
}
ans += L * R * v;
}
printf("%.9f\n", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, r, LL[300005][28], RR[300005][28];
long long PP[28];
int A[300005];
pair<int, int> P[300005];
set<int> H;
set<int>::iterator it;
int L[300005][28], R[300005][28];
double res;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int i;
for (i = 1; i <= n; i++) {
cin >> A[i];
P[i].first = A[i];
P[i].second = i;
}
sort(P + 1, P + n + 1);
int N = 0;
for (i = n; i >= 1; i--) {
it = H.lower_bound(P[i].second);
N = 0;
while (it != H.end() && N <= 26) {
R[P[i].second][++N] = *it;
it++;
}
R[P[i].second][0] = N;
it = H.lower_bound(P[i].second);
N = 0;
if (it != H.begin()) {
it--;
while (it != H.begin() && N <= 26) {
L[P[i].second][++N] = *it;
it--;
}
if (it == H.begin() && N <= 26) L[P[i].second][++N] = *it;
}
L[P[i].second][0] = N;
H.insert(P[i].second);
}
PP[0] = 1;
for (i = 1; i <= 26 + 1; i++) PP[i] = PP[i - 1] * 2;
int j, k, l1, r1, l2, r2;
for (i = 1; i <= n; i++) {
for (j = 0; j <= L[i][0] && j <= 26; j++) {
l1 = (j == 0) ? i : L[i][j];
l2 = (j < L[i][0]) ? L[i][j + 1] : 0;
LL[i][j] = l1 - l2 - 1;
}
for (k = 0; k <= R[i][0] && k <= 26; k++) {
r1 = (k == 0) ? i : R[i][k];
r2 = (k < R[i][0]) ? R[i][k + 1] : n + 1;
RR[i][k] = r2 - r1 - 1;
}
}
long long no[28];
for (i = 1; i <= n; i++) {
memset(no, 0, sizeof(no));
for (j = 0; j <= L[i][0] && j <= 26; j++)
for (k = 0; k <= R[i][0] && k <= 26 - j; k++)
no[j + k] += (long long)LL[i][j] * RR[i][k] + LL[i][j] + RR[i][k] + 1;
for (j = 0; j <= 26; j++) res += (double)A[i] * no[j] / PP[j + 1];
}
printf("%.10lf\n", (res / n) / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int K = 30;
const int N = 300000;
int n, pre[N + 10], nxt[N + 10];
set<int> s;
struct data {
int va, loc;
} a[N + 10];
double ans = 0;
bool cmp(data p, data q) { return p.va > q.va; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].va);
a[i].loc = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
set<int>::iterator nowloc = s.lower_bound(a[i].loc);
if (nowloc == s.end())
nxt[a[i].loc] = n + 1;
else
nxt[a[i].loc] = *nowloc;
if (nowloc == s.begin())
pre[a[i].loc] = 0;
else {
nowloc--;
pre[a[i].loc] = *nowloc;
}
if (nxt[a[i].loc] != -1) pre[nxt[a[i].loc]] = a[i].loc;
if (pre[a[i].loc] != -1) nxt[pre[a[i].loc]] = a[i].loc;
double nowl = 0, px = 1.0;
for (int now = a[i].loc, j = 0; j <= K && now != 0;
j++, now = pre[now], px /= 2)
nowl += px * (now - pre[now]);
double nowr = 0;
px = 1.0;
for (int now = a[i].loc, j = 0; j <= K && now != n + 1;
j++, now = nxt[now], px /= 2)
nowr += px * (nxt[now] - now);
ans += nowl * nowr / n / n * a[i].va;
s.insert(a[i].loc);
}
printf("%.10lf\n", ans / 2);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 3;
const long long M = 43;
const long long SQ = 320;
const long long INF = 1e16;
const long long MOD = 1e9 + 7;
int n;
pair<int, int> a[N];
set<int> st;
double v[N][M], v1[N][M];
int c[N], c1[N];
double t[50];
int main() {
t[0] = 1;
for (int i = 1; i <= 42; i++) t[i] = t[i - 1] / 2;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].first);
a[i].second = i;
}
sort(a, a + n, greater<pair<int, int> >());
for (int i = 0; i < n; i++) {
st.insert(a[i].second);
auto p1 = st.lower_bound(a[i].second);
auto p2 = p1;
for (int j = 0; (p1 != st.begin() && j < 40); p1--, j++) v[i][c[i]++] = *p1;
v[i][c[i]++] = *p1;
v[i][c[i]++] = -1;
for (int j = 0; (p2 != st.end() && j < 40); p2++, j++) v1[i][c1[i]++] = *p2;
v1[i][c1[i]++] = n;
}
double ans = 0;
for (int i = 0; i < n; i++) {
double r = 0;
for (int j = 1; j < c1[i]; j++) r += t[j - 1] * (v1[i][j] - v1[i][j - 1]);
double res = 0;
for (int j = 1; j < c[i]; j++) res += t[j] * (v[i][j - 1] - v[i][j]) * r;
res = res * a[i].first;
ans += res;
}
cout << setprecision(10) << ans / double(n) / double(n) << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int v[700005], id[700005], L[700005], R[700005], n;
double Ans;
bool cmp(const int &i, const int &j) { return v[i] < v[j]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) L[i] = i - 1, R[i] = i + 1;
for (int i = 1; i <= n; i++) scanf("%d", &v[i]), id[i] = i;
sort(id + 1, id + 1 + n, cmp);
for (int _ = 1; _ <= n; _++) {
int i = id[_];
int l = i, r = i;
double Sl = 0, Sr = 0;
for (int j = 0; j <= 60; j++) {
if (l == 0) break;
Sl += 1. / (1LL << j) * (l - L[l]);
l = L[l];
}
for (int j = 1; j <= 60; j++) {
if (r == n + 1) break;
Sr += 1. / (1LL << j) * (R[r] - r);
r = R[r];
}
L[R[i]] = L[i];
R[L[i]] = R[i];
Ans += Sl * Sr * v[i];
}
printf("%.8lf", Ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXL = 58;
int fw[MAXL + 5], bw[MAXL + 5];
double p2[MAXL * 2 + 5];
pair<int, int> A[300005];
set<int> S;
int main() {
p2[0] = 1;
for (int i = 1; i < MAXL * 2 + 5; ++i) p2[i] = p2[i - 1] / 2;
double ans = 0;
int N;
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%d", &A[i].first);
A[i].second = i;
}
sort(A + 1, A + 1 + N);
S.insert(0);
S.insert(N + 1);
for (int i = N; i >= 1; --i) {
int p = A[i].second;
auto start = S.insert(p).first;
auto it = start;
double fsum = 0, bsum = 0;
for (int k = 0; it != S.end() && k < MAXL; ++k, ++it) {
fw[k] = *it;
if (k) fsum += (fw[k] - fw[k - 1]) * p2[k];
}
it = start;
for (int k = 0; k < MAXL; ++k, --it) {
bw[k] = *it;
if (k) bsum += (bw[k - 1] - bw[k]) * p2[k];
if (!*it) break;
}
ans += A[i].first * fsum * bsum * 2 / N / N;
}
printf("%.9lf\n", ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N, K, K1, K2, nr, a[300009], lft[309], rgt[309];
double ans, put[209];
pair<int, int> v[300009];
set<int> S;
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%d", &a[i]), v[i] = make_pair(-a[i], i);
put[0] = 1.0;
for (int i = 1; i <= 102; i++) put[i] = put[i - 1] / 2.0;
sort(v + 1, v + N + 1), K = 43;
for (int I = 1; I <= N; I++) {
int i = v[I].second;
S.insert(i);
set<int>::iterator it = S.find(i), it1, it2;
it1 = it, it2 = it, K1 = K2 = K, lft[0] = rgt[0] = i;
for (int j = 1; j <= K; j++) {
if (it1 == S.begin()) {
K1 = j - 1;
break;
}
it1--, lft[j] = *it1;
}
for (int j = 1; j <= K; j++) {
if (++it2 == S.end()) {
K2 = j - 1;
break;
}
rgt[j] = *it2;
}
lft[K1 + 1] = 0, rgt[K2 + 1] = N + 1;
for (int j1 = 0; j1 <= K1; j1++)
for (int j2 = 0; j2 <= K2; j2++)
ans = (double)ans + (double)a[i] * put[j1 + j2 + 1] *
(rgt[j2 + 1] - rgt[j2]) *
(lft[j1] - lft[j1 + 1]);
}
ans = (double)ans / N;
ans = (double)ans / N;
printf("%.15f\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[300005], pre[300005], nxt[300005], c[300005], p0[100], p1[100];
bool cmp(const int &u, const int &v) { return a[u] < a[v]; }
double ans = 0, pw[100];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), pre[i] = i - 1, nxt[i] = i + 1, c[i] = i;
nxt[n + 1] = n + 1, pre[n + 1] = n, nxt[0] = 1;
sort(c + 1, c + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
int u = c[i], l = 0;
p0[0] = p1[0] = u;
pw[0] = 0.5 * a[u] / n / n;
for (; pw[l] > 1e-18;) ++l, pw[l] = pw[l - 1] * 0.5;
for (int j = 1; j <= l; j++) p0[j] = pre[p0[j - 1]], p1[j] = nxt[p1[j - 1]];
for (int j = 0; j < l; j++)
for (int k = 0; k < l - j; k++)
ans += pw[j + k] * (p1[k + 1] - p1[k]) * (p0[j] - p0[j + 1]);
nxt[pre[u]] = nxt[u], pre[nxt[u]] = pre[u];
}
printf("%.6lf\n", ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 333333;
int a[N];
int pos[N];
set<int> s;
int n;
bool cmp(const int pos1, const int pos2) {
if (a[pos1] != a[pos2]) return a[pos1] > a[pos2];
return pos1 < pos2;
}
double calc(int pos) {
set<int>::iterator sit;
int prev;
double c1 = 0, c2 = 0, two;
two = 1;
sit = s.lower_bound(pos);
sit--;
for (int i = 0, prev = pos; i < 60; i++) {
c1 += two * (prev - *sit);
two /= 2.0;
prev = *sit;
if (*sit == 0) break;
sit--;
}
two = 1;
sit = s.lower_bound(pos);
for (int i = 0, prev = pos; i < 60; i++) {
c2 += two * (*sit - prev);
two /= 2.0;
prev = *sit;
if (*sit == n + 1) break;
sit++;
}
s.insert(pos);
return c1 * c2 / 2.0;
}
void solve() {
sort(pos + 1, pos + n + 1, cmp);
s.insert(0);
s.insert(n + 1);
double ans = 0;
for (int i = 1; i <= n; i++) {
ans += calc(pos[i]) * a[pos[i]];
}
ans = ans / n / n;
printf("%.10f\n", ans);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pos[i] = i;
}
solve();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
struct node {
int v, id;
inline bool operator<(const node &o) const {
return v < o.v || (v == o.v && id < o.id);
}
} p[MAXN];
int n, pre[MAXN], nxt[MAXN];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &p[i].v), p[i].id = i, pre[i] = i - 1, nxt[i] = i + 1;
sort(p + 1, p + n + 1);
double ans = 0;
for (int i = 1; i <= n; ++i) {
double l = 0, r = 0, pw = 1;
int x = p[i].id, lp = x, rp = x;
for (int j = 1; j <= 50; ++j) {
pw /= 2;
if (lp) l += pw * (lp - pre[lp]), lp = pre[lp];
if (rp <= n) r += pw * (nxt[rp] - rp), rp = nxt[rp];
}
ans += 2 * l * r * p[i].v;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
}
printf("%.15f\n", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int Get() {
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
int res = 0;
for (; ch >= '0' && ch <= '9'; ch = getchar()) res = res * 10 + ch - '0';
return res;
}
const int N = (int)1e6;
typedef int arr[N + 10];
int n, m, cnt;
arr a, ord, nex, par;
bool cmp(const int &x, const int &y) { return a[x] < a[y]; }
int main() {
n = Get();
for (int i = 1; i <= n; ++i)
a[i] = Get(), ord[i] = i, par[i] = i - 1, nex[i] = i + 1;
nex[0] = 1, par[n + 1] = n;
par[0] = nex[n + 1] = N + 5;
par[N + 5] = nex[N + 5] = N + 5;
sort(ord + 1, ord + n + 1, cmp);
double ans = 0;
for (int i = 1; i <= n; ++i) {
int x = ord[i];
nex[par[x]] = nex[x], par[nex[x]] = par[x];
int l = par[x], r = nex[x], tl = x, tr = x;
double fac = 2, ca = 0, cb = 0;
for (int k = 1; k <= 70; ++k, fac *= 2) {
if (l == N + 5 && r == N + 5) break;
if (l != N + 5) ca += (double)(tl - l) / fac;
if (r != N + 5) cb += (double)(r - tr) / fac;
tl = l, tr = r, l = par[l], r = nex[r];
}
ans += ca * cb * (double)a[x] * 2;
}
ans = ans / (double)n / (double)n;
printf("%.9lf\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
int b[300000];
int indice[300000];
bool compara(int i1, int i2) { return b[i1] < b[i2]; }
const int primero = 1 << 19;
const int tope = 1 << 20;
int cuantos[tope];
double sumari[tope], sumale[tope];
double expo[300000];
void inserta(int pos, int val) {
pos += primero;
if (val == 0) {
cuantos[pos] = 0;
sumale[pos] = sumari[pos] = 1.0;
} else {
cuantos[pos] = 1;
sumale[pos] = sumari[pos] = 1 / 2.0;
}
while (pos > 1) {
pos /= 2;
cuantos[pos] = cuantos[2 * pos] + cuantos[2 * pos + 1];
sumale[pos] =
sumale[2 * pos + 1] + sumale[2 * pos] * expo[cuantos[2 * pos + 1]];
sumari[pos] =
sumari[2 * pos] + sumari[2 * pos + 1] * expo[cuantos[2 * pos]];
}
}
double acumri(int pos) {
pos += primero;
double s = 0;
int c = cuantos[pos];
while (pos > 1) {
if (pos % 2 == 0) {
s += expo[c] * sumari[pos + 1];
c += cuantos[pos + 1];
}
pos /= 2;
}
return s;
}
double acumle(int pos) {
pos += primero;
double s = 0;
int c = cuantos[pos];
while (pos > 1) {
if (pos % 2 == 1) {
s += expo[c] * sumale[pos - 1];
c += cuantos[pos - 1];
}
pos /= 2;
}
return s;
}
double acum(int pos) {
return (1.0 + acumle(pos) * 2.0) * (1 / 2.0 + acumri(pos)) * b[pos];
}
int main() {
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(15);
expo[0] = 1;
for (int i = 1; i < 300000; i++) expo[i] = expo[i - 1] / 2.0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> b[i];
indice[i] = i;
inserta(i, b[i]);
}
sort(indice, indice + n, compara);
double s = 0;
for (int k = 0; k < n; k++) {
int i = indice[k];
s += acum(i);
inserta(i, 0);
}
cout << s / n / n << endl;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | //package round222;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class E2 {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] a = na(n);
long[] ai = new long[n];
for(int i = 0;i < n;i++)ai[i] = (long)(100000-a[i])<<32|i;
ai = radixSort(ai);
double ret = 0;
int[] ft = new int[n+1];
for(int i = 0;i < n;i++){
int num = 100000-(int)(ai[i]>>>32), pos = (int)ai[i];
double left = 0, right = 0;
int bef = pos, aft = pos;
double u = 1.;
for(int j = 0;j < 30;j++){
int nbef = before(ft, bef);
left += (bef-nbef)*u;
u /= 2;
if(nbef == -1)break;
bef = nbef;
}
u = 1.;
for(int j = 0;j < 30;j++){
int naft = after(ft, aft);
if(naft == -1){
right += (n-aft)*u;
break;
}else{
right += (naft-aft)*u;
}
aft = naft;
u /= 2;
}
ret += num*left*right/2;
addFenwick(ft, pos, 1);
}
out.printf("%.15f\n", ret/n/n);
}
public static long[] radixSort(long[] f)
{
long[] to = new long[f.length];
{
int[] b = new int[65537];
for(int i = 0;i < f.length;i++)b[1+(int)(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < f.length;i++)to[b[(int)(f[i]&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < f.length;i++)b[1+(int)(f[i]>>>16&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < f.length;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < f.length;i++)b[1+(int)(f[i]>>>32&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < f.length;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < f.length;i++)b[1+(int)(f[i]>>>48&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < f.length;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
return f;
}
public static int sumFenwick(int[] ft, int i)
{
int sum = 0;
for(i++;i > 0;i -= i&-i)sum += ft[i];
return sum;
}
public static void addFenwick(int[] ft, int i, int v)
{
if(v == 0)return;
int n = ft.length;
for(i++;i < n;i += i&-i)ft[i] += v;
}
public static int findGFenwick(int[] ft, int v)
{
int i = 0;
int n = ft.length;
for(int b = Integer.highestOneBit(n);b != 0 && i < n;b >>= 1){
if(i + b < n){
int t = i + b;
if(v >= ft[t]){
i = t;
v -= ft[t];
}
}
}
return v != 0 ? -(i+1) : i-1;
}
public static int valFenwick(int[] ft, int i)
{
return sumFenwick(ft, i) - sumFenwick(ft, i-1);
}
public static int[] restoreFenwick(int[] ft)
{
int n = ft.length-1;
int[] ret = new int[n];
for(int i = 0;i < n;i++)ret[i] = sumFenwick(ft, i);
for(int i = n-1;i >= 1;i--)ret[i] -= ret[i-1];
return ret;
}
public static int before(int[] ft, int x)
{
int u = sumFenwick(ft, x-1);
if(u == 0)return -1;
return findGFenwick(ft, u-1)+1;
}
public static int after(int[] ft, int x)
{
int u = sumFenwick(ft, x);
int f = findGFenwick(ft, u);
if(f+1 >= ft.length-1)return -1;
return f+1;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new E2().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300100;
int ar[MAXN];
int A[MAXN];
int B[MAXN];
pair<int, int> arr[MAXN];
int N;
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%d", ar + i);
arr[i - 1] = pair<int, int>(ar[i], i);
}
for (int i = 1; i <= N; ++i) {
A[i] = i - 1;
B[i] = i + 1;
}
long double ans = 0;
sort(arr, arr + N);
for (int i = 0; i < N; ++i) {
int x = arr[i].second;
int a = A[x], b = B[x];
B[a] = b;
A[b] = a;
long double lft = 0, rgt = 0;
int cur = x;
long double scal = 1;
for (int j = 0; j < 40; ++j) {
if (cur) {
lft += (cur - A[cur]) * scal;
cur = A[cur];
scal /= 2;
} else
break;
}
cur = x;
scal = 1;
for (int j = 0; j < 40; ++j) {
if (cur <= N) {
rgt += (B[cur] - cur) * scal;
cur = B[cur];
scal /= 2;
} else
break;
}
ans += lft * rgt * ar[x] / 2;
}
cout << setprecision(10) << (ans / N / N) << "\n";
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60777216")
using namespace std;
int n;
int b[333333];
vector<pair<int, int> > t;
int rv[333333];
struct Node {
Node *lf;
Node *rg;
int l, r;
int mx;
Node() {
lf = rg = 0;
mx = 0;
}
};
Node *root;
Node *buildTree(int from, int to) {
Node *res = new Node();
res->l = from;
res->r = to;
if (from != to) {
res->lf = buildTree(from, (from + to) / 2);
res->rg = buildTree((from + to) / 2 + 1, to);
}
return res;
}
void setValue(Node *curr, int pos, int val) {
if (!curr) return;
if (curr->l > pos || curr->r < pos) return;
curr->mx = max(curr->mx, val);
setValue(curr->lf, pos, val);
setValue(curr->rg, pos, val);
}
int getBiggerL(Node *curr, int to, int val) {
if (!curr) return -1;
if (curr->mx <= val) return -1;
if (curr->l > to) return -1;
if (curr->l == curr->r) return curr->l;
int p1 = getBiggerL(curr->rg, to, val);
if (p1 != -1) return p1;
return getBiggerL(curr->lf, to, val);
}
int getBiggerR(Node *curr, int from, int val) {
if (!curr) return -1;
if (curr->mx <= val) return -1;
if (curr->r < from) return -1;
if (curr->l == curr->r) return curr->l;
int p1 = getBiggerR(curr->lf, from, val);
if (p1 != -1) return p1;
return getBiggerR(curr->rg, from, val);
}
double pw[111111];
int main() {
pw[0] = 1;
for (int i = (1); i < (111111); i++) pw[i] = pw[i - 1] / 2;
cin >> n;
for (int i = (0); i < (n); i++)
scanf("%d", b + i), t.push_back(pair<int, int>(b[i], i));
sort((t).begin(), (t).end());
root = buildTree(0, n - 1);
for (int i = (0); i < (n); i++) {
rv[t[i].second] = i + 1;
setValue(root, t[i].second, i + 1);
}
const int lim = 25;
double sum = 0;
vector<int> l, r;
double den = 1. / n / n;
l.reserve(lim), r.reserve(lim);
for (int i = (0); i < (n); i++) {
l.clear(), r.clear();
int curr = i;
while (1) {
int pos = getBiggerL(root, curr - 1, rv[i]);
if (pos == -1) {
l.push_back(curr + 1);
break;
} else {
l.push_back(curr - pos);
curr = pos;
}
if (l.size() == lim) break;
}
curr = i;
while (1) {
int pos = getBiggerR(root, curr + 1, rv[i]);
if (pos == -1) {
r.push_back(n - curr);
break;
} else {
r.push_back(pos - curr);
curr = pos;
}
if (r.size() == lim) break;
}
for (int i1 = (0); i1 < (l.size()); i1++)
for (int i2 = (0); i2 < (r.size()); i2++) {
sum += b[i] * den * l[i1] * r[i2] * pw[i1 + i2 + 1];
}
}
printf("%.10lf\n", sum);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m;
int id[300010], f[300010], g[300010];
double b[300010], an;
inline bool cc1(const int &A, const int &B) { return b[A] < b[B]; }
int getg(int x) { return g[x] == x ? x : g[x] = getg(g[x]); }
int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); }
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%lf", &b[i]), id[i] = f[i] = g[i] = i;
f[n + 1] = n + 1;
sort(id + 1, id + n + 1, cc1);
for (i = 1; i <= n; i++) {
int A = id[i];
double s = 0, tmp = 0.5;
for (j = A, k = 1; j && k <= 40; k++) {
int B = getg(j - 1);
s += tmp * (j - B);
j = B;
tmp /= 2;
}
tmp = 1;
for (j = A, k = 1; j <= n && k <= 40; k++) {
int B = getf(j + 1);
an += tmp * (B - j) * s * b[A];
j = B;
tmp /= 2;
}
g[A] = A - 1;
f[A] = A + 1;
}
printf("%.10lf\n", an / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N, i, j, B[300010];
pair<int, int> U[300010];
int W[300010];
bool Cmp_pii(pair<int, int> p, pair<int, int> q) {
if (p.first != q.first)
return (p.first > q.first);
else
return (p.second < q.second);
}
const int DEP = 60;
long double Rate[DEP + 5], ANS, Lc, Rc;
int L[300010], R[300010], x, y;
void Delete_element(int x) {
L[R[x]] = L[x];
R[L[x]] = R[x];
}
int main() {
scanf("%d", &N);
for (i = 1; i <= N; i++) scanf("%d", &B[i]);
Rate[0] = 1;
for (i = 1; i <= DEP; i++) Rate[i] = Rate[i - 1] / 2;
for (i = 1; i <= N; i++) U[i] = make_pair(B[i], i);
sort(U + 1, U + N + 1, Cmp_pii);
for (i = 1; i <= N; i++) W[U[i].second] = i;
ANS = 0;
for (i = 0; i <= N; i++) {
R[i] = i + 1;
L[i + 1] = i;
}
for (i = N; i; i--) {
int w = U[i].second;
x = y = w;
Lc = Rc = 0;
for (j = 0; j <= DEP; j++) {
if (x >= 1) {
Lc += Rate[j] * (x - L[x]);
x = L[x];
}
if (y <= N) {
Rc += Rate[j] * (R[y] - y);
y = R[y];
}
}
Delete_element(w);
ANS += 0.5 * Lc * Rc * (U[i].first);
}
ANS /= N;
ANS /= N;
printf("%.9f\n", double(ANS));
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int arr[300005], p[300005];
vector<int> left_[300005], right_[300005];
set<int> f;
const int MAX = 25;
const int UP = MAX + 1;
bool cmp(int i, int j) { return arr[i] < arr[j] || arr[i] == arr[j] && i < j; }
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
p[i] = i;
}
sort(p, p + n, cmp);
set<int>::iterator it;
for (int i = n - 1; i >= 0; --i) {
it = f.upper_bound(p[i]);
for (int j = 0; it != f.end() && j < UP; ++j) {
right_[p[i]].push_back(*it);
++it;
}
it = f.upper_bound(p[i]);
if (it != f.begin()) {
--it;
for (int j = 0; j < UP; ++j) {
left_[p[i]].push_back(*it);
if (it == f.begin()) break;
--it;
}
}
f.insert(p[i]);
}
long double pw2[150] = {0}, sums[150] = {0};
pw2[0] = 1;
for (int i = 1; i < 140; ++i) pw2[i] = pw2[i - 1] * 2;
long double ans = 0;
for (int t = 0; t < n; ++t) {
for (int i = 0; i <= left_[t].size(); ++i) {
int lp = (i == 0 ? t : left_[t][i - 1]) -
(i < left_[t].size() ? left_[t][i] : -1);
for (int j = 0; j <= right_[t].size() && i + j <= MAX; ++j) {
int rp = (j < right_[t].size() ? right_[t][j] : n) -
(j == 0 ? t : right_[t][j - 1]);
sums[i + j + 1] += 1.0 * arr[t] * lp * rp;
}
}
}
for (int i = 0; i <= 100; ++i) ans += sums[i] / pw2[i];
printf("%.15lf\n", (double)ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Egor Kulikov ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
static final int BUBEN = 25;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
// Random random = new Random(239);
int[] array = new int[count];//IOUtils.readIntArray(in, count);
for (int i = 0; i < count; i++) {
array[i] = in.readInt();
// array[i] = random.nextInt(100000) + 1;
}
int[] order = ArrayUtils.order(array);
int[] next = new int[count];
int[] last = new int[count];
for (int i = 0; i < count; i++) {
next[i] = i + 1;
last[i] = i - 1;
}
double answer = 0;
long[] left = new long[BUBEN];
long[] right = new long[BUBEN];
double[] two = new double[2 * BUBEN];
long[] qty = new long[BUBEN * 2];
two[0] = 1;
for (int i = 1; i < two.length; i++)
two[i] = two[i - 1] * 2;
for (int i : order) {
left[0] = i;
int leftSize = 1;
for (int j = 1; j < BUBEN; j++) {
left[j] = last[((int) left[j - 1])];
leftSize++;
if (left[j] == -1)
break;
}
right[0] = i;
int rightSize = 1;
for (int j = 1; j < BUBEN; j++) {
right[j] = next[((int) right[j - 1])];
rightSize++;
if (right[j] == count)
break;
}
Arrays.fill(qty, 0);
for (int j = 1; j < leftSize; j++) {
for (int k = 1; k < rightSize; k++)
// answer += array[i] / two[j + k - 2] * (left[j - 1] - left[j]) * (right[k] - right[k - 1]);
qty[j + k - 2] += (left[j - 1] - left[j]) * (right[k] - right[k - 1]);
}
for (int j = 0; j < leftSize - 3 + rightSize; j++)
answer += array[i] / two[j] * qty[j];
if (last[i] != -1)
next[last[i]] = next[i];
if (next[i] != count)
last[next[i]] = last[i];
}
answer /= count;
answer /= count;
out.printLine(answer / 2);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
class ArrayUtils {
public static int[] createOrder(int size) {
int[] order = new int[size];
for (int i = 0; i < size; i++)
order[i] = i;
return order;
}
public static int[] sort(int[] array, IntComparator comparator) {
return sort(array, 0, array.length, comparator);
}
public static int[] sort(int[] array, int from, int to, IntComparator comparator) {
if (from == 0 && to == array.length)
new IntArray(array).inPlaceSort(comparator);
else
new IntArray(array).subList(from, to).inPlaceSort(comparator);
return array;
}
public static int[] order(final int[] array) {
return sort(createOrder(array.length), new IntComparator() {
public int compare(int first, int second) {
if (array[first] < array[second])
return -1;
if (array[first] > array[second])
return 1;
return 0;
}
});
}
}
abstract class IntCollection {
public abstract IntIterator iterator();
public abstract int size();
}
interface IntIterator {
public int value() throws NoSuchElementException;
/*
* @throws NoSuchElementException only if iterator already invalid
*/
public void advance() throws NoSuchElementException;
public boolean isValid();
}
interface IntComparator {
public static final IntComparator DEFAULT = new IntComparator() {
public int compare(int first, int second) {
if (first < second)
return -1;
if (first > second)
return 1;
return 0;
}
};
public int compare(int first, int second);
}
abstract class IntList extends IntCollection implements Comparable<IntList> {
private static final int INSERTION_THRESHOLD = 16;
public abstract int get(int index);
public abstract void set(int index, int value);
public IntIterator iterator() {
return new IntIterator() {
private int size = size();
private int index = 0;
public int value() throws NoSuchElementException {
if (!isValid())
throw new NoSuchElementException();
return get(index);
}
public void advance() throws NoSuchElementException {
if (!isValid())
throw new NoSuchElementException();
index++;
}
public boolean isValid() {
return index < size;
}
};
}
public IntList subList(final int from, final int to) {
return new SubList(from, to);
}
private void swap(int first, int second) {
if (first == second)
return;
int temp = get(first);
set(first, get(second));
set(second, temp);
}
public IntSortedList inPlaceSort(IntComparator comparator) {
quickSort(0, size() - 1, (Integer.bitCount(Integer.highestOneBit(size()) - 1) * 5) >> 1, comparator);
return new IntSortedArray(this, comparator);
}
private void quickSort(int from, int to, int remaining, IntComparator comparator) {
if (to - from < INSERTION_THRESHOLD) {
insertionSort(from, to, comparator);
return;
}
if (remaining == 0) {
heapSort(from, to, comparator);
return;
}
remaining--;
int pivotIndex = (from + to) >> 1;
int pivot = get(pivotIndex);
swap(pivotIndex, to);
int storeIndex = from;
int equalIndex = to;
for (int i = from; i < equalIndex; i++) {
int value = comparator.compare(get(i), pivot);
if (value < 0)
swap(storeIndex++, i);
else if (value == 0)
swap(--equalIndex, i--);
}
quickSort(from, storeIndex - 1, remaining, comparator);
for (int i = equalIndex; i <= to; i++)
swap(storeIndex++, i);
quickSort(storeIndex, to, remaining, comparator);
}
private void heapSort(int from, int to, IntComparator comparator) {
for (int i = (to + from - 1) >> 1; i >= from; i--)
siftDown(i, to, comparator, from);
for (int i = to; i > from; i--) {
swap(from, i);
siftDown(from, i - 1, comparator, from);
}
}
private void siftDown(int start, int end, IntComparator comparator, int delta) {
int value = get(start);
while (true) {
int child = ((start - delta) << 1) + 1 + delta;
if (child > end)
return;
int childValue = get(child);
if (child + 1 <= end) {
int otherValue = get(child + 1);
if (comparator.compare(otherValue, childValue) > 0) {
child++;
childValue = otherValue;
}
}
if (comparator.compare(value, childValue) >= 0)
return;
swap(start, child);
start = child;
}
}
private void insertionSort(int from, int to, IntComparator comparator) {
for (int i = from + 1; i <= to; i++) {
int value = get(i);
for (int j = i - 1; j >= from; j--) {
if (comparator.compare(get(j), value) <= 0)
break;
swap(j, j + 1);
}
}
}
public int hashCode() {
int hashCode = 1;
for (IntIterator i = iterator(); i.isValid(); i.advance())
hashCode = 31 * hashCode + i.value();
return hashCode;
}
public boolean equals(Object obj) {
if (!(obj instanceof IntList))
return false;
IntList list = (IntList)obj;
if (list.size() != size())
return false;
IntIterator i = iterator();
IntIterator j = list.iterator();
while (i.isValid()) {
if (i.value() != j.value())
return false;
i.advance();
j.advance();
}
return true;
}
public int compareTo(IntList o) {
IntIterator i = iterator();
IntIterator j = o.iterator();
while (true) {
if (i.isValid()) {
if (j.isValid()) {
if (i.value() != j.value()) {
if (i.value() < j.value())
return -1;
else
return 1;
}
} else
return 1;
} else {
if (j.isValid())
return -1;
else
return 0;
}
i.advance();
j.advance();
}
}
private class SubList extends IntList {
private final int to;
private final int from;
private int size;
public SubList(int from, int to) {
this.to = to;
this.from = from;
size = to - from;
}
public int get(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return IntList.this.get(index + from);
}
public void set(int index, int value) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
IntList.this.set(index + from, value);
}
public int size() {
return size;
}
}
}
abstract class IntSortedList extends IntList {
protected final IntComparator comparator;
protected IntSortedList(IntComparator comparator) {
this.comparator = comparator;
}
public void set(int index, int value) {
throw new UnsupportedOperationException();
}
public IntSortedList inPlaceSort(IntComparator comparator) {
if (comparator == this.comparator)
return this;
throw new UnsupportedOperationException();
}
protected void ensureSorted() {
int size = size();
if (size == 0)
return;
int last = get(0);
for (int i = 1; i < size; i++) {
int current = get(i);
if (comparator.compare(last, current) > 0)
throw new IllegalArgumentException();
last = current;
}
}
public IntSortedList subList(final int from, final int to) {
return new IntSortedList(comparator) {
private int size = to - from;
@Override
public int get(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
return IntSortedList.this.get(index + from);
}
@Override
public int size() {
return size;
}
};
}
}
class IntArray extends IntList {
private final int[] array;
public IntArray(int[] array) {
this.array = array;
}
public IntArray(IntCollection collection) {
array = new int[collection.size()];
int i = 0;
for (IntIterator iterator = collection.iterator(); iterator.isValid(); iterator.advance())
array[i++] = iterator.value();
}
public int get(int index) {
return array[index];
}
public void set(int index, int value) {
array[index] = value;
}
public int size() {
return array.length;
}
}
class IntSortedArray extends IntSortedList {
private final int[] array;
public IntSortedArray(int[] array) {
this(array, IntComparator.DEFAULT);
}
public IntSortedArray(IntCollection collection) {
this(collection, IntComparator.DEFAULT);
}
public IntSortedArray(int[] array, IntComparator comparator) {
super(comparator);
this.array = array;
ensureSorted();
}
public IntSortedArray(IntCollection collection, IntComparator comparator) {
super(comparator);
array = new int[collection.size()];
int i = 0;
for (IntIterator iterator = collection.iterator(); iterator.isValid(); iterator.advance())
array[i++] = iterator.value();
ensureSorted();
}
public int get(int index) {
return array[index];
}
public int size() {
return array.length;
}
}
| JAVA |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
struct jsb {
int a, b;
} data[310000];
inline bool cmp(const jsb &a, const jsb &b) { return a.a > b.a; }
double Pow[310000];
struct Info {
double al, ar;
int sum;
inline Info operator+(const Info &b) const {
Info c;
c.al = c.ar = c.sum = 0;
c.sum = b.sum + sum;
c.al = al + b.al * Pow[sum];
c.ar = b.ar + ar * Pow[b.sum];
return c;
}
} info[310000 * 4];
void add(int now, int l, int r, int x) {
if (l == r) {
info[now].sum = 1;
info[now].al = info[now].ar = 0.5 * x;
return;
}
int mid = (l + r) >> 1;
if (x <= mid) add(now * 2, l, mid, x);
if (x > mid) add(now * 2 + 1, mid + 1, r, x);
info[now] = info[now * 2] + info[now * 2 + 1];
}
Info ask(int now, int l, int r, int x, int y) {
if (x <= l && r <= y) return info[now];
int mid = (l + r) >> 1;
Info res;
res.sum = 0;
res.al = res.ar = 0;
if (x <= mid) res = res + ask(now * 2, l, mid, x, y);
if (y > mid) res = res + ask(now * 2 + 1, mid + 1, r, x, y);
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &data[i].a);
data[i].b = i;
}
sort(data + 1, data + 1 + n, cmp);
Pow[0] = 1;
for (int i = 1; i <= n; i++) Pow[i] = Pow[i - 1] * 0.5;
double res = 0;
for (int i = 1; i <= n; i++) {
add(1, 1, n, data[i].b);
double lin, rin;
Info l, r;
if (data[i].b >= 2)
l = ask(1, 1, n, 1, data[i].b - 1);
else {
l.sum = l.al = l.ar = 0;
}
if (data[i].b + 1 <= n)
r = ask(1, 1, n, data[i].b + 1, n);
else {
r.sum = r.al = r.ar = 0;
}
l.ar = data[i].b - l.ar;
r.al = r.al - data[i].b + Pow[r.sum] * (n + 1);
res += 0.5 * l.ar * r.al * data[i].a;
}
printf("%.8lf\n", res / (1. * n * 1. * n));
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int T = 100;
int rk[N], w[N], n;
int nxt[N], pre[N];
bool cmp(int x, int y) { return w[x] ^ w[y] ? w[x] < w[y] : x < y; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]), rk[i] = i;
sort(rk + 1, rk + n + 1, cmp);
for (int i = 1; i <= n; i++) nxt[i] = i + 1, pre[i] = i - 1;
double ans = 0.0;
for (int i = 1; i <= n; i++) {
int x = rk[i], l = x, r = x;
double lw = 0.0, rw = 0.0, coe = 1.0;
for (int j = 1; j <= T; j++) {
if (l) lw += coe * (l - pre[l]), l = pre[l];
if (r <= n) rw += coe * (nxt[r] - r), r = nxt[r];
coe *= 0.5;
}
ans += w[x] * lw * rw * 0.5;
nxt[pre[x]] = nxt[x], pre[nxt[x]] = pre[x];
}
printf("%.12lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, a[300005], b[300005], l[300005], r[300005];
double ans;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) x = (x + (x << 2) << 1) + (ch ^ 48);
return x * f;
}
bool cmp(const int &x, const int &y) { return a[x] < a[y]; }
int main() {
n = read();
for (int i = 1; i <= n; i++)
a[i] = read(), b[i] = i, l[i] = i - 1, r[i] = i + 1;
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++) {
double x = 0, y = 0, z = 1;
int u = b[i], v = b[i];
for (int j = 60; j; j--) {
if (u) x += (u - l[u]) * z, u = l[u];
if (v <= n) y += (r[v] - v) * z, v = r[v];
z /= 2;
}
ans += x * y * a[b[i]], l[r[b[i]]] = l[b[i]], r[l[b[i]]] = r[b[i]];
}
printf("%lf", ans / 2 / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int n, b[N], i, j, l[N], r[N], u, v, p[N], k;
double x, y, z, ans;
bool cmp(int x, int y) { return b[x] < b[y]; }
int main() {
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
scanf("%d", b + i);
l[i] = i - 1;
r[i] = i + 1;
p[i] = i;
}
sort(p + 1, p + n + 1, cmp);
for (i = 1; i <= n; ++i) {
x = y = 0;
z = 1;
u = v = k = p[i];
for (j = 1; j <= 45; ++j) {
if (u) x += (u - l[u]) * z, u = l[u];
if (v <= n) y += (r[v] - v) * z, v = r[v];
z /= 2;
}
l[r[k]] = l[k];
r[l[k]] = r[k];
ans += x * y * b[k] / 2;
}
printf("%.6f\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
int arr[300010], n;
std::pair<int, int> idx[300010];
int A[300010], B[300010];
double ans, l, r, pw;
int main(void) {
int i, x, a, b, now, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
idx[i].first = arr[i], idx[i].second = i;
}
std::sort(idx + 1, idx + n + 1);
for (i = 1; i <= n; i++) {
A[i] = i - 1, B[i] = i + 1;
}
for (i = 1; i <= n; i++) {
x = idx[i].second;
a = A[x], b = B[x];
B[a] = b;
A[b] = a;
l = r = 0;
now = x, pw = 1;
for (j = 1; j <= 40; j++) {
if (now) {
l += (double)(now - A[now]) * pw;
now = A[now];
pw /= 2;
} else
break;
}
now = x, pw = 1;
for (j = 1; j <= 40; j++) {
if (now <= n) {
r += (double)(B[now] - now) * pw;
now = B[now];
pw /= 2;
} else
break;
}
ans += l * r * (double)idx[i].first / 2;
}
double N = (double)n;
printf("%.10lf\n", ans / N / N);
return false;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 312345;
pair<int, int> b[maxn];
int n, pre[maxn], nxt[maxn];
int main() {
scanf("%d", &n);
for (int(i) = (1); (i) <= (n); (i)++) {
scanf("%d", &b[i].first);
b[i].second = i;
}
sort(b + 1, b + n + 1);
for (int(i) = (1); (i) <= (n); (i)++) nxt[i] = i + 1, pre[i] = i - 1;
double ans = 0;
for (int(i) = (1); (i) <= (n); (i)++) {
int id = b[i].second, lo = id, hi = id;
double l = 0, r = 0, f = 1;
for (int(j) = (0); (j) <= ((60) - 1); (j)++) {
f /= 2;
if (lo) {
l += f * (lo - pre[lo]);
lo = pre[lo];
}
if (hi <= n) {
r += f * (nxt[hi] - hi);
hi = nxt[hi];
}
}
ans += 2 * b[i].first * l * r;
nxt[pre[id]] = nxt[id];
pre[nxt[id]] = pre[id];
}
printf("%.10lf", ans / ((double(n)) * (double(n))));
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 300010;
const int TINY = 1e-13;
int N;
double b[MAXN];
pair<double, int> first[MAXN];
const int MAXS = 30;
int Ls, Rs;
int L[MAXS], R[MAXS];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(12) << fixed;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> b[i];
b[i] += i * TINY;
}
for (int i = 0; i < N; i++) first[i] = make_pair(b[i], i);
sort(first, first + N);
set<int> greater;
double ans = 0;
for (int i = N - 1; i >= 0; i--) {
Ls = Rs = 0;
set<int>::iterator it = greater.lower_bound(first[i].second);
while (it != greater.begin() && Ls < MAXS) {
it--;
L[Ls++] = *it;
}
if (Ls < MAXS) L[Ls++] = -1;
for (it = greater.upper_bound(first[i].second);
it != greater.end() && Rs < MAXS; it++)
R[Rs++] = *it;
if (Rs < MAXS) R[Rs++] = N;
int prevl = first[i].second;
double coef = 0.5;
double sa = 0;
for (int j = 0; j < Ls; j++) {
sa += coef * double(prevl - L[j]);
prevl = L[j];
coef *= 0.5;
}
int prevr = first[i].second;
coef = 1;
for (int j = 0; j < Rs; j++) {
ans += first[i].first * coef * double(R[j] - prevr) * sa;
prevr = R[j];
coef *= 0.5;
}
greater.insert(first[i].second);
}
cout << ans / (N * double(N)) << endl;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int key[300005], pos[300005], pre[300005], nxt[300005], n;
const int maxj = 35;
bool cmp(int x, int y) { return key[x] < key[y]; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &key[i]);
pos[i] = i;
pre[i] = i - 1;
nxt[i] = i + 1;
}
sort(pos + 1, pos + n + 1, cmp);
double ans = 0;
for (int i = 1; i <= n; i++) {
int pnt1 = pos[i], pnt2 = pos[i];
double lsum = 0, rsum = 0, haha = 1;
for (int j = 1; j <= maxj; j++) {
if (pnt1) lsum += (pnt1 - pre[pnt1]) * haha, pnt1 = pre[pnt1];
if (pnt2 <= n) rsum += (nxt[pnt2] - pnt2) * haha, pnt2 = nxt[pnt2];
haha /= 2;
}
ans += lsum * rsum * key[pos[i]] / 2 / n / n;
nxt[pre[pos[i]]] = nxt[pos[i]];
pre[nxt[pos[i]]] = pre[pos[i]];
}
printf("%.15lf\n", ans);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 300 * 1000;
const int MAXK = 30;
const int INF = 1000 * 1000 * 1000;
int a[MAXN + 2];
int l[MAXN + 2][MAXK];
int r[MAXN + 2][MAXK];
int cntl[MAXN + 2], cntr[MAXN + 2];
set<pair<int, int> > is;
long long two[MAXK + 10];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
two[0] = 1;
for (int i = 1; i < MAXK + 10; i++) two[i] = two[i - 1] * 2;
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
cntl[i] = cntr[i] = 1;
l[i][0] = r[i][0] = i;
}
a[0] = a[n + 1] = INF;
is.insert(make_pair(-a[0], 0));
for (int i = 1; i <= n + 1; i++) {
auto it = is.insert(make_pair(-a[i], i)).first;
it++;
while (it != is.end()) {
r[it->second][cntr[it->second]++] = i;
if (cntr[it->second] == MAXK) {
auto buf = it;
it++;
is.erase(buf);
} else {
it++;
}
}
}
is.clear();
is.insert(make_pair(-a[n + 1], n + 1));
for (int i = n; i >= 0; i--) {
auto it = is.insert(make_pair(-a[i], i)).first;
it++;
while (it != is.end()) {
l[it->second][cntl[it->second]++] = i;
if (cntl[it->second] == MAXK) {
auto buf = it;
it++;
is.erase(buf);
} else {
it++;
}
}
}
double ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < MAXK; j++) {
for (int L = 0; L <= min(j, cntl[i] - 1); L++) {
int R = j - L;
if (cntr[i] - 1 > R) {
int cntl = l[i][L] - l[i][L + 1], cntr = r[i][R + 1] - r[i][R];
ans += (double)cntl * cntr * a[i] / two[j + 1];
}
}
}
}
cout.setf(ios::fixed);
cout.precision(20);
cout << ans / n / n;
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int c = 60;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(20) << fixed;
vector<long double> p2(c + 2);
p2[0] = 1.;
for (int i = 1; i <= c + 1; i++) p2[i] = p2[i - 1] / 2.;
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i;
sort(a.begin(), a.end());
long double ans = 0.;
vector<int> nx(n), pv(n);
for (int i = 0; i < n; i++) nx[i] = i + 1, pv[i] = i - 1;
for (pair<int, int> p : a) {
int i = p.second, val = p.first;
vector<int> l, r;
for (int j = i; j != -1 && l.size() < c; j = pv[j]) l.push_back(j);
if (l.size() < c) l.push_back(-1);
for (int j = i; j != n && r.size() < c; j = nx[j]) r.push_back(j);
if (r.size() < c) r.push_back(n);
long double suml = 0., sumr = 0.;
for (int li = 0; li + 1 < l.size(); li++)
suml += ((long double)(l[li] - l[li + 1])) * p2[li + 1];
for (int ri = 0; ri + 1 < r.size(); ri++)
sumr += ((long double)(r[ri + 1] - r[ri])) * p2[ri];
ans += suml * sumr * (long double)val;
if (pv[i] != -1) nx[pv[i]] = nx[i];
if (nx[i] != n) pv[nx[i]] = pv[i];
}
ans /= n * 1. * n;
cout << ans << "\n";
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int MOD = 1e9 + 7;
const int N = 500000;
const int B = 60;
set<int> sk;
long double pw2[3 * B];
long double solve(int id) {
sk.insert(id);
vector<int> lf;
vector<int> rt;
rt.push_back(0);
lf.push_back(0);
auto it = sk.find(id);
it++;
while (it != sk.end() && rt.size() < B) {
rt.push_back(*it - id);
it++;
}
auto iter = set<int>::reverse_iterator(sk.find(id));
for (; (int)lf.size() <= B && iter != sk.rend(); iter++)
lf.push_back(id - *iter);
long double an1 = 0, an2 = 0, an3 = 0;
for (int i = 1; i < rt.size(); i++) {
an1 += (long double)(rt[i] - rt[i - 1]) * pw2[i - 1];
}
for (int i = 1; i < lf.size(); i++) {
an2 += (long double)(lf[i] - lf[i - 1]) * pw2[i - 1];
}
an3 = an1 * an2;
return an3;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
cout << fixed << setprecision(20);
pw2[0] = 1;
for (int i = 1; i < 3 * B; i++) {
pw2[i] = pw2[i - 1] / 2.0;
}
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
long double ans = 0;
sk.insert(-1);
sk.insert(n);
for (int i = 0; i < n; i++) {
ans += (long double)a[i].first * solve(a[i].second) / (long double)n /
(long double)n;
}
cout << ans / 2.0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
int nums[MAXN], b;
set<int> s;
set<int>::iterator it;
double p2[100], tot = 0;
pair<int, int> sortn[MAXN];
void calc(int x) {
double sr = 0, sl = 0;
int prev = x;
it = s.lower_bound(x);
int ct = 0;
while (it != s.end() && ct <= 60) {
sr += (*it - prev) * p2[ct];
++ct;
prev = *it;
it++;
}
prev = x;
ct = 0;
it = s.lower_bound(x);
it--;
while (*it != -1 && ct <= 60) {
sl += (prev - *it) * p2[ct];
++ct;
prev = *it;
it--;
}
sl += (prev + 1) * p2[ct];
s.insert(x);
tot += nums[x] * sr * sl / 2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> b;
for (int i = 0; i < b; ++i) {
cin >> nums[i];
sortn[i] = pair<int, int>(nums[i], i);
}
sort(sortn, sortn + b);
s.insert(-1);
s.insert(b);
p2[0] = 1;
for (int i = 1; i < 100; ++i) p2[i] = .5 * p2[i - 1];
for (int i = b - 1; i >= 0; --i) calc(sortn[i].second);
cout << fixed << setprecision(10) << tot / b / b << endl;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int K = 50;
const int N = 300000;
int n, pre[N + 10], nxt[N + 10];
set<int> s;
struct data {
int va, loc;
} a[N + 10];
double ans = 0;
bool cmp(data p, data q) { return p.va > q.va; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].va);
a[i].loc = i;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
set<int>::iterator nowloc = s.lower_bound(a[i].loc);
if (nowloc == s.end())
nxt[a[i].loc] = n + 1;
else
nxt[a[i].loc] = *nowloc;
if (nowloc == s.begin())
pre[a[i].loc] = 0;
else {
nowloc--;
pre[a[i].loc] = *nowloc;
}
if (nxt[a[i].loc] != -1) pre[nxt[a[i].loc]] = a[i].loc;
if (pre[a[i].loc] != -1) nxt[pre[a[i].loc]] = a[i].loc;
double nowl = 0, px = 1.0;
for (int now = a[i].loc, j = 0; j <= K && now != 0;
j++, now = pre[now], px /= 2)
nowl += px * (now - pre[now]);
double nowr = 0;
px = 1.0;
for (int now = a[i].loc, j = 0; j <= K && now != n + 1;
j++, now = nxt[now], px /= 2)
nowr += px * (nxt[now] - now);
ans += nowl * nowr / n / n * a[i].va;
s.insert(a[i].loc);
}
printf("%.10lf\n", ans / 2);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
bool cmax(int &a, int b) { return (a < b) ? a = b, 1 : 0; }
bool cmin(int &a, int b) { return (a > b) ? a = b, 1 : 0; }
template <typename T>
T read() {
T ans = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (isdigit(ch))
ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
return ans * f;
}
template <typename T>
void write(T x, char y) {
if (x == 0) {
putchar('0');
putchar(y);
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
static char wr[20];
int top = 0;
for (; x; x /= 10) wr[++top] = x % 10 + '0';
while (top) putchar(wr[top--]);
putchar(y);
}
void file() {}
int n;
int a[N];
int rk[N];
void input() {
n = read<int>();
for (register int i = (int)1; i <= (int)n; ++i) a[i] = read<int>(), rk[i] = i;
}
bool cmp(int x, int y) { return a[x] ^ a[y] ? a[x] < a[y] : x < y; }
int nex[N], pre[N];
void work() {
sort(rk + 1, rk + n + 1, cmp);
for (register int i = (int)1; i <= (int)n; ++i)
nex[i] = i + 1, pre[i] = i - 1;
double ans = 0.0;
for (register int i = (int)1; i <= (int)n; ++i) {
int x = rk[i], l = x, r = x;
double lw = 0.0, rw = 0.0, coe = 0.5;
for (register int j = (int)1; j <= (int)50; ++j) {
if (l) lw += coe * (l - pre[l]), l = pre[l];
if (r <= n) rw += coe * (nex[r] - r), r = nex[r];
coe *= 0.5;
}
ans += lw * rw * (double)a[x] * 2;
nex[pre[x]] = nex[x];
pre[nex[x]] = pre[x];
}
printf("%.10lf\n", ans / n / n);
}
int main() {
file();
input();
work();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
set<int> S;
double pwr[44], ans;
int n;
pair<int, int> a[333333];
int p[44], q[44];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].first), a[i].second = i;
sort(a + 1, a + 1 + n);
S.insert(0), S.insert(n + 1);
pwr[0] = 1;
for (int i = 1; i <= 40; ++i) pwr[i] = pwr[i - 1] * 2;
for (int z = n; z >= 1; --z) {
int i = a[z].second, v = a[z].first;
S.insert(i);
set<int>::iterator c = S.find(i);
p[0] = i;
double L = 0, R = 0;
for (int j = 1; j <= 40; ++j) {
if (c == S.begin()) break;
c--;
p[j] = *c;
L += double(p[j - 1] - p[j]) / pwr[j];
}
c = S.find(i);
q[0] = i;
for (int j = 1; j <= 40; ++j) {
c++;
if (c == S.end()) break;
q[j] = *c;
R += double(q[j] - q[j - 1]) / pwr[j - 1];
}
ans += L * R * v;
}
printf("%.10f\n", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void smax(T &a, T b) {
if (a < b) a = b;
}
const int maxn = 3 * 100000 + 1000;
const int K = 50;
int n;
pair<int, int> arr[maxn];
set<int> s;
vector<int> L[maxn], R[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 0, _n = (int)(n); i < _n; i++)
cin >> arr[i].first, arr[i].second = i;
sort(arr, arr + n, greater<pair<int, int> >());
for (int p = 0, _n = (int)(n); p < _n; p++) {
auto cur = s.insert(arr[p].second).first, it = cur;
auto &CL = L[arr[p].second], &CR = R[arr[p].second];
for (int i = 0, _n = (int)(K); i < _n; i++) {
it++;
if (it == s.end()) break;
CR.push_back(*it);
}
it = cur;
for (int i = 0, _n = (int)(K); i < _n; i++) {
if (it == s.begin()) break;
it--;
CL.push_back(*it);
}
while ((int((CL).size())) <= K) CL.push_back(-1);
while ((int((CR).size())) <= K) CR.push_back(n);
}
vector<double> pow2;
pow2.push_back(1.0);
for (int i = 0, _n = (int)(K); i < _n; i++) pow2.push_back(pow2.back() * 2.0);
double res = 0;
for (int i = 0, _n = (int)(n); i < _n; i++) {
double cur = 0;
for (int x = 0, _n = (int)(K); x < _n; x++) {
int rgt =
R[arr[i].second][x] - (x ? R[arr[i].second][x - 1] : arr[i].second);
cur += arr[i].first * (long long)rgt / pow2[x];
}
for (int x = 0, _n = (int)(K); x < _n; x++) {
int lft =
L[arr[i].second][x] - (x ? L[arr[i].second][x - 1] : arr[i].second);
res += cur * lft / pow2[x + 1];
}
}
cout << fixed << setprecision(10) << -res / n / n << endl;
{ return 0; }
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
struct pi {
int le, ri, ma;
} pp[maxn << 2];
void build(int tot, int l, int r) {
pp[tot].le = l;
pp[tot].ri = r;
pp[tot].ma = 0;
if (l == r) return;
build(2 * tot, l, (l + r) / 2);
build(2 * tot + 1, (l + r) / 2 + 1, r);
}
void merg(int tot, int p, int k) {
if (pp[tot].le == pp[tot].ri) {
pp[tot].ma = k;
return;
}
int mid = (pp[tot].le + pp[tot].ri) >> 1;
if (p <= mid)
merg(tot << 1, p, k);
else
merg((tot << 1) | 1, p, k);
pp[tot].ma = max(pp[tot << 1].ma, pp[(tot << 1) | 1].ma);
}
int ss;
void query(int u, int tot, int r, int p) {
if (r == 0) return;
if (pp[tot].ri <= r) {
if (pp[tot].ma >= p + u) {
ss = tot;
}
return;
}
int mid = (pp[tot].le + pp[tot].ri) >> 1;
if (r <= mid)
query(u, tot << 1, r, p);
else {
query(u, tot << 1, r, p);
query(u, (tot << 1) | 1, r, p);
}
}
int find(int u, int tot, int p) {
if (pp[tot].le == pp[tot].ri) return pp[tot].le;
if (pp[(tot << 1) | 1].ma >= p + u) return find(u, (tot << 1) | 1, p);
return find(u, tot << 1, p);
}
int b[maxn];
int a[maxn];
int r[maxn][60];
int l[maxn][60];
double er[61];
int main() {
int i, j, n;
cin >> n;
for (i = 1; i <= n; i++) scanf("%d", &b[i]);
build(1, 1, n);
er[0] = 1;
for (i = 1; i <= 46; i++) {
er[i] = er[i - 1] * 2.0;
}
for (i = 1; i <= n; i++) {
r[n - i + 1][0] = n - i + 1;
r[n - i + 1][1] = -1;
int no = i - 1;
int p = 1;
while (no && p < 45) {
ss = -1;
query(0, 1, no, b[i]);
int q;
if (ss > 0) ss = find(0, ss, b[i]);
q = ss;
r[n - i + 1][p] = q;
if (q == -1)
break;
else
r[n - i + 1][p] = n - q + 1;
no = q - 1;
p++;
}
r[n - i + 1][p] = -1;
merg(1, i, b[i]);
}
for (i = 1; i <= n; i++) a[i] = b[n - i + 1];
build(1, 1, n);
double s = 0;
for (i = 1; i <= n; i++) {
l[i][0] = i;
l[i][1] = -1;
int no = i - 1;
int p = 1;
while (no && p < 45) {
ss = -1;
query(1, 1, no, a[i]);
int q;
if (ss > 0) ss = find(1, ss, a[i]);
q = ss;
l[i][p] = q;
if (q == -1) break;
no = q - 1;
p++;
}
l[i][p] = -1;
double s1 = 0, s2 = 0;
for (j = 0; j < p; j++) {
int x;
if (l[i][j + 1] == -1)
x = l[i][j];
else
x = l[i][j] - l[i][j + 1];
s1 += (1.0 * x) / er[j];
}
for (j = 0; j < 45 && r[i][j] != -1; j++) {
int x;
if (r[i][j + 1] == -1)
x = n - r[i][j] + 1;
else
x = r[i][j + 1] - r[i][j];
s2 += (1.0 * x) / er[j];
}
s += s1 * s2 * a[i] / 2;
merg(1, i, a[i]);
}
s /= 1.0 * n * n;
printf("%.10lf\n", s);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 * 3 + 10, M = 60;
int n, a[MAXN], id[MAXN], l[MAXN], r[MAXN];
long double Tohka;
bool cmp(int first, int second) { return a[first] < a[second]; }
int main() {
cout << setprecision(10);
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]), l[i] = i - 1, r[i] = i + 1, id[i] = i;
sort(id + 1, id + n + 1, cmp);
for (int tt = 1; tt <= n; ++tt) {
int i = id[tt], L = i, R = i;
long double f0 = 0, f1 = 0, p = 1;
for (int j = 1; j <= M; ++j) {
if (L) f0 += p * (L - l[L]), L = l[L];
if (R <= n) f1 += p * (r[R] - R), R = r[R];
p *= 0.5;
}
l[r[i]] = l[i], r[l[i]] = r[i];
Tohka += f0 * f1 * a[i] * 0.5;
}
cout << fixed << Tohka / n / n << endl;
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
gn(x1), gn(x2);
}
template <class T1, class T2, class T3>
inline void gn(T1 &x1, T2 &x2, T3 &x3) {
gn(x1, x2), gn(x3);
}
template <class T1, class T2, class T3, class T4>
inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
gn(x1, x2, x3), gn(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
gn(x1, x2, x3, x4), gn(x5);
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T1, class T2>
inline void print(T1 x1, T2 x2) {
printsp(x1), println(x2);
}
template <class T1, class T2, class T3>
inline void print(T1 x1, T2 x2, T3 x3) {
printsp(x1), printsp(x2), println(x3);
}
template <class T1, class T2, class T3, class T4>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {
printsp(x1), printsp(x2), printsp(x3), println(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {
printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int a[300300], id[300300];
set<int> s;
bool cmp(int u, int v) {
if (a[u] > a[v])
return 1;
else if (a[u] == a[v]) {
if (u > v)
return 1;
else
return 0;
} else
return 0;
}
double calc(int p) {
s.insert(p);
double A = 0;
double B = 0;
set<int>::iterator pre, nxt;
pre = nxt = s.find(p);
for (int i = 0; i < 50 and pre != s.begin(); i++) {
pre--;
A += (double)(*nxt - *pre) / (1ll << i);
nxt--;
}
pre = nxt = s.find(p);
nxt++;
for (int i = 0; i < 50 and nxt != s.end(); i++) {
B += (double)(*nxt - *pre) / (1ll << i);
pre++;
nxt++;
}
return A * B / 2;
}
int main() {
int n;
gn(n);
for (int i = 0; i < n; i++) {
gn(a[i]);
id[i] = i;
}
sort(id, id + n, cmp);
double ans = 0;
s.insert(-1);
s.insert(n);
for (int i = 0; i < n; i++) {
ans += calc(id[i]) * a[id[i]];
}
ans /= ((long long)n * n);
printf("%.6lf\n", ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
int inp() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while (c >= '0' && c <= '9') {
sum = sum * 10 + c - '0';
c = getchar();
}
return sum;
}
int a[300010], nxt[300010], pre[300010], id[300010];
bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
int n = inp();
for (int i = 1; i <= n; i++) {
a[i] = inp();
id[i] = i;
}
std::sort(id + 1, id + n + 1, cmp);
double ans = 0;
for (int i = 1; i <= n; i++) {
pre[i] = i - 1;
nxt[i] = i + 1;
}
nxt[n + 1] = n + 1;
for (int p = 1; p <= n; p++) {
int i = id[p];
double cc = 1;
int l = i;
int r = i;
double ls = 0, rs = 0;
for (int T = 1; T <= 60; T++) {
ls += cc * (double)(l - pre[l]);
l = pre[l];
rs += cc * (double)(nxt[r] - r);
r = nxt[r];
cc /= 2.0;
}
ans += ls * rs * (double)a[i] / 2;
nxt[pre[i]] = nxt[i];
pre[nxt[i]] = pre[i];
}
printf("%.10lf\n", ans / ((double)(n) * (double)(n)));
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int pre[300000 + 5], nxt[300000 + 5];
struct Node {
int val;
int id;
friend bool operator<(Node p, Node q) {
if (p.val == q.val) {
return p.id < q.id;
}
return p.val < q.val;
}
} a[300000 + 5];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].val);
a[i].id = i;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
pre[i] = i - 1;
nxt[i] = i + 1;
}
int x;
double ans = 0.0;
double pow;
double l, r;
int left, right;
for (int i = 1; i <= n; i++) {
x = left = right = a[i].id;
pow = 1.0;
l = r = 0.0;
for (int j = 1; j <= 30; j++) {
pow *= 0.5;
if (left > 0) {
l += pow * (left - pre[left]);
left = pre[left];
}
if (right <= n) {
r += pow * (nxt[right] - right);
right = nxt[right];
}
}
ans += 2 * l * r * a[i].val;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
}
printf("%.6lf\n", ans / n / n);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
set<int> S;
double r[44], ans;
int n, p[44], q[44];
pair<int, int> a[333333];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].first), a[i].second = i;
sort(a + 1, a + 1 + n);
S.insert(0), S.insert(n + 1);
r[0] = 1;
for (int i = 1; i <= 40; ++i) r[i] = r[i - 1] * 2;
for (int z = n; z >= 1; --z) {
int i = a[z].second;
S.insert(p[0] = q[0] = i);
set<int>::iterator c = S.find(i);
double L = 0, R = 0;
for (int j = 1; j <= 40; ++j) {
if (c == S.begin()) break;
c--;
L += (p[j - 1] - (p[j] = *c)) / r[j];
}
c = S.find(i);
for (int j = 1; j <= 40; ++j) {
c++;
if (c == S.end()) break;
R += ((q[j] = *c) - q[j - 1]) / r[j - 1];
}
ans += L * R * a[z].first;
}
printf("%.9f\n", ans / n / n);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int pai[2][300300];
int foi[300300];
int find(int x, int id) {
return pai[id][x] = (x == pai[id][x]) ? x : find(pai[id][x], id);
}
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
int u;
scanf("%d", &u);
v.push_back(pair<int, int>(u, i));
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) pai[0][i] = pai[1][i] = i;
double ans = 0;
for (int i = 0; i < n; i++) {
double L = 0, R = 0, x = 1;
int p = v[i].second;
;
if (p) pai[0][p - 1] = p;
if (p != n - 1) pai[1][p + 1] = p;
;
for (int j = p, t = 0; j < n && t < 100; t++) {
int u = find(j, 0);
R += x * (u - j + 1);
;
x /= 2;
j = u + 1;
}
x = 1;
;
for (int j = p, t = 0; j >= 0 && t < 100; t++) {
int u = find(j, 1);
L += x * (j - u + 1);
;
x /= 2;
j = u - 1;
};
double s = L * v[i].first * R / 2;
s /= n;
s /= n;
ans += s;
}
printf("%.10lf\n", ans);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 77, LOG = 70;
int n, a[N], L[N], R[N], P[N];
bool CMP(int x, int y) { return a[x] < a[y]; }
double A;
vector<int> V, T;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i), L[i] = i - 1, R[i] = i + 1, P[i] = i;
R[n + 1] = n + 1;
sort(P + 1, P + 1 + n, CMP);
for (int i = 1; i <= n; ++i) {
int id = P[i];
int prv = id;
V.clear();
T.clear();
for (int j = 1; j < LOG; ++j) {
int t = L[prv];
V.push_back(prv - t);
prv = t;
}
prv = id;
for (int j = 1; j < LOG; ++j) {
int t = R[prv];
T.push_back(t - prv);
prv = t;
}
L[R[id]] = L[id];
R[L[id]] = R[id];
reverse(V.begin(), V.end());
reverse(T.begin(), T.end());
double le = 0, ri = 0;
for (int x : V) le = (le / 2 + x);
for (int x : T) ri = (ri / 2 + x);
le = (le * ri * a[id] / 2 / n / n);
A += le;
}
printf("%.9f", A);
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
const int OO = 0;
const int inf = 2e9 + 7;
using namespace std;
const int lim = 60;
int n;
vector<pair<int, int>> a;
set<int> S;
vector<int> R, L;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
long double ans = 0;
for (auto &i : a) {
if (OO) cout << "adding " << i.first << " " << i.second << '\n';
L.clear();
R.clear();
int lst = i.second, iter;
auto it = S.upper_bound(lst);
iter = lim;
while (iter > 0 && it != S.end()) {
iter--;
R.push_back(*it - lst);
lst = *it;
it++;
}
if (iter && it == S.end()) R.push_back(n - lst);
lst = i.second;
it = S.lower_bound(lst);
iter = lim;
if (it != S.begin()) {
it--;
while (iter > 0 && it != S.begin()) {
iter--;
L.push_back(lst - *it);
lst = *it;
it--;
}
if (iter && it == S.begin()) {
iter--;
L.push_back(lst - *it);
lst = *it;
}
}
if (iter && it == S.begin()) L.push_back(lst + 1);
if (OO) {
cout << "L ";
for (const auto &j : L) cout << j << " ";
cout << '\n';
cout << "R ";
for (const auto &j : R) cout << j << " ";
cout << '\n';
}
long double suml = 0, coef = 0.5, add = 0;
for (int i = 0; i < L.size(); i++) {
suml += coef * L[i];
coef /= 2;
}
for (int i = 0; i < R.size(); i++) {
add += R[i] * suml;
suml /= 2;
}
ans += i.first * add;
if (OO) cout << "new ans " << ans << '\n';
S.insert(i.second);
}
cout.precision(10);
cout << ans / n / n << '\n';
cin.ignore();
cin.get();
return 0;
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Main223E {
private FastScanner in;
private PrintWriter out;
private class Item {
private int value;
private int pos;
Item(int value, int pos) {
this.value = value;
this.pos = pos;
}
}
public void solve() throws IOException {
int COUNT = 22;
int n = in.nextInt();
Item[] array = new Item[n];
for(int i = 0; i < n; i++) {
array[i] = new Item(in.nextInt(), i);
}
int[] prev = new int[n];
int[] next = new int[n];
for(int i = 0; i < n; i++) {
prev[i] = i - 1;
next[i] = i + 1;
}
Arrays.sort(array, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o1.value - o2.value;
}
});
double result = 0;
long[] left = new long[COUNT];
long[] right = new long[COUNT];
double[] coeff = new double[2 * COUNT];
int[] pow2 = new int[COUNT];
pow2[0] = 2;
for(int i = 1; i < pow2.length; i++) {
pow2[i] = pow2[i - 1] << 1;
}
for(int i = 0; i < n; i++) {
int countLeft = 1;
int currPos = array[i].pos;
left[0] = currPos;
while(currPos != -1 && countLeft < COUNT) {
left[countLeft++] = prev[currPos];
currPos = prev[currPos];
}
int countRight = 1;
currPos = array[i].pos;
right[0] = currPos;
while(currPos != n && countRight < COUNT) {
right[countRight++] = next[currPos];
currPos = next[currPos];
}
for(int j = 0; j < COUNT; j++) {
coeff[j] = 0;
}
for(int j = 1; j < countLeft; j++) {
int c = Math.min(countRight, COUNT + 2 - j);
for(int k = 1; k < c; k++) {
coeff[j + k - 2] += (left[j - 1] - left[j]) * (right[k] - right[k - 1]);
}
}
double res = 0;
for(int j = 0; j < COUNT; j++) {
res += coeff[j] / pow2[j];
}
result += res * array[i].value;
currPos = array[i].pos;
if(next[currPos] != n) {
prev[next[currPos]] = prev[currPos];
}
if(prev[currPos] != -1) {
next[prev[currPos]] = next[currPos];
}
}
result /= n;
result /= n;
out.println(String.format("%.15f", result));
}
public void run() {
try {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] arg) {
new Main223E().run();
}
} | JAVA |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
set<int> S;
int vl[maxn][33];
int vr[maxn][33];
int nl[maxn], nr[maxn];
int a[maxn];
struct NN {
int ind, val;
} nn[maxn];
int cmp(NN a, NN b) { return a.val < b.val; }
long double arr[110];
int main() {
int n, i, j, k;
for (arr[0] = 1, i = 1; i <= 30; i++) arr[i] = arr[i - 1] / 2;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]), nn[i].ind = i, nn[i].val = a[i];
sort(nn + 1, nn + 1 + n, cmp);
for (i = n; i >= 1; i--) {
set<int>::iterator it;
int cnt = 0;
for (it = S.lower_bound(nn[i].ind); it != S.end(); it++) {
vr[nn[i].ind][++nr[nn[i].ind]] = (*it) - nn[i].ind;
++cnt;
if (cnt == 30) break;
}
it = S.lower_bound(nn[i].ind);
if (it == S.begin()) {
S.insert(nn[i].ind);
vl[nn[i].ind][++nl[nn[i].ind]] = nn[i].ind;
vr[nn[i].ind][++nr[nn[i].ind]] = n + 1 - nn[i].ind;
continue;
}
it--;
cnt = 0;
for (;; it--) {
vl[nn[i].ind][++nl[nn[i].ind]] = nn[i].ind - (*it);
++cnt;
if (cnt == 30 || it == S.begin()) break;
}
vl[nn[i].ind][++nl[nn[i].ind]] = nn[i].ind;
vr[nn[i].ind][++nr[nn[i].ind]] = n + 1 - nn[i].ind;
S.insert(nn[i].ind);
}
long double ans = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= nl[i]; j++)
for (k = 1; k <= nr[i]; k++)
if (j + k - 1 <= 30)
ans += a[i] * arr[j + k - 1] * (vl[i][j] - vl[i][j - 1]) *
(vr[i][k] - vr[i][k - 1]);
else
break;
}
double aa = ans / n / n;
printf("%.12lf\n", aa);
}
| CPP |
380_E. Sereja and Dividing | Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element i (1 β€ i β€ |a|);
2. consecutively perform assignments: <image>.
Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.
Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: <image>. Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
Input
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
Output
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
Examples
Input
5
1 2 3 4 1
Output
1.238750000000000 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void __read(T &a) {
cin >> a;
}
template <typename T, typename... Args>
void __read(T &a, Args &...args) {
cin >> a;
__read(args...);
}
constexpr long long M7 = 1000000007ll;
constexpr long long M9 = 1000000009ll;
constexpr long long MFFT = 998244353ll;
template <class T>
void outv(const T &a) {
for (auto &x : a) cout << x << ' ';
}
static mt19937 rnd(static_cast<unsigned>(
chrono::steady_clock::now().time_since_epoch().count()));
auto __fast_io__ = (ios_base::sync_with_stdio(false), cin.tie(nullptr));
int32_t main() {
long long n;
__read(n);
vector<pair<long long, long long> > a(n);
for (long long i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort((a).rbegin(), (a).rend());
const long long B = 40;
double ans = 0;
set<long long> pos{-1, n};
vector<double> st(3 * B);
st[0] = 1;
for (long long i = 1; i < st.size(); ++i) {
st[i] = st[i - 1] / 2;
}
for (auto &[val, ind] : a) {
pos.insert(ind);
vector<long long> left, right;
auto it = pos.lower_bound(ind);
left.push_back(*it);
for (long long i = 1; i < B && it != pos.begin(); ++i) {
it--;
left.push_back(*it);
}
it = pos.lower_bound(ind);
for (long long i = 0; i < B && it != pos.end(); ++i) {
right.push_back(*it);
it++;
}
for (long long i = 0; i + 1 < left.size(); ++i) {
for (long long j = 0; j + 1 < right.size(); ++j) {
ans += val * st[i + j + 1] * (left[i] - left[i + 1]) *
(right[j + 1] - right[j]);
}
}
}
ans /= a.size();
ans /= a.size();
cout << setprecision(10) << fixed << ans;
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.