题意:1 、往集合里面加一个数,这个数比集合里所有元素都大
2、取一个子集使得 (子集中最大数MAX - 子集所有数的平均值) 最大
题解:emm不太会,看题解才发现是个凹性函数,可以用三分求解。
因为加入的数一定是最大数,贪心的想一定取这个数+一些较小的数
而三分的特点是尽量取较小的数,写一写可以发现是个三分
还有看到一个坑是(当三分查找的元素只剩两个时,不管大小,他只会默认左边的元素小,所以这里需要特殊判断,就是元素为2个的时候就跳出,转自这)
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define mod 10056
const int maxn = 5e5 + 5;
using namespace std;
LL a[maxn];
LL sum[maxn];
int cnt;
double judge(int x){
return (double)a[cnt] - ((sum[x]+a[cnt])/(double)(x+1));
}
int main(){
int q;
scanf("%d",&q);
cnt = 0;
for(int i=0; i<q; i++){
int x;
scanf("%d",&x);
if(x == 1){
cnt++;
scanf("%lld",&a[cnt]);
sum[cnt] = sum[cnt-1] + a[cnt];
}
else{
int l = 1;
int r = cnt;
while(l<r-1){
int mid = (l+r)/2;
int mmid = (mid+r)/2;
if( judge(mid) >= judge(mmid) )
r = mmid;
else
l = mid;
}
double ans = 0;
for(int i=l; i<=r; i++){
ans = max(ans, judge(i));
}
printf("%.10lf\n",ans);
}
}
}