The main idea: input an integer t, indicating that there is a T Group test sample. Each test sample sample first input three integers, is n (1≤n≤300) represents the moment of N observation aircraft, L (1≤L≤100) represents the highest L group in the output result, M (1≤m≤300) represents the minimum interval number of time interval. then enter n integers, representing the number of aircraft that can be observed at each moment, as Pi (1≤i≤n). The L-group interval with the largest number of aircraft can be observed in the interval with the minimum interval requirement.
Other conditions:
The conditions for the interval pi to be better than PJ are:
1.pi interval Average time number of aircraft extra PJ interval
2. If the average number of aircraft is the same, the Pi interval greater than the PJ interval is required
3. If the average number of aircraft in the same write, the interval size is the same, you need pi than PJ early completion.
Output format:
First output "Result for Run #:", where # represents the first set of sample samples, starting from 1.
Then output the maximum L group interval, "{from}-{to}", {from} indicates the interval, and {to} indicates the end of the interval.
The idea of solving problems: first select all the intervals that meet the minimum time requirement, and then compare each interval in order. The last highest group of L results, if the required interval is not sufficient, the entire order is output sequentially.
Considering the interval and, think of the prefix and, this is a small optimization.
The code is as follows:
1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5 6 Const intMAXN =305;7 intARR[MAXN];8 intSUM[MAXN];9 Ten structInterval { One int from; A intto ; -Interval (intf =0,intt =0) { - from=F; theto =T; - } - }; - + BOOLCmpfunc (ConstInterval & I1,ConstInterval &I2) { - BOOLswap; + DoubleS1 = (sum[i1.to]-sum[i1. from-1] +0.0)/(I1.TO-I1. from+1); A DoubleS2 = (sum[i2.to]-sum[i2. from-1] +0.0)/(I2.to-i2. from+1); at if(S1 >S2) { -Swap =false; -}Else if(S1 <S2) { -Swap =true; -}Else { - intL1 = I1.to-i1. from+1; in intL2 = I2.to-i2. from+1; - if(L1 >L2) { toSwap =false; +}Else if(L1 <L2) { -Swap =true; the}Else { * if(I1.to >i2.to) { $Swap =true;Panax Notoginseng}Else if(I1.to <i2.to) { -Swap =false; the}Else { +cout <<"Error this time"<<Endl; A } the } + } - return!swap; $ } $ - intMain () { - intt, N, L, M; the -CIN >>T;Wuyi for(inttt =1; tt <= t; tt++) { theVector<interval>VT; -CIN >> N >> L >>m; Wu - //get the number of planes per hour Aboutarr[0] = sum[0] =0; $ for(inti =1; I <= N; i++) { -CIN >>Arr[i]; -Sum[i] =0; - } A + //computes the prefix and the for(inti =1; I <= N; i++) { -Sum[i] = sum[i-1] +Arr[i]; $ } the the //get all the possible intervals the for(inti =1; i + M-1<= N; i++) { the for(intj = i + M-1; J <= N; J + +) { - Vt.push_back (Interval (i, J)); in } the } the About //sort the interval information the sort (Vt.begin (), Vt.end (), cmpfunc); the the //Output Results +cout <<"Result for Run"<< TT <<":"<<Endl; - intc = l > vt.size ()?vt.size (): l; the for(inti =0; I < C; i++) {Bayicout << Vt[i]. from<<"-"<< vt[i].to <<Endl; the } the - } - the return 0; the}
SOJ 1046 Plane Spotting