Codeforces Round #618 (Div. 2) E. Water Balance

关注好题目的每一个条件,比如可以多次倒水就是一个很重要的提示
刷题开心!用脑开心

#include
//#include
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
const int MAXN = 1e6+20;
int n,arr[MAXN];
double mean[MAXN],sum[MAXN],ans[MAXN];
//set,greater>>se;
vector<pair<double,int>>vec;
int main()
{
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        scanf("%d",&arr[i]);
        sum[i]=arr[i]+sum[i-1];
        mean[i]=sum[i]/i;
    }
    vec.push_back({arr[n],n});
    for (int i = n-1; i >= 1; --i) {
        auto cur=vec.back();
        int idx=cur.second;
        //cout << idx << " idx arr[i]" << arr[i] << endl;
        if(cur.first>arr[i]){
            vec.push_back({arr[i],i});
        }
        else{
            double curave=arr[i];
            while(vec.size()){
                cur=vec.back();
                if(cur.first>curave)break;
                else{
                    idx=cur.second;
                    curave=(sum[idx]-sum[i-1])/(idx-i+1);
                    vec.pop_back();
                }
            }
            //cout << idx << " idx i " << i << endl;
            //cout << "ave:" << curave << endl;
            vec.push_back({curave,idx});
        }
    }
    for (int i = 1; i <= n; ++i) {
        auto cur=vec.back();
        for (int j = i; j <= cur.second; ++j) {
            printf("%.9f\n",cur.first);
        }
        i=cur.second;
        vec.pop_back();
    }
    return 0;
}

你可能感兴趣的:(codeforce)