codeforces # 1300E. Water Balance (greedy)

Topic links:

https://codeforces.com/contest/1300/problem/E

Meaning of the questions:

 Given a drain, each operation section may be such that the water becomes a water average interval, this operation may be performed many times, the results obtained lexicographically smallest

data range:

$ 1 \ leq a \ leq 1e10 $

$ 1 \ leq m \ leq 1e10 $

analysis: 

 Consider sub-problems, if you already have a glass of water before the $ i $ result, coupled with a glass of water should be how to operate?

 Obviously the best way is to make the comparison to the last glass of water in front of and, if the last water than previous small, then merge it with the previous

 Realization of the time there is a clever way to merge good water we put them as a block, so there will be at most $ n-1 $ merger to ensure that the complexity

AC Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+7;
int n,a[maxn],cnt,len[maxn];
dual years [maxn];
int main () {
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
        years [cnt ++] = a [i];
        Only [cnt] = 1;
        while(cnt>1&&ans[cnt]<ans[cnt-1]){
            ans [cnt-1] = (ans [cnt-1] * as [cnt-1] + ans [cnt] * as [cnt]) / (referred to as [cnt] + as [cnt-1]);
            Only [cnt-1] + = as [cnt];
            cnt--;
        }
//        printf("-----------\n");
    }
    for(int i=1;i<=cnt;i++){
        for(int j=1;j<=len[i];j++)
            printf("%.12f\n",ans[i]);
    }
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/carcar/p/12305440.html
Recommended