Guest User

Sum of averages

a guest
Oct 5th, 2016
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f(i,a,b) for(int i = (a); i <= (b); i++)
  3.  
  4. using namespace std;
  5.  
  6. double F[100005], S[100005];
  7. int A[100005], N;
  8.  
  9. int main()
  10. {
  11.     cin >> N;
  12.     f(i,1,N) scanf("%d", &A[i]);
  13.  
  14.     S[1] = 1;
  15.     f(i,2,100000) S[i] = S[i-1] + (1.0 / i);
  16.  
  17.     int a = 1, b = N;
  18.     while(a <= b)
  19.     {
  20.         F[a] = F[a-1] + S[b] - S[a-1];
  21.         F[b] = F[b+1] + S[b] - S[a-1];
  22.         a++, b--;
  23.     }
  24.  
  25.     double ans = 0;
  26.     f(i,1,N) ans += F[i] * A[i];
  27.  
  28.     cout << setprecision(6) << fixed << ans << "\n";
  29. }
Advertisement
Add Comment
Please, Sign In to add comment