题意:

给定一个01序列,选一个长度至少为L 的连续子序列使其平均值最大;输出这个子序列的起点和终点;如果有多个答案,输出长度最小的,还有多个就输出第一个编号最小的;

思路:

用sum[i]表示[1,i]的和;题目的平均值就可以变成(sum[i]-sum[j-1])/(i-(j-1));

问题也变成求横坐标的距离至少为L的两点连线斜率最大的那两点的横坐标是多少?

对于每个点作为横坐标较大的点,判断横坐标距离最少为L的点,指针r维护这些点是一个下凸线,指针l维护与当前点斜率最大点;

复杂度是O(n)的,具体的解释分析可见《浅谈数形结合在信息学竞赛中的应用》;

AC代码:

#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-10; char s[N];
int sum[N],a[N]; int main()
{
int t ;
read(t);
while(t--)
{
int n,L;
read(n);read(L);
scanf("%s",s);
For(i,0,n-1)
sum[i+1]=sum[i]+s[i]-'0';
//For(i,1,n)cout<<sum[i]<<" ";
int r = -1,l = 0,ans1=0,ans2=L,x=sum[L],y=L;
For(i,L,n)
{
while(r>l)
{
if((sum[i-L]-sum[a[r]])*(a[r]-a[r-1])<=(sum[a[r]]-sum[a[r-1]])*(i-L-a[r]))r--;
else break;
}
a[++r]=i-L;
while(l<r)
{
if((sum[i]-sum[a[l+1]])*(i-a[l])>=(sum[i]-sum[a[l]])*(i-a[l+1]))l++;
else break;
}
if((sum[i]-sum[a[l]])*y>x*(i-a[l]))
{
x=sum[i]-sum[a[l]];
y=i-a[l];
ans1=a[l];
ans2=i;
}
else if((sum[i]-sum[a[l]])*y==x*(i-a[l]))
{
if(i-a[l]<y)
{
x=sum[i]-sum[a[l]];
y=i-a[l];
ans1=a[l];
ans2=i;
}
}
//cout<<i<<" "<<l<<" "<<r<<"@@"<<endl;
}
cout<<ans1+1<<" "<<ans2<<"\n";
}
return 0;
}

  

最新文章

  1. div的打开与关闭js
  2. c/c++常用代码 -- 共享内存
  3. C++ primer里的template用法
  4. 日志分析(二) logstash patterns
  5. poj 3249 Test for Job (记忆化深搜)
  6. js中时间戳与日期转换-js日期操作
  7. VS2015 开发人员命令提示,如何实现记事本编程
  8. 关于vs code的个人配置
  9. HDU 6170----Two strings(DP)
  10. Milking Time
  11. ES学习
  12. 【51nod1847】 奇怪的数学题
  13. Spring boot Mybatis 整合(完整版)
  14. [译]bootstrap-select (selectpicker)方法
  15. Kubernetes学习
  16. CSS知识点集锦
  17. 深入理解Linux内核-内存管理
  18. TCP 三次握手 四次握手
  19. [VSTO] warning CS0467 解决方案
  20. Vue.js学习笔记 第七篇 表单控件绑定

热门文章

  1. R语言入门视频笔记--6--R函数之cat、format、switch函数
  2. BZOJ 2957 楼房重建 (线段树)
  3. transition、animation在macbook air上图片动画边缘抖动2
  4. Delphi GDI对象之绘制文本
  5. Linux上利用NFS实现远程挂载
  6. 谷歌訪问之直接输入ip地址
  7. 具体解释window.history
  8. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part3
  9. MySQL之常见问题总结
  10. BZOJ 3363 POJ 1985 Cow Marathon 树的直径