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

56 篇文章 0 订阅
该博客介绍了UVa 1451题目的解法,涉及DNA序列的GC-ratio(即A/T与C/G的比例),并将其转化为二进制序列求平均值的问题。博主分享了如何利用斜率优化来寻找满足长度条件的子序列,以达到最大平均值。解题报告中推荐了一篇相关论文,并给出了代码示例。
摘要由CSDN通过智能技术生成

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         11111111
Index12345678901234567
Sequence00101011011011010


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 n (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 length n 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);
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值