标签:iostream cto || getch cst mit 利用 read pre
分块讨论。字典序最小,则,当i-1满足最小时,第i个也必须要满足往后找平均数使得第i个最小。
即,因为答案最后肯定是多个块组成,我们讨论第k块,当第k块最小时,k+1,k+2。。。。往后的所有块,均值一定大于第k快的均值,否则,将后面的块向前合并,一定更优。满足单调性,利用单调栈即可解决。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<‘0‘ || ch>‘9‘) { if (ch == ‘-‘)f = -1; ch = getchar(); }
while (ch >= ‘0‘ && ch <= ‘9‘) { x = x * 10 + ch - ‘0‘; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 1e6 + 10;
ll ai[N], cnt[N], len[N], sum[N];
double ans[N];
int top = 0;
int n;
int main()
{
n = read();
upd(i, 1, n)ai[i] = read();
upd(i, 1, n)
{
if (ans[top] > ai[i])
{
sum[top] += ai[i];
len[top]++;
ans[top] = (double)sum[top] / len[top];
}
else if(top==0||ai[i]>=ans[top]){
top++;
len[top] = 1;
sum[top] = ai[i];
ans[top] = ai[i];
}
//cout << top << endl;
while (top>=2&&ans[top]<ans[top-1])
{
sum[top - 1] += sum[top];
len[top - 1] += len[top];
ans[top - 1] = (double)sum[top - 1] / (double)len[top - 1];
top--;
}
}
upd(i, 1, top)
{
upd(j, 1, len[i])
{
printf("%.9lf ", ans[i]);
}
}
return 0;
}
Codeforces Round #618 1300E Water Balance
标签:iostream cto || getch cst mit 利用 read pre
原文地址:https://www.cnblogs.com/LORDXX/p/12797901.html