嘟嘟嘟




看到比值,就想到01分数规划,令\(ans = \frac{\sum a_i}{\sum l_i}\),其中\(l\)表示长度,所以\(l_i\)都是\(1\)。

然后变一下型,得到\(\sum (a_i - ans) = 0\)。这就是01分数规划的标准形式了。

所以我们按套路二分,每一次数组中的元素就是\(a_i - mid\),然后求最大连续和。

如果大于等于\(0\),说明\(mid\)小了,向右额分;否则向左二分。




求最大连续和的时候,因为要记录左右端点,所以用前缀和的方法求最方便。

预处理前缀和。枚举右端点,那么左端点自然要取最小的,这样才能使差最大,\(O(n)\)扫一遍的时候一起维护。

总复杂度\(O(n \log n)\)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, len;
char s[maxn]; int id1, id2;
db sum[maxn];
bool judge(db x)
{
for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + s[i] - '0' - x;
int pos = 0; db Max = -INF;
for(int i = len; i <= n; ++i)
{
if(sum[i - len] <= sum[pos] - eps) pos = i - len;
if(sum[i] - sum[pos] > Max + eps) id1 = pos + 1, id2 = i, Max = sum[i] - sum[pos]; }
return Max > -eps;
}
void solve()
{
db L = 0, R = 1;
while(R - L > eps)
{
db mid = (L + R) / 2;
if(judge(mid)) L = mid;
else R = mid;
}
judge(L + eps);
write(id1), space, write(id2), enter;
} int main()
{
int T = read();
while(T--)
{
n = read(); len = read();
scanf("%s", s + 1);
solve();
}
return 0;
}

UVA1451 Average的更多相关文章

  1. 【斜率优化】Average

    [UVa1451]Average 算法竞赛入门经典第8章8-9 ( P243 ) 题目大意:给定一个长度为N的01串,选择一个长度至少为L的连续子串,使序列平均值最大 (N<=100001) 题 ...

  2. training 2

    Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU ...

  3. [LeetCode] Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  4. Average Precision of VOC

    转一篇文章,主要是关于VOC中Average Precision指标的 原文出处:https://sanchom.wordpress.com/tag/average-precision/ 还有一篇文章 ...

  5. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  6. average slice

    A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such ...

  7. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  8. Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)

    在Linq中有一些这样的操作,根据集合计算某一单一值,比如集合的最大值,最小值,平均值等等.Linq中包含7种操作,这7种操作被称作聚合操作. 1.Count操作,计算序列中元素的个数,或者计算满足一 ...

  9. Load Average

    在Linux系统下面,有很多的命令可以查看系统的负载情况:比如top,uptime,w,示例如下: [wenchao.ren@l-cmsweb1.ops.cn1 ~]$ w 18:39:10 up 7 ...

随机推荐

  1. Spark2.1.0——内置Web框架详解

    Spark2.1.0——内置Web框架详解 任何系统都需要提供监控功能,否则在运行期间发生一些异常时,我们将会束手无策.也许有人说,可以增加日志来解决这个问题.日志只能解决你的程序逻辑在运行期的监控, ...

  2. csharp: Setting the value of properties reflection

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. jQuery合并同一列中相同文本的相邻单元格

    一.效果图 二.在html的head中引入jQuery <script language="javascript" src="${pageContext.reque ...

  4. 将表格导出为excel

    <table id="tableExcel" border="1"> <tr> <th>零</th> <t ...

  5. 2018-01-19 Xtext试用: 快速实现简单领域专用语言(DSL)

    环境搭建 使用的Eclipse版本: Oxygen.1a Release (4.7.1a) Build id: 20171005-1200, 通过添加Xtext - Download上列出的Relea ...

  6. html标签必备

    常用快捷键 Ctrl+c 复制 Ctrl+v 粘贴 Ctrl+x 剪切 Ctrl+a 全选 Ctrl+s 保存 Ctrl+z 撤销一步 Windows+d 返回桌面 Windows+e 我的电脑 Wi ...

  7. ionic3打包不能prod的问题

    在最近的项目中,我ionic3采用了懒加载,来提高性能.但是当我普通打包的时候,正常成功了,但是加上--prod的时候,就报错了. 报错如下: 大概意思就是page是声明的一部分,然后请在更高级声明之 ...

  8. web全栈架构师[笔记] — 02 数据交互

    数据交互 一.http协议 基本特点 1.无状态的协议 2.连接过程:发送连接请求.响应接受.发送请求 3.消息分两块:头.体 http和https 二.form 基本属性 action——提交到哪儿 ...

  9. 我的Java之旅 第一课 开发环境准备

    1.JDK JDK(Java Development Kit) 是 Java 语言的软件开发工具包(SDK). SE(J2SE),standard edition,标准版,是我们通常用的一个版本,从J ...

  10. Session Cookies随笔

    1.对于Session而言 它是用来保存在服务端的信息,可以用来做登录验证,在后台保存有用信息. 一个浏览器访问一个网站,就是建立一个连接,连接信息是独立的,就是在该建立的连接内,保存到Session ...