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[ * ]; int main(void) {
#ifdef JDEBUG
freopen("1046.in", "r", stdin);
freopen("1046.out", "w", stdout);
#endif
int ppq[]; // plane per quater
int sum[]; // planes in ppq[1~i]
int t;
scanf("%d", &t); for (int i = ; i <= t; ++i) {
sum[] = ; // 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 = ; j <= total; ++j) {
scanf("%d", &ppq[j]);
sum[j] = sum[j-] + ppq[j];
} int counter = ; // for every [start, end] in [1, total]
// where start - end + 1 >= available
for (int start = ; start + available - <= total; ++start) {
for (int end = start + available - ; end <= total; ++end) {
periods[counter].start = start;
periods[counter].end = end;
periods[counter].len = end - start + ;
periods[counter].avg =
(double)(sum[end] - sum[start - ]) / periods[counter].len;
counter++;
}
} std::sort(periods, periods + counter); printf("Result for run %d:\n", i);
for (int p = ; p < requested && p < counter; ++p) {
printf("%d-%d\n", periods[p].start, periods[p].end);
}
} return ;
}

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

  1. sicily 1046. Plane Spotting

    1046. Plane Spotting Time Limit: 1sec    Memory Limit:32MB  Description Craig is fond of planes. Mak ...

  2. soj1046. Plane Spotting

    1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond o ...

  3. hive 分组排序,topN

    hive 分组排序,topN 语法格式:row_number() OVER (partition by COL1 order by COL2 desc ) rankpartition by:类似hiv ...

  4. [PY3]——求TopN/BtmN 和 排序问题的解决

    需求 K长的序列,求TopN K长的序列,求BtmN 排序问题 解决 heap.nlargest().heap.nsmallest( ) sorted( )+切片 max( ).min( ) 总结和比 ...

  5. 第2节 网站点击流项目(下):3、流量统计分析,分组求topN

    四. 模块开发----统计分析 select * from ods_weblog_detail limit 2;+--------------------------+---------------- ...

  6. POJ 2388 Who's in the Middle(水~奇数个数排序求中位数)

    题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #in ...

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

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  8. Hadoop学习之路(二十)MapReduce求TopN

    前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...

  9. PHP实现 bitmap 位图排序 求交集

    2014年12月16日 17:15:09 初始化一串全为0的二进制; 现有一串无序的整数数组; 如果整数x在这个整数数组当中,就将二进制串的第x位置为1; 然后顺序读取这个二进制串,并将为1的位转换成 ...

随机推荐

  1. Python入门 语法

    Python入门 语法 语言介绍 对象,类型,值 编码规范  https://www.python.org/dev/peps/pep-0008/ 一.语言介绍 编程语言: 机器语言,汇编语言,高级语言 ...

  2. 同一个局域网中用Windows自己的远程桌面远程局域网中的其他PC

    同一个局域网中用Windows自己的远程桌面远程局域网中的其他PC... ===================== 建立Windows远程访问的前提: 1.访问PC和被访问的PC在同一个局域网中 2 ...

  3. mac python2.7.10 升级到 3.6

    第一步: 下载当前最新版本 Python3.6,下载地址:https://www.python.org/downloads/ 第二步: 安装下载好的包,安装完成后的目录如下: /Library/Fra ...

  4. 科学计算三维可视化---TraitsUI(Group对象组织界面)

    使用Group对象组织界面 将一组相关的Item对象组织在一起 from traitsui.api import Group from traits.api import HasTraits,Int, ...

  5. android textview空格占位符以及一些其他占位符汇总

    == 普通的英文半角空格   ==   ==   == no-break space (普通的英文半角空格但不换行)   == 中文全角空格 (一个中文宽度)   ==   == en空格 (半个中文 ...

  6. What Does “Neurons that Fire Together Wire Together” Mean?

    What Does “Neurons that Fire Together Wire Together” Mean? I’ve heard the phrase “neurons that fire ...

  7. 好的MongoDB学习文章链接

    1.MongoDB 极简实践入门 2.MongoDB中文社区 3.极客学院Mongodb 教程

  8. 一个简单的ns2实验全过程

    实验名称:比较tcp和udp的丢包行为 试验目的:1. 熟练用ns2做网络仿真试验的整个流程:2. 练习写tcl脚本,了解怎么应用http和rtp:3. 练习用awk处理trace数据,了解怎么计算丢 ...

  9. js 各类判断用户输入字符的格式函数

    1.JS 判断IP格式是否正确: function checkIP(ip) { var regular = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;//正则表达式 if (reg ...

  10. Unity 3(二):Unity在AOP方面的应用

    本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): AOP简介: Interception using Unity示例 配置文件示例 一.AOP简介 AO ...