sicily 1046 Plane Spotting 快速排序解题

1046. Plane Spotting

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 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);

	}
}

  

时间: 2024-10-13 02:39:02

sicily 1046 Plane Spotting 快速排序解题的相关文章

Sicily 1046 Plane Spotting

1046. Plane Spotting 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

sicily 1046. Plane Spotting(排序求topN)

DescriptionCraig 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 investi

Sicily 1046. Plane Spotting 解题报告

1046_Plane_Spotting 题目链接: http://soj.me/1046 题目大意: 给出序号为1,2,3...的时间段和每段时间上出现的飞机次数,找出几个连续的时间段,如2-5,其中持续的长度要大于或者等于题目要求.按照以下的要求来筛选出几个最优的时间段,如果达到要求的数量不够则全部输出,符合以下要求视为时间段p1优于p2: p1平均每段时间出现的飞机数大于p2,要求精度是double 平均每段时间出现飞机数一样,但是p1的持续时间比p2长 平均每段时间出现飞机数一样且持续时间

SOJ 1046 Plane Spotting

题目大意:输入整数t,表示有t组测试样例.每组测试样例首先输入三个整数,分别是n(1 ≤ n ≤300)表示有n个观测飞机的时刻,l(1 ≤ l ≤ 100)表示输出结果中的的最高l组,m(1 ≤ m ≤ 300)表示时刻区间的最少包含区间数.接着输入n个整数,分别代表每个时刻可观测的飞机数,计作pi(1 ≤ i ≤ n).求解满足最少区间要求的区间中平均时刻能观测飞机数最多的l组区间. 其他条件: 区间pi比pj好的条件是: 1.pi区间平均时刻飞机数多余pj区间 2.若平均时刻飞机数相同,则

Sicily 1308. Dependencies among J 解题报告

题目:1308. Dependencies among J 思路: 比较简单的一道题,要知道m最早完成的时间,只需要找出所有需要在m之前完成的工作,将它们的完成时间加起来即可.这里使用vector的数组存储每个结点的邻接点,从结点m开始,依次宽度优先搜索m的每个邻接点...数组visited记录每个结点是否被访问过,遇到已经访问过的结点直接跳过. 读取的时候一开始没有找到解决办法,要读取一行的数字,并且要判断是否换行,可以首先读取第一个数即完成时间,然后用getchar读取一个字符,如果是'\n

sicily 1006 team rankings 枚举解题

1006. Team Rankings Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description It's preseason and the local newspaper wants to publish a preseason ranking of the teams in the local amateur basketball league. The teams are the Ants, the Buckets,

sicily 1176 two ends 动态规划解题

1176. Two Ends Constraints Time Limit: 1 secs, Memory Limit: 64 MB Description In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a c

sicily 1150 简单魔方 队列解题

1150. 简单魔板 Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示. 其初始状态是 1 2 3 4 8 7 6 5 对魔板可进行三种基本操作: A操作(上下行互换): 8 7 6 5 1 2 3 4 B操作(每次以行循环右移一个): 4 1 2 3 5 8 7 6 C操作(中间四小块顺时针转一格): 1 7 2 4

POJ 1046 Color Me Less解题报告

Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of