24h購物| | PChome| 登入
2014-03-02 21:17:16| 人氣2,423| 回應0 | 上一篇 | 下一篇

[UVA][最大平均值問題] 1451 - Average

推薦 0 收藏 0 轉貼0 訂閱站台

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


轉換成 01 字串,問題等價求最大平均值問題,

最大平均值問題,使用單調&斜率優化的方式去解,
證明方式轉換成幾何問題。

效率 O(n)

證明請網路搜一下這篇論文 算法合集之《浅谈数形结合思想在信息学竞赛中的应用》


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
double get_avg(int l, int r, int sum[]) {
return (double)(sum[r]-sum[l])/(r-l);
}
int q[100005], sum[100005];
double maxAvgProblem(int n, int L, int sum[]) {
double ans = (double)sum[n]/n;
int front, rear, i, j;
int retL = 0, retR = n;
front = 0, rear = -1;
for(i = L; i <= n; i++) {
int tmp = i-L;
while(front < rear && get_avg(q[rear], tmp, sum) <= get_avg(q[rear-1], q[rear], sum))
rear--;
q[++rear] = tmp;
while(front < rear && get_avg(q[front], i, sum) <= get_avg(q[front+1], i, sum))
front++;
double tans = get_avg(q[front], i, sum);
if(tans > ans)
ans = tans, retL = q[front], retR = i;
if(tans == ans && i - q[front] < retR - retL)
retL = q[front], retR = i;
}
printf("%d %d\n", retL + 1, retR);
return ans;
}
int main() {
int testcase, n, L;
int i, j, k;
char s[100005];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &n, &L);
scanf("%s", s+1);
for(i = 1, sum[0] = 0; i <= n; i++)
sum[i] = sum[i-1] + (s[i] == '1');
maxAvgProblem(n, L, sum);
}
return 0;
}

台長: Morris
人氣(2,423) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][歐拉函數] 11317 - GCD+LCM
此分類上一篇:[UVA][圓周角] 12395 - Regular Convex Polygon

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文