AcWing 3801. 最佳连续子数组
Description
给定一个长度为 \(n\) 的数组 \(a_1\), \(a_2\),\(...\),\(a_n\)。
请你找到其中的最佳连续子数组。
最佳连续子数组需满足:
- 子数组内各元素的算术平均数(即所有元素之和除以元素个数)尽可能大。
- 满足条件 1 的前提下,子数组的长度尽可能长。
输出最佳连续子数组的长度。
Input
第一行包含整数 \(T\),表示共有 \(T\) 组测试数据。
每组数据,第一行包含整数 \(n\)。
第二行包含 \(n\) 个整数 \(a_1, a_2, … , a_n\)。
Output
每组数据输出一行结果,表示最佳连续子数组的长度。
Data range
\(1≤T≤20\),
\(1≤n≤10^5\),
\(0≤a_i≤10^9\),
Solution
就是最大值的最大连续数(我这都花了20分钟ac, 太菜了)
Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int q[N];
int main()
{
    int T;
    cin >> T;
    while (T --)
    {
        int n, maxn = 0;
        cin >> n;
        for(int i = 0; i < n; i ++)
        {
            cin >> q[i];
            maxn = max(maxn, q[i]);
        }
        int res = 1, num = 1;
        for (int i = 0; i < n; i ++ )
        {
    
            if(q[i] == maxn)
            {
                int j = i + 1;
                while(j < n && q[j] == maxn) j ++;
                res = max(res, j - i);
                i = j;
            }
        }
        
        cout << res << endl;
    }
    
    return 0;
}

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号