Loading

Codeforces Round #825 (Div. 2) C1 Good Subarrays (Easy Version)

Good Subarrays (Easy Version)

双指针

显然 \(l\) 往右走一步的时候,\(r\) 一定是在更远的地方

因此直接双指针求解答案即可

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    init();
    while(t--)
    {
        int n;
        cin >> n;
        vector<int>a(n);
        for(int i=0; i<n; i++) cin >> a[i];
        int l = 0, r = 0;
        ll ans = 0;
        while(l < n)
        {
            while(r < n && a[r] >= (r - l + 1)) r++;
            ans += r - l;
            l++;
        }
        cout << ans << "\n";
    }
    // cout << endl;
    return 0;
}
posted @ 2022-10-11 15:06  dgsvygd  阅读(68)  评论(0编辑  收藏  举报