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[ * ]; 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)的更多相关文章
- sicily 1046. Plane Spotting
1046. Plane Spotting Time Limit: 1sec Memory Limit:32MB Description Craig is fond of planes. Mak ...
- soj1046. Plane Spotting
1046. Plane Spotting Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Craig is fond o ...
- hive 分组排序,topN
hive 分组排序,topN 语法格式:row_number() OVER (partition by COL1 order by COL2 desc ) rankpartition by:类似hiv ...
- [PY3]——求TopN/BtmN 和 排序问题的解决
需求 K长的序列,求TopN K长的序列,求BtmN 排序问题 解决 heap.nlargest().heap.nsmallest( ) sorted( )+切片 max( ).min( ) 总结和比 ...
- 第2节 网站点击流项目(下):3、流量统计分析,分组求topN
四. 模块开发----统计分析 select * from ods_weblog_detail limit 2;+--------------------------+---------------- ...
- POJ 2388 Who's in the Middle(水~奇数个数排序求中位数)
题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #in ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
- Hadoop学习之路(二十)MapReduce求TopN
前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...
- PHP实现 bitmap 位图排序 求交集
2014年12月16日 17:15:09 初始化一串全为0的二进制; 现有一串无序的整数数组; 如果整数x在这个整数数组当中,就将二进制串的第x位置为1; 然后顺序读取这个二进制串,并将为1的位转换成 ...
随机推荐
- SQL2005函数大全
表达式:是常量.变量.列或函数等与运算符的任意组合.以下参数中表达式类型是指表达式经运算后返回的值的类型 字符串函数 函数名称 参数 示例 说明 ascii (字符串表达式) select ascii ...
- [应用篇]第一篇 EL表达式入门
概念 EL表达式:EL 全名为Expression Language,就是为了替代<%= %>脚本表达式. 作用 获取数据: EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的 ...
- 调用weka模拟实现 “主动学习“ 算法
主动学习: 主动学习的过程:需要分类器与标记专家进行交互.一个典型的过程: (1)基于少量已标记样本构建模型 (2)从未标记样本中选出信息量最大的样本,交给专家进行标记 (3)将这些样本与之前样本进行 ...
- Python练习-不知道弄个什么鬼
Alex大神,今天丢过来一个PDF,结果就成了这个样子! 1. 执行 Python 脚本的两种方式 交互方式: 命令行 文件方式: ...
- Ubuntu 设置 sudo 开机自启动项 无需输入密码
如果你想设置一个需要sudo权限执行的开机自启动项,而不需要输入密码,那么你需要把该程序加入 /etc/sudoers 中.要直线这个, 首先执行 sudo visudo ,在文件最后加入下面一行 ...
- CentOS时区GMT修改为CST
GMT:格林尼标准时间 北京时间=GMT时间+8小时 [root@sa~]# date -R 查看目前服务器的时间标准 [root@sa~]# vi /etc/sysconfig/clock 将ZON ...
- from setuptools import setup ImportError: No module named setuptools【转】
转自:http://www.cnblogs.com/chinacloud/archive/2010/12/24/1915644.html from setuptools import setupImp ...
- Python 使用 Redis 操作
1.redis简介 redis是一款开源免费的高性能key-value数据库,redis特点: 支持更多的数据类型:字符串(String).列表(List).哈希(Map).数字(Int).集合(Se ...
- centos7 部署 seafile
=============================================== 2018/5/13_第1次修改 ccb_warlock == ...
- 11 The Go Memory Model go语言内置模型
The Go Memory Model go语言内置模型 Version of May 31, 2014 Introduction 介绍 Advice 建议 Happens Before 在发生之前 ...