嘟嘟嘟




看到比值,就想到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. Algolia使用教程 , 超详细傻子看都会

    框架描述 发现网上Algolia这块的资料较少,就花了点时间从官网上整理了下,总结了几项常用的功能用法. 现在比较有名的Algolia提供了云搜索的服务.具体办法是我们将数据库的信息以JSON的格式上 ...

  2. vi/vim使用

    移动光标上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以单词向前移动 nw:光标向前 ...

  3. T-SQL:谓词和运算符(六)

    谓词一般有 where和having,check  谓词只计算 TRUE ,FALSE或者UNKNOWN  逻辑表达式  如 AND 和OR 1.IN 谓词的用法 SELECT orderid, em ...

  4. 【github&&git】7、gitignore 修改不起作用

    在git使用过程中有时会遇到修改了.gitignore文件,修改了之后发现,不能起作用,这是因为git存在缓存问题,所以做一下步骤即可: git rm -r --cached . git add . ...

  5. html页面边框的另一种写法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 《Head First设计模式》批注系列(一)——观察者设计模式

    最近在读<Head First设计模式>一书,此系列会引用源书内容,但文章内容会更加直接,以及加入一些自己的理解. 观察者模式(有时又被称为模型-视图(View)模式.源-收听者(List ...

  7. EmitMapper的使用小结

    最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了EmitMapper相对其他映射框架处理速度可以更快,就拿来用了.下面是代码中常用的 ...

  8. js之跑马灯广告

    目标效果:每过1秒重复把广告的第一个字符放到最后,达到动态跑马灯效果 代码如下: <!DOCTYPE html> <html lang="en"> < ...

  9. 洛谷P2447 [SDOI2010]外星千足虫(异或方程组)

    题意 题目链接 Sol 异或高斯消元的板子题. bitset优化一下,复杂度\(O(\frac{nm}{32})\) 找最优解可以考虑高斯消元的过程,因为异或的特殊性质,每次向下找的时候找到第一个1然 ...

  10. 访问WEB-INF下的jsp页面

      访问web-inf下的jsp文件, 1)使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <!--jsp视图解析器--> <bean class ...