cf Educational Codeforces Round 60 A. Best Subsegment

原题:
A. Best Subsegment

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given array a1,a2,…,an. Find the subsegment al,al+1,…,ar (1≤l≤r≤n) with maximum arithmetic mean
1 r − l + 1 ∑ i = l r a i \frac{1}{r-l+1}\sum\limits_{i=l}^ra_i rl+11i=lrai
(in floating-point numbers, i.e. without any rounding).

If there are many such subsegments find the longest one.

Input
The first line contains single integer n (1≤n≤10^5) — length of the array a.

The second line contains n integers a1,a2,…,an (0≤ai≤10^9) — the array a.

Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.

Example
input
5
6 1 6 6 0
output
2
Note
The subsegment [3,4] is the longest among all subsegments with maximum arithmetic mean.

中文:

给你一个序列,让你找出这个序列当中的一个连续的子序列,使得这个连续的子序列的平均值最大的情况下该子序列长度最长。

代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100005;
int a[maxn];
int n;
int main()
{
	ios::sync_with_stdio(false);
	while (cin >> n)
	{
		for (int i = 1; i <= n; ++i)
			cin >> a[i];
        int ans=1,tmp=a[1];

        for(int i=1;i<=n;)
        {
            int j=i+1;
            while(a[j]==a[i]&&j<=n)
                j++;


            if(a[i]==tmp)
            {
                if(j-i>ans)
                    ans=j-i;
                i=j;
                continue;
            }
            if(a[i]>tmp)
            {
                tmp=a[i];
                ans=j-i;
                i=j;

                continue;
            }
            if(a[i]<tmp)
            {
                i=j;
                continue;
            }
        }

        cout<<ans<<endl;

	}
	return 0;
}

思路:

A题还有点做头
要找到某一个序列的子序列的平均值最大且子序列长度最长,那么肯定是这个序列中最大值连续出现的次数,所以可以先遍历一遍序列,找到最大值,然后再遍历一遍序列,把最大值连续出现的长度找出来即可。也可以在一次循环中完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的影城管理系统,源码+数据库+论文答辩+毕业论文+视频演示 随着现在网络的快速发展,网上管理系统也逐渐快速发展起来,网上管理模式很快融入到了许多生活之中,随之就产生了“小徐影城管理系统”,这样就让小徐影城管理系统更加方便简单。 对于本小徐影城管理系统的设计来说,系统开发主要是采用java语言技术,在整个系统的设计中应用MySQL数据库来完成数据存储,具体根据小徐影城管理系统的现状来进行开发的,具体根据现实的需求来实现小徐影城管理系统网络化的管理,各类信息有序地进行存储,进入小徐影城管理系统页面之后,方可开始操作主控界面,主要功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影信息管理、购票统计管理、系统管理、订单管理,用户前台;首页、电影信息、电影资讯、个人中心、后台管理、在线客服等功能。 本论文主要讲述了小徐影城管理系统开发背景,该系统它主要是对需求分析和功能需求做了介绍,并且对系统做了详细的测试和总结。具体从业务流程、数据库设计和系统结构等多方面的问题。望能利用先进的计算机技术和网络技术来改变目前的小徐影城管理系统状况,提高管理效率。 关键词:小徐影城管理系统;Spring Boot框架,MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值