当前位置:网站首页>关于一个神奇函数的用法
关于一个神奇函数的用法
2022-07-01 18:53:00 【Aidan 347】
题目描述
给定一个长度为n的序列a_i,定义a[i]为第i个元素的价值。现在需要找出序列中最有价值的“段落”。段落的定义是长度在[S,T]之间的连续序列。最有价值段落是指平均值最大的段落,
段落的平均值=段落总价值/段落长度。
输入输出格式
输入格式:
第一行一个整数n,表示序列长度。
第二行两个整数S和T,表示段落长度的范围,在[S,T]之间。
第三行到第n+2行,每行一个整数表示每个元素的价值指数。
输出格式:
一个实数,保留3位小数,表示最优段落的平均值。
输入输出样例
输入样例#1:
3
2 2
3
-1
2
输出样例#1:
1.000
说明
【数据范围】
对于30%的数据有n<=1000。
对于100%的数据有n<=100000,1<=S<=T<=n,-10000<=价值指数<=10000。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int n,s,t;
int a[N];
double res;
int main()
{
scanf("%d", &n);
scanf("%d%d", &s, &t);
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
for(int i=1;i<=n;i++) a[i]+=a[i-1];
for(int i=s;i<=t&&(double)clock() / CLOCKS_PER_SEC <= 0.95;i++)
{
for(int j=1;j+i-1<=n;j++)
{
int r = j+i-1;
double sum = a[r] - a[j-1];
res = max(res, (double)(sum*1.0/i));
}
}
printf("%.3lf",res);
return 0;
}下面这段代码可以保证循环进行了0.95s之后自动退出循环。。。
(double)clock() / CLOCKS_PER_SEC <= 0.95
边栏推荐
猜你喜欢

Example explanation: move graph explorer to jupyterlab

全国职业院校技能大赛网络安全“splunk“详细配置

开发那些事儿:EasyCVR平台添加播放地址鉴权功能

对象的创建

Getting started with fastdfs

ModSim基本使用(Modbus模拟器)

今日群里分享的面试题

Review the collection container again

Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation

解决VSCode下载慢或下载失败的问题
随机推荐
实例讲解将Graph Explorer搬上JupyterLab
New window open page -window open
uniapp使用腾讯地图选点 没有window监听回传用户的位置信息,怎么处理
List is divided into sets that meet and do not meet conditions (partitioningby)
STC 32位8051单片机开发实例教程 三 程序编译设置与下载
安装sharp报错
[exercise] HashSet
Oracle 死锁测试
今日群里分享的面试题
The large list set is divided into multiple small list sets in equal proportion
自定义插入页面标签以及实现类似通讯录的首字母搜索
Define dichotomy lookup
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
A quietly rising domestic software, low-key and powerful!
2022/6/8-2022/6/12
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
mysql 報錯 Can‘t create table ‘demo01.tb_Student‘ (errno: 150)*
math_利用微分算近似值
Anaconda安装虚拟环境到指定路径
新窗口打开页面-window.open