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;
}
原文地址:https://www.cnblogs.com/-yjun/p/10427607.html