Codeforces 1300E. Water Balance

给你一个数列,有一个操作,将一段数字变成其和除以个数,求字典序最小的那一个,分析知,求字典序最小,就是求一个不下降序列,但我们此时有可以更改数字的操作,已知已经不下降的序列不会因为操作而变的更小,只有右边的数比左边的数小的时候才需要操作,那我们可以维护一个单调栈,依次加入数字,栈顶就是当前最右的数字,再维护一个长度信息,这样,每次加入一个数就能判断是否需要操作了,若需要操作,就更新长度和数字进行即可

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;

const int maxm = 1e6+5;
double q[maxm];
int buf[maxm], len[maxm];


void run_case() {
    int n;
    cin >> n;
    int top = 0;
    for(int i = 1; i <= n; ++i) cin >> buf[i];
    q[++top] = buf[1], len[1] = 1;
    for(int i = 2; i <= n; ++i) {
        double now = buf[i];
        int nowlen = 1;
        while(q[top] > now) {
            now = (now*nowlen+q[top]*len[top])/(nowlen+len[top]);
            nowlen += len[top--];
        }
        q[++top] = now, len[top] = nowlen;
    }
    for(int i = 1; i <= top; ++i)
        for(int j = 0; j < len[i]; ++j)
            cout << q[i] << "\n";
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.setf(ios_base::showpoint);cout.precision(10);
    //int t; cin >> t;
    //while(t--)
    run_case();
    cout.flush();
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/GRedComeT/p/12296140.html