【題解】Codeforces 1300E. Water Balance

【題目敘述】http://codeforces.com/contest/1300/problem/E
【解題想法】用vector維護的單調棧

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
 
int n, a;
struct s{
    long long sum;
    int sz;
    double avg;
};
s x, y;
vector <s> v;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a;
        v.push_back({a, 1, a});
        while (v.size() >= 2 && v.back().avg < v[v.size()-2].avg){
            x = v.back();
            v.pop_back();
            y = v.back();
            v.pop_back();
            v.push_back({x.sum+y.sum, x.sz+y.sz, (double)(x.sum+y.sum)/(double)(x.sz+y.sz)});
        }
    }
    cout << fixed << setprecision(10);
    for (s i:v){
        for (int j = 0; j < i.sz; j++){
            cout << i.avg << "\n";
        }
    }
}
分享本文 Share with friends