UVA1451 Average
嘟嘟嘟
看到比值,就想到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的更多相关文章
- 【斜率优化】Average
[UVa1451]Average 算法竞赛入门经典第8章8-9 ( P243 ) 题目大意:给定一个长度为N的01串,选择一个长度至少为L的连续子串,使序列平均值最大 (N<=100001) 题 ...
- training 2
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU ...
- [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 ...
- Average Precision of VOC
转一篇文章,主要是关于VOC中Average Precision指标的 原文出处:https://sanchom.wordpress.com/tag/average-precision/ 还有一篇文章 ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- average slice
A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)
在Linq中有一些这样的操作,根据集合计算某一单一值,比如集合的最大值,最小值,平均值等等.Linq中包含7种操作,这7种操作被称作聚合操作. 1.Count操作,计算序列中元素的个数,或者计算满足一 ...
- Load Average
在Linux系统下面,有很多的命令可以查看系统的负载情况:比如top,uptime,w,示例如下: [wenchao.ren@l-cmsweb1.ops.cn1 ~]$ w 18:39:10 up 7 ...
随机推荐
- SpringMVC教程1
一.SpringMVC介绍 1.MVC介绍 ==模型-视图-控制器(MVC== 是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型.视图及控制器在应用程序中的角色将业务逻辑从界面中 ...
- netty源码解解析(4.0)-13 Channel NIO实现: 关闭和清理
Channel提供了3个方法用来实现关闭清理功能:disconnect,close,deregister.本章重点分析这个3个方法的功能的NIO实现. disconnect实现: 断开连接 disco ...
- Hive基础之Hive与关系型数据库的比较
Hive与关系型数据库的比较 使用Hive的CTL(命令行接口)时,你会感觉它很像是在操作关系型数据库,但是实际上,Hive和关系型数据库有很大的不同. 1)Hive和关系型数据库 ...
- VPS杂谈(一)
1. VPS购买推荐 可参考:http://www.laozuo.org/myvps 2. VPS配置SSH端口号 购买的VPS的主机,一般情况下端口号不是22,被改成了其它的,这个时候为了方便自己的 ...
- 配置hadoop-eclipse-plugin(版本hadoop2.7.3):
配置hadoop-eclipse-plugin(版本hadoop2.7.3): 1:首先下载我们需要的 hadoop-eclipse-plugin-2.7.3.jar,winutils.exe 和 ...
- mysql 8小时超时设置
1.打开MySQL配置文件 2.添加 interactive_timeout=31536000wait_timeout=31536000 3.重新启动服务 打开MySQL命令行界面查看设置是否成功
- 关于Unsupported major.minor version 52.0解决办法(再次回顾)
对于web项目的配置问题,在很大程度上,tomcat的版本问题起到很大的决定性因素,例如以上问题:Unsupported major.minor version 52.0 表示stanford par ...
- 【Mybatis】1、Mybatis拦截器学习资料汇总
MyBatis拦截器原理探究 http://www.cnblogs.com/fangjian0423/p/mybatis-interceptor.html [myBatis]Mybatis中的拦截器 ...
- Java中,类与类,类中的代码执行顺序
代码的执行顺序如下: 1.一个类的静态代码块.构造代码块.构造方法的执行流程为: 静态代码块 > 构造代码块 > 构造方法 2.静态的内容是随着类的加载而加载,静态代码块的内容会优先执行 ...
- JavaScript中的递归
译者按: 程序员应该知道递归,但是你真的知道是怎么回事么? 原文: All About Recursion, PTC, TCO and STC in JavaScript 译者: Fundebug 为 ...