UVa 1451 Average 解题报告(斜率优化)
2014-07-19 18:03
344 查看
1451 - Average
Time limit: 3.000 secondsA 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 |
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 ofT 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
n
100,
000) and L (1
L
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);
}
}
相关文章推荐
- Average UVA - 1451 斜率优化
- UVa 1451 Average——斜率优化
- UVa 1451 Average - 斜率优化
- UVA 1451 Average(斜率优化)
- UVA 1451 Average 斜率优化
- UVa 1451 Average (斜率优化)
- UVA 1451 Average平均值 (数形结合,斜率优化)
- 斜率优化,数形结合(Average,uva 1451)
- UVaLive LA 4726 UVa 1451 - Average (子序列最大平均数 数形结合 斜率优化 单调队列)
- UVALive 4726 Average 单调队列+斜率优化
- POJ 2018 Best Cow Fences + UVA Live 4726 Average 斜率优化DP
- HDU 3045 DP斜率优化 解题报告
- UVALive - 4726 Average (斜率优化+单调队列)
- HDU 3480 DP斜率优化 解题报告
- HDU 3507 DP斜率优化 解题报告
- (dp的斜率优化)玩具装箱 解题报告
- HDU 2829 DP斜率优化 解题报告
- UVA - 1451 Average 单调队列 (数形结合-更新最大斜率)
- UVa 1326 Jurassic Remains 解题报告(Meet-in-the-Middle)
- uva10130解题报告