您的位置:首页 > 其它

1046. Plane Spotting

2013-08-25 19:51 453 查看
                                             1046. Plane SpottingDescriptionCraig 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 theoptimal times are for his plane spotting. Together with some friends he has collected statistics of the number of passing planes in consecutive periods of fifteen minutes (which for obvious reasons we shall call ‘quarters’). In order to plan his trips as efficientlyas 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 timeperiods 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 optimalperiods. If not enough time periods exist which meet requirement 1, you should give only the allowed time periods.InputThe 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 wantsto 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.OutputThe 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 Copy sample input to clipboard
310 5 51 5 0 2 1 4 2 5 0 210 3 510 3 1 4 2 6 3 0 8 05 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
题目分析:题意    给出一个长度为N的非负整数序列(所有数不超过200),还有两个整数M和K,求前M个最优的长度不小于K的连续子序列。    连续子序列的优先级如何比较   平均值大的序列优于平均值小的   长度大的序列优于长度小的   结束位置靠前的序列优于结束位置靠后的1≤N≤300,1≤M≤100,1≤K≤300思路1.使用结构体表示一个子序列,重写比较操作“<”。2.主要是是一个个,满足条件的序列的列举,然后比较。3.首先必须选min_q个元素,从第一个数开始数min_q个元素,求和;接着从第二个数开始把后面min_q个元素累加求和,第三,第四。。。   接着是选择min_q+1个元素,min_q+2,min_q+3,min_q+4。。个   将所得的序列排序,输出前面out_q个
#include<iostream>#include<vector>#include<algorithm>using namespace std;struct Node{int begin;int end;int sum;double avg;};bool cmp(Node a,Node b){if(a.avg!=b.avg) return a.avg > b.avg;else if ((a.end-a.begin)!=(b.end-b.begin)) return (a.end-a.begin) > (b.end-b.begin);else if(a.end!=b.end) return a.end < b.end;return false;}int main(){int time;int all_q,out_q,min_q;int count=0;cin>>time;while(time--){int a[305];int i;count++;vector<Node>ve;ve.clear();cin>>all_q>>out_q>>min_q;a866for(i=1;i<=all_q;i++){cin>>a[i];}for(i=min_q;i <= all_q;i++) //一开始选定 i 个 元素 ,逐步增加{for(int j=1;j+i-1 <= all_q;j++) //从 J 开始 ,每次至少加 i-1 个{Node noe;noe.begin=j;noe.end=j+i-1;noe.sum=0;for(int k=0;k < i;k++) //求这段队列里面的总数{noe.sum+=a[j+k];}noe.avg=1.0*noe.sum/i;ve.push_back(noe);}}sort(ve.begin(),ve.end(),cmp);cout<<"Result for run "<<count<<":"<<endl;for(int x=0;x < out_q && x < ve.size();x++){cout<<ve[x].begin<<"-"<<ve[x].end<<endl;}}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sicily