UVa1451 - 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 finding 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 finding, 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.
Position 1 1 1 1 1 1 1 1
Index 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7
Sequence 0 0 1 0 1 0 1 1 0 1 1 0 1 1 0 1 0
For the binary sequence above, if the length lower bound is 7, the maximum average is 6/8 which
happens in the subsequence [7,14]. Its length is 8, which is greater than the length lower bound 7. If the length lower bound is 5, then the subsequence [7,11] gives the maximum average 4/5. The length is 5 which is equal to the length lower bound. For the subsequence [7,11], 7 is its starting index and 11 is its ending index.
Given a binary sequence and a length lower bound L, write a program to find a subsequence of the binary sequence whose length is at least L and whose average is maximum over all subsequences of the binary sequence. If two or more subsequences have the maximum average, then find the shortest one; and if two or more shortest subsequences with the maximum average exist, then find the one with the smallest starting index.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases
T
is given in the first line of the input. Each test case starts with a line containing two integers
Output
Your program is to write to standard output. Print the starting and ending index of the subsequence.
Sample Input
2
17 5
00101011011011010
20 4
11100111100111110000
Sample Output
7 11
6 9
题意
给出一个01串,要求找到一个长度最小为L且均值最大的子串。
题解
首先预处理出前缀和s,令
s[0]=0
,以
A
为横坐标,s为纵坐标画图,i到j的平均值
代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define fout freopen("out.txt","w",stdout)
#define fin freopen("in.txt","r",stdin)
using namespace std;
int T;
int n,L;
int a[100005],s[100005];
int P[100005];
int ansl,ansr;
int cmp(int x1,int x2,int x3,int x4)
{
return (s[x2]-s[x1])*(x4-x3)-(s[x4]-s[x3])*(x2-x1);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&L);
s[0]=a[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%1d",&a[i]);
s[i]=s[i-1]+a[i];
}
ansl=0;
ansr=n;
int i=0,j=0;
for(int t=L;t<=n;t++)
{
while(j-i>1&&cmp(P[j-2],t-L,P[j-1],t-L)>=0)
j--;
P[j++]=t-L;
while(i<j-1&&cmp(P[i],t,P[i+1],t)<=0)
i++;
int c=cmp(P[i],t,ansl,ansr);
if(c>0||(c==0&&ansr-ansl>t-P[i]))
{
ansl=P[i];
ansr=t;
}
}
printf("%d %d\n",ansl+1,ansr);
}
return 0;
}