Problem G: G. Interference Signal
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 17 Solved: 16
[ Submit][ Status][ Web Board]
Description
Dr.Kong’s laboratory monitor some interference signals. The interference signals can be digitized into a series of positive integer. May be, there are N integers a1,a2,…,an.
Dr.Kong wants to know the average strength of a contiguous interference signal block. the block must contain at least M integers.
Please help Dr.Kong to calculate the maximum average strength, given the constraint.
Input
The input contains K test cases. Each test case specifies:
* Line 1: Two space-separated integers, N and M.
* Lines2~line N+1: ai (i=1,2,…,N)
1 ≤ K≤ 8, 5 ≤ N≤ 2000, 1 ≤ M ≤ N, 0 ≤ ai ≤9999
Output
For each test case generate a single line containing a single integer that is 1000 times the maximal average value. Do not perform rounding.
Sample Input
2
10 6
6
4
2
10
3
8
5
9
4
1
5 2
10
3
8
5
9
10 6
6
4
2
10
3
8
5
9
4
1
5 2
10
3
8
5
9
Sample Output
6500
7333
7333
代码如下:
#include<iostream> #include<stdio.h> using namespace std; double A[2000], mk; int N, M; void FindmkAver(double sum, int x,int num) { if (x == N)return; if (num >= M ) if (((A[x] + sum)/ num) > mk) mk = (A[x] + sum) / num; FindmkAver(sum + A[x], x + 1, num + 1); FindmkAver(0, x + 1, 1); } int main() { int K; cin >> K; while (K--) { mk = 0; cin >> N >> M; for (int i = 0; i < N; i++) cin >> A[i]; for (int j = 0; j < N; j++) FindmkAver(0, j, 1); printf("%.f\n", mk*1000); } return 0; }