sicily 1046 Plane Spotting 快速排序解题
2014-09-25 19:33
344 查看
1046. Plane Spotting
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
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 fifteen 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 should 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
3 10 5 5 1 5 0 2 1 4 2 5 0 2 10 3 5 10 3 1 4 2 6 3 0 8 0 5 5 5 1 2 3 4 5
Sample Output
Result for run 1: 4-8 2-8 6-10 1-8 2-6 Result for run 2: 1-6 1-7 1-9 Result for run 3: 1-5 这道题关键在于理解清楚题意,注意首先需要收集数据,即所有符合要求的时间段period, 从下标0开始依次到下标num_ps-mp,对于所有符合要求的period全部保存在一个Period类型的vector中,Period为自定义的一个结构体,用于提供排序的条件所需的变量,最后用快速排序对该vector进行排序,输出指定的几个最好的period.
#include <iostream> #include <vector> using namespace std; typedef struct period { vector<int> pd; int first; int last; int length; double pq; } Period; void qsort(vector<Period>&, int low, int height); int main() { int cases; int order = 1; cin >> cases; while (order <= cases) { int num_qs, qs, rp, mp; int enough = 0; vector<int> hold_qs; cin >> num_qs >> rp >> mp; vector<Period> periods; for (int i = 0; i < num_qs; i++) { cin >> qs; hold_qs.push_back(qs); } for (int i = 0; i <= num_qs-mp; i++) { //到下标为num_ps-mp就可以了,后面的达不到最小要求长度时间段 if (hold_qs[i] == 0) { //如果某个quarter为 0, 直接返回,观察下一个period continue; } //从末尾依次往回查找 int first = i+1; //以1为开始,而不是0 int last = num_qs; //对满足最短长度的periods, 即大于或等于,记录在一个vector中 while (last-first+1 >= mp) { if (hold_qs[last-1] != 0) { //从后往前找 double pq = 0; //这个一定要,否则vector为空的 Period temp; periods.push_back(temp); for (int j = first-1; j < last; j++) { pq += hold_qs[j]; //用于计算每个quarter的航班数 periods[enough].pd.push_back(hold_qs[j]); } periods[enough].first = first; periods[enough].last = last; periods[enough].length = last-first+1; periods[enough].pq = pq/(last-first+1); enough++; } last--;//往前找 } } //进行快速排序 qsort(periods, 0, enough-1); cout << "Result for run " << order << ":" << endl; //输出所要求的时间段,如果未达到要求数目,则输出得到的 ;否则输出指定要求数目 if (enough < rp) { for (int i = enough-1; i >= 0; i--) { cout << periods[i].first << "-" << periods[i].last << endl; } } else { for (int i = enough-1; i >= enough-rp; i--) { cout << periods[i].first << "-" << periods[i].last << endl; } } order++; } return 0; } void swap (Period *a, Period *b) { Period temp = *a; *a = *b; *b = temp; } //快速排序 void qsort(vector<Period> &periods, int low, int height) { if (low < height) { Period pvt = periods[(low+height)/2]; swap(periods[(low+height)/2], periods[height]); int p = low; for (int i = low; i < height; i++) { if (periods[i].pq < pvt.pq) { swap(periods[i], periods[p]); p++; } else if (periods[i].pq == pvt.pq && periods[i].length < pvt.length) { swap(periods[i], periods[p]); p++; } else if (periods[i].pq == pvt.pq && periods[i].length == pvt.length && periods[i].last > pvt.last) { swap(periods[i], periods[p]); p++; } } swap(periods[p], periods[height]); qsort(periods, low, p-1); qsort(periods, p+1, height); } }
相关文章推荐
- 1063 sicily (木有准确理解题意,一直wrong error)——不过这事快速排序的一次复习,还是记录一下
- 洛谷1177 快速排序 解题报告
- 快速排序浅谈——(解题报告)HDU1157和POJ2388---Who's in the Middle
- POJ 1007(无序度 排序) 解题报告
- C原地快速排序
- 算法导论第四章-快速排序
- 排序:快速排序与选择排序
- 白话经典算法系列之六 快速排序 快速搞定
- 快速排序学习2(随机化版本)
- 快速排序
- Sicily 2711. 模板与STL 解题报告
- 划分算法与快速排序
- 分治算法——快速排序
- 快速排序
- Java-冒泡排序、快速排序、插入排序、快速排序
- 快速排序
- 排序-快速排序-基本-随机化快速排序
- 快速排序之Java实现
- C++ 快速排序
- 排序算法之快速排序