1 条题解

  • 0
    @ 2026-7-1 15:09:16

    Let

    $p_i=\sum_{j=1}^{i} a_j,\qquad q_i=\sum_{j=1}^{i} b_j.$

    The key is to look at prefix sums. One operation on segment [l,r][l,r] adds the pattern +1,1,+1,1,+1,-1,+1,-1,\dots starting from the left end. Therefore, the prefix sum of the modified array changes by 11 on some prefixes and by 00 on the others, so no prefix sum can ever decrease.

    Now take the special segment [i,i+1][i,i+1]. It adds +1+1 to aia_i and 1-1 to ai+1a_{i+1}, so only the ii-th prefix sum increases by 11, while all other prefix sums stay unchanged. Also, [n,n][n,n] increases only the nn-th prefix sum by 11.

    So every prefix sum can be increased independently and never decreased. Hence, the transformation is possible iff

    piqifor all i. p_i\le q_i \qquad \text{for all } i.

    Time Complexity: O(n)\mathcal{O}(n).

    $p_i=\sum_{j=1}^{i} a_j,\qquad q_i=\sum_{j=1}^{i} b_j.$

    关键在于观察前缀和。对区间 [l,r][l,r] 进行一次操作,会从左端开始添加 +1,1,+1,1,+1,-1,+1,-1,\dots 的模式。因此,修改后数组的前缀和在某些前缀上增加 11,在其他前缀上保持不变,所以任何前缀和都不会减少。

    现在考虑特殊区间 [i,i+1][i,i+1]。它会使 aia_i 增加 +1+1ai+1a_{i+1} 增加 1-1,因此只有第 ii 个前缀和增加 11,而其他所有前缀和保持不变。此外,[n,n][n,n] 只会使第 nn 个前缀和增加 11

    因此,每个前缀和都可以独立增加且永不减少。因此,变换可行的充要条件是

    piqi对所有 i 成立. p_i\le q_i \qquad \text{对所有 } i \text{ 成立}.

    时间复杂度:O(n)\mathcal{O}(n)

    Solution

    #include<bits/stdc++.h>
    using namespace std;
     
    int main(){
     
        int tt;
        cin >> tt;
     
        while(tt--){
            int n;
            cin >> n;
     
            vector<long long> a(n), b(n);
     
            for(int i = 0; i < n; i++) cin >> a[i];
            for(int i = 0; i < n; i++) cin >> b[i];
            for(int i = 1; i < n; i++) a[i] += a[i - 1];
            for(int i = 1; i < n; i++) b[i] += b[i - 1];
     
            bool is = 1;
            for(int i = 0; i < n; i++) if(a[i] > b[i]) is = 0;
     
            if(is) cout << "YES\n";
            else cout << "NO\n";
        }
     
        return 0;
    }
    
    • 1

    信息

    ID
    5
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者