web123456

Apple Fun in c++

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //Calculate maximum length
  4. void fun(int n,int m,int a[]){
  5. int ma=0;
  6. 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
  7. else {
  8. int s;
  9. for(int i=m+1;i<=n+1;i++){//3 2 10 50 90
  10. s=a[i]-1-a[i-m-1];
  11. if(s>ma)ma=s;
  12. }
  13. }
  14. cout<<ma<<endl;
  15. }
  16. int main(){
  17. int t,n,m,i,j;//n bananas, m props, t sets of data
  18. cin>>t;
  19. for(i=1;i<=t;i++){
  20. cin>>n>>m;
  21. int a[102]={0};
  22. for(j=1;j<=n;j++)cin>>a[j];//position of the banana
  23. a[0]=0;//Add a banana at the top and at the end
  24. a[n+1]=101;
  25. fun(n,m,a);
  26. }
  27. return 0;
  28. }