sicily--1046. Plane Spotting

优先级队列参考:http://blog.sina.com.cn/s/blog_5e518b010100kbts.html

1.一直在“比较”上错了,没有注意到

/* 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.
		*/
2.比较水,用优先级队列模拟即可

#include<iostream>
#include<queue>//使用优先级队列
#include<vector>
using namespace std;

struct Node//某段时间
{
	int start;
	int end;
	double avr;//该段时间内平均每个quarter的飞机
};

struct cmp
{
	bool operator()(const Node &n1, const Node &n2)
	{
		/* 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.
		*/
		if(n1.avr != n2.avr)
			return n1.avr < n2.avr;//相当于less,大顶堆;   平均飞机数更多
		else if((n1.end - n1.start) != (n2.end - n2.start))
			return (n1.end - n1.start) < (n2.end - n2.start); //相当于greater,小顶堆;  平均飞机数一样,长度更长
		else	
			return n1.end > n2.end;//平均飞机数一样,长度一样,更早的结束
	}
};
int main()
{
	int runNum;
	cin >> runNum;
	for(int i = 1; i <= runNum; i++)
	{
		int quarters;
		cin >> quarters;
		int output;//输出的period数
		cin >> output;
		int min;//要求的最小quarter数
		cin >> min;

		int *arr = new int[quarters];
		for(int j = 0; j < quarters; j++)
		{
			cin >> arr[j];//每个quarter内的飞机数
		}
		vector<Node> v;
		//遍历计算
		for(int m = quarters; m >= min; m--)//因为同等程度下,长的优先
		{
			for(int j = 0; j < quarters; j++)
			{
				/*
				**譬如:quarters = 3, 数组的下标是0,1,2; min = 3
				*/
				if(j + m > quarters)//越界
					break;
				int sum = 0;
				for(int k = 0; k < m; k++)
				{
					sum = sum + arr[j + k];
				}
				Node temp;
				temp.start = j + 1;
				temp.end = j + m;
				temp.avr = (double)sum / m;//平均每个period内的飞机数
				v.push_back(temp);
			}
		}
		priority_queue<Node, vector<Node>, cmp> Q(v.begin(), v.end());
		cout << "Result for run " << i << ":" << endl;
		for(int j = 0; j < output; j++)
		{
			if(Q.empty())
				break;
			else
			{
				Node temp = Q.top();
				Q.pop();
				cout << temp.start << "-" << temp.end << endl;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值