您的位置:首页 > 其它

sicily--1046. Plane Spotting

2012-08-08 19:45 274 查看
优先级队列参考: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: