zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 60 (Rated for Div. 2) A. Best Subsegment

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    You are given array
    a
    1
    ,
    a
    2
    ,…,
    a
    n
    a1,a2,…,an
    . Find the subsegment
    a
    l
    ,
    a
    l+1
    ,…,
    a
    r
    al,al+1,…,ar
    (
    1≤l≤r≤n
    1≤l≤r≤n
    ) with maximum arithmetic mean
    1
    r−l+1


    i=l
    r
    a
    i
    1r−l+1∑i=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
    n
    (
    1≤n≤
    10
    5
    1≤n≤105
    ) — length of the array
    a
    a
    .
    The second line contains
    n
    n
    integers
    a
    1
    ,
    a
    2
    ,…,
    a
    n
    a1,a2,…,an
    (
    0≤
    a
    i

    10
    9
    0≤ai≤109
    ) — the array
    a
    a
    .
    Output
    Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
    Example
    Input
    Copy
    5
    6 1 6 6 0
    Output
    Copy
    2
    Note
    The subsegment
    [3,4]
    [3,4]
    is the longest among all subsegments with maximum arithmetic mean.

    题解:,题目条件的意思就是区间的平均值,我们知道,区间平均值是肯定小于等于区间内的最大值,所以我们干脆就去最大值就好了,当然如果还有最大值连续的区间,那我们就取那个最大的长度.如 数列 6 1 6 6 0,有 6 和 6 6,取最长的就是 6 6 ,长度为2.

    #include <bits/stdc++.h>
    const int N=1e5+5;
    
    using namespace std;
    int a[N];
    int main()
    {
        int n;
        scanf("%d",&n);
        int maxn=-1;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(a[i]>maxn) maxn=a[i];
            //cout<<maxn<<endl;
        }
        int len=0;
        int ans=0;
    
        for(int i=1;i<=n;i++){
            if(a[i]==maxn) ans++;
            else{
                if(ans>len) len=ans;
    
                ans=0;
            }
        }
        if(ans>len) len=ans;
        printf("%d
    ",len);
        //cout << "Hello world!" << endl;
        return 0;
    }
    
  • 相关阅读:
    一则由表单提交引发的思考
    前端技术栈持续汇总中(已解锁)
    5599充值中心功能开发
    CSS动画持续汇总中
    编程小技巧持续汇总中
    开发软件安装方法汇总
    HashMap中tableSizeFor
    2019年JVM面试都问了什么?快看看这22道面试题!(附答案解析)
    Spring注解@EnableWebMvc使用坑点解析
    线程池中 work 为何要实现 AbstractQueuedSynchronizer
  • 原文地址:https://www.cnblogs.com/-yjun/p/10427607.html
Copyright © 2011-2022 走看看