UVA 1451 Average
A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the
number of Cs and Gs of the sequence divided by the length of the sequence. GC-ratio is important
in gene nding because DNA sequences with relatively high GC-ratios might be good candidates for
the starting parts of genes. Given a very long DNA sequence, researchers are usually interested in
locating a subsequence whose GC-ratio is maximum over all subsequences of the sequence. Since short
subsequences with high GC-ratios are sometimes meaningless in gene nding, a length lower bound is
given to ensure that a long subsequence with high GC-ratio could be found. If, in a DNA sequence,
a 0 is assigned to every A and T and a 1 to every C and G, the DNA sequence is transformed into a
binary sequence of the same length. GC-ratios in the DNA sequence are now equivalent to averages in
the binary sequence.
题目大意:给出一个01序列,求长度至少为L的子序列,使得平均值最大
解题报告:
比较简单,但是花了许久时间,还是太渣.
开始以为是简单贪心,长度固定为L即可,发现这个单调性只有排序后才有QWQ,所以拍WA后改写斜率优化DP:我们要求的是\((sum[i]-sum[j])/(i-j+1)\)最大值,明显对应平面上的斜率,所以直接做就好,注意要把0加进去,调了很久
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1e5+5;const double eps=1e-6;
int a[N],n,m,sum[N],q[N];char s[N];
int fy(int i,int j){
return sum[i]-sum[j];
}
int fx(int i,int j){
return i-j;
}
void work()
{
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=1;i<=n;i++){
a[i]=s[i]-'0',sum[i]=sum[i-1]+a[i];
if(m==1 && a[i]){
printf("%d %d\n",i,i);
return ;
}
}
if(m==1){puts("1 1");return ;}
int l=1,r=0,j,k,L=0,R=m;int tot;
for(int i=m;i<=n;i++){
while(r-l>=1){
j=q[r];k=q[r-1];
if(fy(i-m,j)*fx(i-m,k)<=fy(i-m,k)*fx(i-m,j))r--;
else break;
}
q[++r]=i-m;
while(r-l>=1){
j=q[l+1];k=q[l];
if(fy(i,j)*fx(i,k)>=fy(i,k)*fx(i,j))l++;
else break;
}
tot=fy(i,q[l])*fx(R,L)-fy(R,L)*fx(i,q[l]);
if(tot>0 || (tot==0 && i-q[l]<R-L)){
L=q[l];R=i;
}
}
printf("%d %d\n",L+1,R);
}
int main()
{
int T;cin>>T;
while(T--)work();
return 0;
}
UVA 1451 Average的更多相关文章
- UVa 1451 Average - 斜率优化
A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the numbe ...
- UVA 1451 Average平均值 (数形结合,斜率优化)
摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...
- UVA - 1451 Average (斜率优化)
题意:由01组成的长度为n的子串,AT由0表示,GC由1表示,求一段长度大于等于L且GC率最高的子串的起始终止坐标,若GC率相同,取长度较小,若长度相同,取起始坐标最小. 分析: 1.一个子串(i+1 ...
- 【UVA 1451】Average
题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...
- UVa 1451 (数形结合 单调栈) Average
题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...
- uva 1451 数形结合
思路:枚举点t,寻找满足条件的点t': 计sum[i]为前i项合,平均值即为sum[t]-sum[t'-1]/t-t'+1 设(Pi=(i,Si),表示点在s中的位置,那么就可以画出坐标图,问题就转化 ...
- UVa 1451 平均值
https://vjudge.net/problem/UVA-1451 题意:给定长度为n的01串,选一个长度至少为L的连续子串,使得子串中数字的平均值最大. 思路:这题需要数形结合,真的是很灵活. ...
- 1451 - Average 高速求平均值
怎样高速求取一段区间的平均值 用前缀的思想来看 很easy 可是 本题题意要求的是 大于等于一段长度的区间的平均值的最大值 并且给出的数据范围非常大 O(n*L)的直白比較算法 用于解决此问题不合适 ...
- 紫书 例题8-9 UVa 1451 (数形结合)
这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...
随机推荐
- C简单实现动态顺序表
<span style="font-size:18px;">一下为简单实现:</span> #define SIZE 3; typedef int Data ...
- jav音频格式转换 ffmpeg 微信录音amr转mp3
项目背景: 之前公司开发了一个微信公众号,要求把js-sdk录音文件在web网页也能播放.众所周知,html的<audio>标签ogg,mp3,wav,也有所说苹果safari支持m4a格 ...
- WebApi一个控制器中定义多个Get方法。
问题:怎样解决一个ApiController中定义多个Get方法或者Post方法? 答:要想实现一个ApiController中定义多个Get方法或者Post方法,则需要在WebApiConfig类中 ...
- MySQL/MariaDB中游标的使用
本文目录:1.游标说明2.使用游标3.游标使用示例 1.游标说明 游标,有些地方也称为光标.它的作用是在一个结果集中逐条逐条地获取记录行并操作它们. 例如: 其中select是游标所操作的结果集,游标 ...
- thinkphp中ajax技术
thinkphp可以直接返回json数据,json数据事可以跟前端的js通用的
- token 验证
组件: https://jwt.io/#libraries-io
- Python内置函数(51)——hasattr
英文文档: hasattr(object, name) The arguments are an object and a string. The result is True if the stri ...
- 新概念英语(1-15)Your passports please
Is there a problem wtih the Customers officer? A:Are you Swedish? B:No. We are not. We are Danish. A ...
- Docker学习笔记 - Docker容器与外部网络的连接
学习目的: ip_forward 包过滤防护墙 iptables 允许端口映射访问 限制ip访问容器 1.ip_forward 控制系统是否会转发流量 检查linux系统转发是否开启命令:sysctl ...
- Spring Security入门(2-3)Spring Security 的运行原理 3
关键组件关系 FilterSecurityInterceptor--- authenticationManager --- UserDetailService--- accessDecisionMan ...