河南省第八届ACM大学生程序设计大赛
G.Interference Signal
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
the maximum average
Sample Input
2
10 6
6
4
2
10
3
8
5
9
4
1
5 2
10
3
8
5
9
Sample Output
6500
7333
Source
比赛的时候一直WA,WA到比赛结束都不知道错在了哪,出来后小伙伴们说当时也WA,不知道错哪了,最后抱着试一试的心态,用%d输出,居然A了,太坑了。。 不能用%.f输出,只能%d,这次算是记住了。其实思路很简单,就暴力。。 枚举出所有的情况找最大的就好了,坑点就是输出!!!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {
int k;
scanf("%d", &k);
while(k--){
int n, m;
int a[2010];
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
double max = 0, sum = 0;
double avg, c = 0;
for(int i = 0; i+m <= n; i++){
sum = 0;
c = 0;
for(int j = i; j < n; j++){
if(c<m){
sum+=a[j];
c+=1;
}
else{
avg = sum/c;
if(avg > max)
max = avg;
sum+=a[j];
c+=1;
if(j==n-1){
avg = sum/c;
if(avg > max)
max = avg;
}
}
}
if(c == m){
avg = sum/c;
if(avg > max) max = avg;
}
}
int im = (int)(max*1000);
printf("%d\n", im);
}
return 0;
}