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 (数形结合)
这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...
随机推荐
- Struts2之Struts2的下载与安装
Struts2的下载 登陆struts的官网 下载Full Distribution这个选项的struts2的包. 这是Struts2的完整版,里面包括Struts2的实例应用,空实例应用,核心库,源 ...
- 【基础知识】Flex-弹性布局原来如此简单!!
简言 布局的传统解决方案是基于盒状模型,依赖 display + position + float 方式来实现,灵活性较差.2009年,W3C提出了一种新的方案-Flex,Flex是Flexible ...
- 面试必问---HashMap原理分析
一.HashMap的原理 众所周知,HashMap是用来存储Key-Value键值对的一种集合,这个键值对也叫做Entry,而每个Entry都是存储在数组当中,因此这个数组就是HashMap的主干.H ...
- 在wamp集成环境下安装laravel5.2.*框架
虽然官方一直强烈推荐使用homestead,但是这个相对麻烦一点,所以我还是选择使用wamp集成开发环境.还有这里我只讲解windows系统下的安装,其他例如mac或linux就不写了,此文章是面向刚 ...
- vue 在methods中调用mounted中的方法?
首先可以在data中先声明一个变量 比如 isShow=' ' mounted 中 ---> methods 中 ---> this.sureDelBox(item) 直接this调用 ...
- linux的链接工具secure设置字体大小和颜色
- LeetCode & Q121-Best Time to Buy and Sell Stock-Easy
Array DP Description: Say you have an array for which the ith element is the price of a given stock ...
- Javascript 装饰器极速指南
pablo.png Decorators 是ES7中添加的JavaScript新特性.熟悉Typescript的同学应该更早的接触到这个特性,TypeScript早些时候已经支持Decorators的 ...
- c# 几种深拷贝方式的比较
public static class Tools { //利用 BinaryFormatter 实现深拷贝 public static T DeepCopyByBinary<T>(thi ...
- Docker学习笔记 - Docker的基本概念
一.cs架构 Docker客户端:本地或远程 Docker服务端:守护进程Docker Daemon 二.基本概念 Docker镜像:打包阶段,层叠的只读文件系统,引导->root(ubuntu ...