sicily 1046. Plane Spotting(排序求topN)

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.

题意:按照他给的规则,求能看到最多飞机的时间区间。

思路就是枚举+排序+要top几输出top几

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define eps 1e-6

struct period {
    int start, end;
    int len;
    double avg;  /* plane per quarter */
    bool operator<(const period& other) const {
        if (fabs(avg - other.avg) < eps) {  // this.ppq == other.ppq
            if (fabs(len - other.len) < eps) {
                return end < other.end;
            } else {
                return len > other.len;
            }
        } else {
            return avg > other.avg;
        }
    }
} periods[310 * 310];

int main(void) {
#ifdef JDEBUG
    freopen("1046.in", "r", stdin);
    freopen("1046.out", "w", stdout);
#endif
    int ppq[310];  // plane per quater
    int sum[310];  // planes in ppq[1~i]
    int t;
    scanf("%d", &t);

    for (int i = 1; i <= t; ++i) {
        sum[0] = 0;  // bound
        int total; // number of quarters
        int requested; // number of requested best periods
        int available; // minimum number of quarters spent on spotting planes

        scanf("%d %d %d", &total, &requested, &available);

        for (int j = 1; j <= total; ++j) {
            scanf("%d", &ppq[j]);
            sum[j] = sum[j-1] + ppq[j];
        }

        int counter = 0;

        // for every [start, end] in [1, total]
        // where start - end + 1 >= available
        for (int start = 1; start + available - 1 <= total; ++start) {
            for (int end = start + available - 1; end <= total; ++end) {
                periods[counter].start = start;
                periods[counter].end = end;
                periods[counter].len = end - start + 1;
                periods[counter].avg =
                    (double)(sum[end] - sum[start - 1]) / periods[counter].len;
                counter++;
            }
        }

        std::sort(periods, periods + counter);

        printf("Result for run %d:\n", i);
        for (int p = 0; p < requested && p < counter; ++p) {
            printf("%d-%d\n", periods[p].start, periods[p].end);
        }
    }

    return 0;
}
时间: 2024-10-20 16:28:57

sicily 1046. Plane Spotting(排序求topN)的相关文章

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 快速排序解题

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 解题报告

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.若平均时刻飞机数相同,则

poj2299--归并排序求逆序数

/** \brief poj2299  *  * \param date 2014/8/5  * \param state AC  * \return memory 4640K time 3250ms  *  */ #include <iostream> #include <fstream> #include <cstdio> #include <cstring> using namespace std; const int MAXN=500000; int

POJ 2388 Who&#39;s in the Middle(水~奇数个数排序求中位数)

题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 int na[n+1]; 10 for(int i=0; i

LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到,在for (auto p: prerequistites)中特判了输入中可能出现的平行边或自环. 代码: class Solution { public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { // [0, {1, 2, 3}], me

hive 分组排序,topN

语法格式:row_number() OVER (partition by COL1 order by COL2 desc ) rankpartition by:类似hive的建表,分区的意思:order by :排序,默认是升序,加desc降序:rank:表示别名表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) -- 分组排序-- 求某用户日期最大的3天select a.* from( select p_day,muuid,r

ACM ICPC 2011–2012, NEERC, Northern Subregional Contest J. John’s Inversions(合并排序求逆序数对数)

题目链接:http://codeforces.com/gym/100609/attachments 题目大意:有n张牌,每张牌有红色和蓝色两面,两面分别写了一些数字,同种颜色的任意两个数字若排在前面的数字比排在后面的数字大就叫做一对逆序数.求怎样排序得到的逆序数对最少. 解题思路:其中一种颜色的数字是顺序且这种颜色数字相同时对应的另一种颜色的数字是顺序时得到的逆序数对数最少.难点在于求逆序数对数.因为数量很大O(n^2)复杂度不能满足,这里根据合并排序的原理求解每个数字前面有多少个比它大的数字,