UVa 1451 Average 解题报告(斜率优化)

来源:互联网 发布:软件需求调查问卷 编辑:程序博客网 时间:2024/04/29 21:35

1451 - Average

Time limit: 3.000 seconds

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         11111111Index12345678901234567Sequence00101011011011010


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 leastL 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 integersn (1$ \le$n$ \le$100, 000) and L (1$ \le$L$ \le$1, 000) which are the length of a binary sequence and a length lower bound, respectively. In the next line, a string, binary sequence, of lengthn is given.

Output 

Your program is to write to standard output. Print the starting and ending index of the subsequence.

The following shows sample input and output for two test cases.

Sample Input 

2 17 5 00101011011011010 20 4 11100111100111110000

Sample Output 

7 11 6 9

    解题报告: 斜率优化裸题。一直听说过斜率优化这个概念,但是没有具体的学过,今天也是看论文和代码才了解了。

    论文可以看这个:《浅谈数形结合思想在信息学竞赛中的应用》

    详细的看一遍论文,然后对照着代码看就好了。代码如下:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <string>using namespace std;#define ff(i, n) for(int i=0;i<(n);i++)#define fff(i, n, m) for(int i=(n);i<=(m);i++)#define dff(i, n, m) for(int i=(n);i>=(m);i--)#define mem(a) memset((a), 0, sizeof(a))typedef long long LL;typedef unsigned long long ULL;void work();int main(){#ifdef ACM    freopen("in.txt", "r", stdin);//    freopen("in.txt", "w", stdout);#endif // ACM    work();}/*****************************************/const int maxn = 111111;int  sum[maxn];int  que[maxn];char str[maxn];double getK(int a, int b){    return (sum[b]-sum[a]+0.0)/(b-a);}void work(){    int T;    scanf("%d", &T);    fff(cas, 1, T)    {        int n, l;        scanf("%d%d%s", &n, &l, str+1);        fff(i, 1, n)            sum[i] = sum[i-1]+(str[i]-'0');        int len = l;        double ans = getK(0, l);        int sta = 0, end = l;        int top = -1, bot = 0;        fff(i, l, n)        {            int last = i - l;            while(bot < top && getK(que[top], last) <= getK(que[top-1], que[top]))                top--;            que[++top] = last;            while(bot < top && getK(que[bot], i) <= getK(que[bot+1], i))                bot++;            double k = getK(que[bot], i);            if(ans < k || (ans == k && len > i - que[bot]))            {                ans = k;                sta = que[bot];                end = i;                len = i - que[bot];            }        }        printf("%d %d\n", sta + 1, end);    }}


0 0
原创粉丝点击