问题 G. Interference Signal

题目描述

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.

输入

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

输出

For each test case generate a single line containing a single integer that is 1000 times the maximal average value. Do not perform rounding.

样例输入

2
10 6
6
4
2
10
3
8
5
9
4
1
5 2
10
3
8
5
9

样例输出

6500
7333

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
int x[2100];
int main()
{
	int k,n,m,t;
	cin>>k;
	double av,ma=-1,sum;
	while(k--)
	{
		cin>>n>>m;
		for(int i=0;i<n;i++) scanf("%d",&x[i]);
		ma=-1;
		for(int i=0;i<n;i++)
		{
			sum=0; t=0;
			for(int j=i;j<n;j++)
			{
				sum+=x[j]; t++;
				if(t>=m)
				{
					av=sum/t;
					ma=max(ma,av);	
				}
			}
		}
		printf("%.lf\n",ma*1000);
	}
	return 0;
}
posted @ 2021-05-03 17:36  斯文~  阅读(37)  评论(0)    收藏  举报

你好!