CSES - Practice Contest 2024 - Results
Submission details
Task:Approximate
Sender:\(._.)/
Submission time:2024-09-28 13:00:57 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.32 sdetails

Code

// Online C++ compiler to run C++ program online
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
using namespace std;
int main() {
    int n, q;
    cin >> n >> q;
    
    vector<int> arr(n);
    vector<int> arr2(n);
    
    int f = 0;
    cin >> f;
    arr[0] = f;
    arr2[0] = f * f;
    for (int i = 1; i < n; i++)
    {
        int t = 0;
        cin >> t;
        arr[i] = t + arr[i-1];
        arr2[i] = t * t + arr2[i-1];
    }
    
    //for (int i = 0; i < n; i++)
    //    cout << arr[i] << " ";
    
    for (int i = 0; i < q; i++)
    {
        int a, b;
        cin >> a >> b;
        double n = b-a+1;
        a--;a--;
        b--;
        int sub = a >= 0 ? arr[a] : 0;
        int c = arr[b] - sub;
        
        int sub2 = a >= 0 ? arr2[a] : 0;
        int c2 = arr2[b] - sub2;
        
        //cout << c << " " << c2 << " " << n << endl;;
        
        //double equation = ( 1.0/n*c + sqrt(pow(1.0/n*c, 2) - 4.0 * (1.0/n) * c2) ) / 2.0;
        // cout << c2 * n << endl;
        //double equation = ( c - sqrt(pow(c, 2) - c2 * n) ) / n;
        double num = ((double)c) / n;
        //cout << num << endl;
        //cout << 
        double num2 = c2 / n - 2 * c * num / n + num * num;
        std::cout << std::fixed;
        std::cout << std::setprecision(6);
        cout << num2 << endl;
        // cout << equation << endl;
        
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100000 100000
62 64 35 47 57 38 52 4 56 13 7...

correct output
831.753342
833.361649
833.847478
834.425131
831.468120
...

user output
831.753342
833.361649
833.847478
834.425131
831.468120
...
Truncated