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. ubuntu thrift

    1.下载 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz 2.解压 tar -xvf thrif ...

  2. Python【操作EXCEL文件】

    #Python中,对EXCEL文件的读写操作需要安装.导入几个第三方模块#xlrd模块:只能读取EXCEL文件,不能进行写操作#xlwt模块:只能进行写操作,但是不能是覆盖写操作(也就是修改Excel ...

  3. Hadoop生态圈-Hbase过滤器(Filter)

    Hadoop生态圈-Hbase过滤器(Filter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  4. Hadoop部署方式-完全分布式(Fully-Distributed Mode)

    Hadoop部署方式-完全分布式(Fully-Distributed Mode) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本博客搭建的虚拟机是伪分布式环境(https://w ...

  5. matlab和C语言实现最小二乘法

    参考:https://blog.csdn.net/zengxiantao1994/article/details/70210662 Matlab代码: N = ; x = [ ]; y = [ ]; ...

  6. [HNOI 2013]切糕

    COGS 2398. [HNOI 2013]切糕 http://www.cogs.pro/cogs/problem/problem.php?pid=2398 ★★★☆   输入文件:nutcake.i ...

  7. python3 flask部署新浪sae

    第一步需要注册新浪云 https://sae.sinacloud.com/ 第二步 创建应用,注:直接选python2.7不用纠结,后面可以指定python版本,共享版 第三步 找到git仓库信息 第 ...

  8. Gulp API之怎样压缩CSS

    先做一个简单的科普 gulp.src() 是用来定位执行路径的,参数通常是一个path gulp.dest() 是用来定位输出路径的,执行的结果都会保存在这个路径下面,可以到路径下面查看结果 gulp ...

  9. iframe中的历史记录问题汇总及解决方案[转]

    在做页面统计的时候遇到了两个问题: 1.包含iframe的页面,在IE下按后退按钮不能刷新主页面.隐藏Iframe的src是统计程序的url,每点一次后退,就会发出一次页面加载时间请求. 2.由js动 ...

  10. 20155217 2016-2017-2 《Java程序设计》第4周学习总结

    20155217 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 在java中,继承时使用extends关键字,private成员也会被继承,只不过子 ...