P1404 平均数(二分)

7 篇文章 0 订阅
3 篇文章 0 订阅

P1404 平均数(二分)

这是一个很常见的二分题目可能题目正解不是二分是单调队列,而这题呢 我之前一直被几组数组卡到怀疑人生,01数规划嘛就是让a[i]-mid,然后加起来如果>=0那么就满足条件,然后我就想着长度为m的一定是最大的,但是数据教我做人,等下看代码就知道了。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 1e6 + 10;
const int maxm = 1.5e5 + 50;
const double eps = 1e-7;
const double inf = 0x3f3f3f3f;
const ll  lnf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const  double pi = 3.141592653589;
int n, m;
double a[maxn], sum[maxn];
bool check(double x)
{
	for (int i = 1; i <= n; i++)
	{
		sum[i] = sum[i - 1] + (a[i] - x);
	}
	double mi = 1e16;
	for (int i = m; i <= n; i++)
	{
		mi = min(mi, sum[i - m]);
		if (double(sum[i] - mi)  >= 0.0)return true;
	}
	return false;
}
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lf", &a[i]);
	}
	double l = 0, r = 1e9;
	while (r - l > eps)
	{
		double mid = (l + r) / 2.0;
		if (check(mid))
		{
			l = mid;

		}
		else
		{
			r = mid;
		}
	}
	printf("%d\n", (int)((r) * 1000));
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值