Title Description
There are 100 apples and bananas lined up in a straight line, in which there are N bananas, you can use up to M magic props to turn the bananas into apples, and finally the "longest number of consecutive apples" is your current apple!DigestGiven an arrangement of apples and bananas, find the maximum score you can get.
importation
The first row is an integer T (1 <= T <= 10) representing the number of groups of test data. The first row of each test data is 2 integers N and M (0 <= N, M <= 100). The second row contains N integers a1, a2, ... aN(1 <= a1 < a2 < ... < aN <= 100), representing the a1th, a2th, ... aN positions are placed with bananas.
exports
For each set of data, output the maximum score you can get by using magic props.
example
importation
3 5 1 34 77 82 83 84 5 2 10 30 55 56 90 5 10 10 30 55 56 90
exports
76 59 100
source (of information etc)
Electronics Institute Level 3 202103 Real Questions
-
#include<bits/stdc++.h>
-
using namespace std;
-
//Calculate maximum length
-
void fun(int n,int m,int a[]){
-
int ma=0;
-
if(m>=n)ma=100;//If the number of props is greater than the number of bananas, all the bananas can be turned into apples, no need to calculate, direct result100
-
else {
-
int s;
-
for(int i=m+1;i<=n+1;i++){//3 2 10 50 90
-
s=a[i]-1-a[i-m-1];
-
if(s>ma)ma=s;
-
}
-
}
-
cout<<ma<<endl;
-
}
-
int main(){
-
int t,n,m,i,j;//n bananas, m props, t sets of data
-
cin>>t;
-
for(i=1;i<=t;i++){
-
cin>>n>>m;
-
int a[102]={0};
-
for(j=1;j<=n;j++)cin>>a[j];//position of the banana
-
a[0]=0;//Add a banana at the top and at the end
-
a[n+1]=101;
-
fun(n,m,a);
-
}
-
return 0;
-
}