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 ...
随机推荐
- [HAOI 2015]按位或
Description 题库链接 刚开始你有一个数字 \(0\) ,每一秒钟你会随机选择一个 \([0,2^n-1]\) 的数字,与你手上的数字进行或( \(\text{or}\) )操作.选择数字 ...
- openssl签署和自签署证书的多种实现方式
openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.采用自定义配置文件的实现方法 1.1 自建CA 自建CA的机制:1.生成 ...
- mui 实用封装销毁页面
/* * 描述:页面销毁封装 * 说明:针对订单特殊定位页面 * 返回首页,页面空白前进行销毁页面处理 * 使用:plusReady之后 */ (function(w) { var destructi ...
- [javaEE] HTTP协议总结
OSI网络7层协议 物理层 ==> 数据链路层 ==> 网络层ip ==> 传输层 TCP UDP ==>会话层 ==>表示层 ==> 应用层 HTTP FTP S ...
- scala简单入门_wordCount
scala的语法写起来是非常的舒服的,相比java来说,简便许多.而Java在scala面前就显的略微有些笨重了. 接下来我们看一下scala版的wordcount import scala.io.S ...
- yum 安装 php5.6.36
PHP安装测试可以 rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm; rp ...
- JS实现页面分享代码share,不需要第三方接口
在开发一个页面的时候常常会有这么一个小功能,就是分享该页面中的信息. 常见的分享代码有百度分享,JiaThis分享插件(现在貌似不能用了),bshare分享插件等,我主要分享一下自定义分享代码,如下: ...
- loadrunner 运行脚本-Run-time Settings-ContentCheck简单设置
运行脚本-Run-time Settings-ContentCheck简单设置 by:授客 QQ:1033553122 ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测 ...
- SQLite保存报错sqlite.SQLiteConstraintException: UNIQUE constraint failed: ······ code 1555
往数据库里保存数据的时候报错,用的afinal框架,明明在save操作之前执行了一遍deleteAll操作,还是报错. 百度了一下说报这种错有两种情况:一是定义的字段为not null ,插入时对应的 ...
- Android 时间与日期操作类
获取本地日期与时间 public String getCalendar() { @SuppressLint("SimpleDateFormat") SimpleDateFormat ...