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的更多相关文章

  1. 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 ...

  2. UVA 1451 Average平均值 (数形结合,斜率优化)

    摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...

  3. UVA - 1451 Average (斜率优化)

    题意:由01组成的长度为n的子串,AT由0表示,GC由1表示,求一段长度大于等于L且GC率最高的子串的起始终止坐标,若GC率相同,取长度较小,若长度相同,取起始坐标最小. 分析: 1.一个子串(i+1 ...

  4. 【UVA 1451】Average

    题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...

  5. UVa 1451 (数形结合 单调栈) Average

    题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...

  6. uva 1451 数形结合

    思路:枚举点t,寻找满足条件的点t': 计sum[i]为前i项合,平均值即为sum[t]-sum[t'-1]/t-t'+1 设(Pi=(i,Si),表示点在s中的位置,那么就可以画出坐标图,问题就转化 ...

  7. UVa 1451 平均值

    https://vjudge.net/problem/UVA-1451 题意:给定长度为n的01串,选一个长度至少为L的连续子串,使得子串中数字的平均值最大. 思路:这题需要数形结合,真的是很灵活. ...

  8. 1451 - Average 高速求平均值

    怎样高速求取一段区间的平均值 用前缀的思想来看 很easy 可是 本题题意要求的是 大于等于一段长度的区间的平均值的最大值 并且给出的数据范围非常大 O(n*L)的直白比較算法 用于解决此问题不合适 ...

  9. 紫书 例题8-9 UVa 1451 (数形结合)

    这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...

随机推荐

  1. DES MEI号码加密

    对于加密来说,使用DES加密解密很好,但是为了使不同设备的密文不一样,可以使用 固定字符串 + 设备IMEI号码 加密的方式,这样可以分辨不同手机,限制手机的使用(若是注册,一个手机只有一个IMEI号 ...

  2. 1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    解决问题:windows下:修改my.ini 在[mysqld]内加入secure_file_priv = linux下:修改my.cnf 在[mysqld]内加入secure_file_priv = ...

  3. 阿里云API网关(15)监控预警

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  4. 2017年Unity游戏开发视频教程(入门到精通)

    本文是我发布的一个Unity游戏开发的学习目录,以后我会持续发布一系列的游戏开发教程,都会更新在这个页面上,适合人群有下面的几种: 想要做独立游戏的人 想要找游戏开发相关工作的人 对游戏开发感兴趣的人 ...

  5. cache和buffer

    一.free命令是Linux查看内存使用情况的命令 1. centos 7风格 [root@bogon init.d]# free -m total used free shared buff/cac ...

  6. SublimeText用FileHeader给代码文件生成头部注释

    https://github.com/shiyanhui/FileHeader 修改的模板在这个路径下:C:\Users\[USERNAME]\AppData\Roaming\Sublime Text ...

  7. 在删除一个指针之后,一定将该指针设置成空指针(即在delete *p之后一定要加上: p=NULL)

    在删除一个指针之后,一定将该指针设置成空指针(即在delete *p之后一定要加上: p=NULL)

  8. enumerate给列表加索引

    >>> list = ['a','b','c'] >>> for i,j in enumerate(list): print(i,j) 0 a 1 b 2 c &g ...

  9. 在CentOS 7+ 安装Kubernetes入门

    TL;DR; 科学上网,科学上网,科学上网,重要的事情说三次.如果不会科学上网,这篇文章就没有看下去的意义.作为一个技术人员如果不愿意折腾,很难有所作为.作为一个单纯的技术人员,最好把心思放在技术上, ...

  10. mysql乱码配置

    1.进入mysql   show variables like "char%"   2.在/etc/mysql/my.cnf中增加以下内容   [client] default-c ...