Constraints
Time Limit: 1 secs, memory limit: 32 MB
Description
Craig is fond of planes. making photographs of planes forms a major part of his daily life. since he tries to stimulate his social life, and since it's quite a drive from his home to the airport, Craig tries to be very efficient by investigating what the optimal
Times are for his plane spotting. together with some friends he has collected statistics of the number of passing planes in consecutive periods of specified teen minutes (which for obvious reasons we shall call 'quarters '). in order to plan his trips as efficiently
As possible, he is interested in the average number of planes over a certain time period. this way he will get the best return for the time invested. furthermore, in order to plan his trips with his other activities, he wants to have a list of possible time
Periods to choose from. these time periods must be ordered such that the most preferable time period is at the top, followed by the next preferable time period, etc. etc. the following rules define which is the order between time periods:
1. A period has to consist of at least a certain number of quarters, since Craig will not drive three hours to be there for just one measly quarter.
2. A period P1 is better than another period P2 if:
* The number of planes per quarter in P1 is higher than in P2;
* The numbers are equal but P1 is a longer period (more quarters );
* The numbers are equal and they are equally long, but period P1 ends earlier.
Now Craig is not a clever programmer, so he needs someone who will write the good stuff: that means you. so, given input consisting of the number of planes per quarter and the requested number of periods, you will calculate the requested list of optimal periods.
If not enough time periods exist which meet requirement 1, you shoshould give only the allowed time periods.
Input
The input starts with a line containing the number of runs n. next follows two lines for each run. the first line contains three numbers: the number of quarters (1-300), the number of requested best periods (1-100) and the minimum number of quarters Craig wants
To spend spotting planes (1-300 ). the SEC-nod line contains one number per quarter, describing for each quarter the observed number of planes. the airport can handle a maximum of 200 planes per quarter.
Output
The output contains the following results for every run:
* A line containing the text "result for run <n>:" where <n> is the index of the run.
* One Line for every requested period: "<F>-<L>" where <F> is first quarter and <L> is the last quarter of the period. the numbering of quarters starts at 1. the output must be ordered such that the most preferable period is at the top.
Sample Input
310 5 51 5 0 2 1 4 2 5 0 2 10 3 510 3 1 4 2 6 3 0 8 0 5 5 51 2 3 4 5
Sample output
Result for run 1:4-82-86-101-82-6Result for run 2:1-61-71-9Result for run 3:1-5
Question Analysis:
The question is quite long. In fact, it is to find the maximum values of a substring according to the requirements. First, we need to calculate the sum of all substrings and put them in an array, and then sort them according to requirements using sort, final output
/**/# Include <iostream> # include <iomanip> # include <stdio. h> # include <cmath> # include <iomanip> # include <list> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> # include <queue> # include <string. h> using namespace STD; typedef struct set {int I; // closed interval Int J; // closed interval int sum;} set; // compare bool CMP (const set & X1, const set & x2) {int size1 = x1.j-x1. I + 1; int size2 = x2.j-x2. I + 1 as required; Double ave1 = (double) x1.sum/(double) size1; double ave2 = (double) x2.sum/(double) size2; If (FABS (ave1-ave2)> 1e-6) return ave1> ave2; If (size1! = Size2) return size1> size2; return x1.j <x2.j;} int main () {int Geshu; CIN> Geshu; For (INT xx = 0; XX <Geshu; XX ++) {int N, outputnum, least; CIN> N> outputnum> least; vector <int> data (n); For (INT I = 0; I <n; I ++) CIN> data [I]; vector <set> matrix (N * n ); // store the substring and for (INT I = 0; I <n; I ++) {matrix [I * n + I]. I = I; matrix [I * n + I]. j = I; matrix [I * n + I]. sum = data [I] ;}for (INT I = 1; I <n; I ++) {for (Int J = 0; j <n-I; j ++) {set TMP = {J, J + I, matrix [J * n + J + I-1]. sum + d ATA [J + I]}; matrix [J * n + J + I] = TMP ;}} // end forcout <"result for Run" <xx + 1 <":" <Endl; sort (matrix. begin (), matrix. end (), CMP); For (INT I = 0; outputnum! = 0 & I <n * n; I ++) {If (Matrix [I]. j-matrix [I]. I + 1> = least) {cout <matrix [I]. I + 1 <"-" <matrix [I]. J + 1 <Endl; outputnum -- ;}}// end}