1 条题解
-
0
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 adds the pattern starting from the left end. Therefore, the prefix sum of the modified array changes by on some prefixes and by on the others, so no prefix sum can ever decrease.
Now take the special segment . It adds to and to , so only the -th prefix sum increases by , while all other prefix sums stay unchanged. Also, increases only the -th prefix sum by .
So every prefix sum can be increased independently and never decreased. Hence, the transformation is possible iff
Time Complexity: .
设
$p_i=\sum_{j=1}^{i} a_j,\qquad q_i=\sum_{j=1}^{i} b_j.$
关键在于观察前缀和。对区间 进行一次操作,会从左端开始添加 的模式。因此,修改后数组的前缀和在某些前缀上增加 ,在其他前缀上保持不变,所以任何前缀和都不会减少。
现在考虑特殊区间 。它会使 增加 , 增加 ,因此只有第 个前缀和增加 ,而其他所有前缀和保持不变。此外, 只会使第 个前缀和增加 。
因此,每个前缀和都可以独立增加且永不减少。因此,变换可行的充要条件是
时间复杂度:。
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; }
信息
- ID
- 5
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者