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. [老法新用]使用PADDING-TOP:(PERCENTAGE)实现响应式背景图片

    处理响应式布局中背景图片的简单方法是等比例缩放背景图片.我们知道宽度设为百分比的  <img> 元素,其高度会随着宽度的变化自动调整,且其宽高比不变.如果想在背景图片中实现同样的效果,我们 ...

  2. NATS_08:NATS客户端Go语言手动编写

    NATS客户端    一个NATS客户端是基于NATS服务端来说既可以是一个生产数据的也可以是消费数据的.生产数据的叫生产者英文为 publishers,消费数据的叫消费者英文为 subscriber ...

  3. 科学计算三维可视化---Traits(Property属性)

    Property属性 使用Property函数为类创建Property属性,Property属性用法和一般属性相同,但是他在获取值和赋值时会调用相应的方法 traits库也提供了Property属性 ...

  4. 互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)

    1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are us ...

  5. git爬坑不完全指北(二):failed to push some refs to ‘XXX’的解决方案

    报错分析        从字面理解,这个报错的意思就是说远程仓库里有一个改动是本地仓库里没有的,所以在push前要先把远程仓库上的改动pull或者fetch到本地仓库.然后再执行push的操作,把本地 ...

  6. 判断android是否是debug

    1.使用BuildConfig.DEBUG,这个在住modul里面是有效的,但是在有依赖库里面使用就会一直返回false,可以通过下面的方法解决:在library的build.gradle中添加以下代 ...

  7. [转载]Visual Studio支持Node.js

    http://news.cnblogs.com/n/193893/ https://nodejstools.codeplex.com/ 微软发布了一个官方插件“Node.js Tools for Vi ...

  8. 【转】线程间操作无效: 从不是创建控件“textBox2” 的线程访问它。

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. 【译】第二篇 Integration Services:SSIS数据泵

    本篇文章是Integration Services系列的第二篇,详细内容请参考原文. 简介SSIS用于移动数据.数据流任务提供此功能.因为这个原因,当介绍SSIS时我喜欢从数据流任务开始.数据流任务的 ...

  10. 【总结】2017年当下最值得你关注的前端开发框架,不知道你就OUT了!

    近几年随着 jQuery.Ext 以及 CSS3 的发展,以 Bootstrap 为代表的前端开发框架如雨后春笋般挤入视野,可谓应接不暇. 在这篇分享中,我将总结2017年当下最值得你关注的前端开发框 ...