JZOI1169A 平均数Ave

#include <cstdio>
#include <cmath>
#define lztin() read()
#define ztyout( a ) printf( "%.4lf", a )
#define eps 1e-5
#define ll long long
using namespace std;
inline int read() {
	int x = 0,tmp = 1;char ch = getchar();
	while( ch < '0' || ch > '9' ) {if ( ch == '-' ) tmp = -1; ch = getchar();}
	while( ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar(); }
	return x * tmp;
}
int A[110000], N, K, cnt;
double B[1100000], T[1100000];
double ans;
void msort( int l, int r ) {
	if( l == r ) return;
	int mid = l + r >> 1;
	msort( l, mid ); 
	msort( mid + 1, r );
	int i = l, k = l, j = mid + 1;
	while( i <= mid && j <= r ) {
		if( B[i] > B[j] ) {
			cnt += mid - i + 1;
			T[k] = B[j];
			k ++, j ++;
		} else {
			T[k] = B[i];
			k ++, i ++;
		}
	}
	while( i <= mid ) {
		T[k] = B[i];
		k ++, i ++;
	}
	while( j <= r ) {
		T[k] = B[j];
		k ++, j ++;
	}
	for( int w = l ; w <= r ; ++ w ) B[w] = T[w];
}
int check( double mid ) {
	B[0] = 0; cnt = 0;
	for( int i = 1 ; i <= N ; ++ i ) {
		B[i] = B[i - 1] + (double)A[i] - mid;
	}
	msort( 1, N );
	for( int i = 1 ; i <= N ; ++ i )
		cnt += ( B[i] < 0 );
	return ( cnt >= K );
}
int main() {
	N = lztin(), K = lztin();
	for( int i = 1 ; i <= N ; ++ i ) {
		A[i] = lztin();
	}
	double l = 0, r = (double)1e9, mid;
	while( fabs( l - r ) > eps ) {
		mid = ( l + r ) / 2.0;
		if( check( mid ) ) ans = mid, r = mid;
		else l = mid;
	}
	ztyout( ans );
	
	return 0;
}
posted @ 2017-05-16 16:24  ARZhu  阅读(207)  评论(0编辑  收藏  举报