Extreme Subtraction

Extreme Subtraction

Created by LXC on Thu Mar 28 03:32:56 2024

https://codeforces.com/problemset/problem/1442/A

ranting: 1800

tag: constructive algorithms, dp, greedy

problem

你有一个序列 $a$,你可以进行 $2$ 种操作:

  • 选择前 $k$ 个数,将它们全部减 $1$
  • 选择后 $k$ 个数,将它们全部减 $1$

$k$ 由你自己定,并且每次操作可以不同。
问是否可以把通过若干次操作整个序列变为全是 $0$ 的序列

本题多测,有 $t$ 组数据
$t \le 30000$,$\sum n \le 30000$,$a_i \le {10}^6$

solution

最大下降前缀,最大上升后缀可以去掉,剩余数组a[l...r]

如果 a[l]a[r] 每次下降的总和如果不大于a[l],那么可以通过前缀操作,将数组变为上升。然后后缀操作,可以将所有数归零。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <bits/stdc++.h>
// #define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
using namespace std;

template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}

void sol() {
int n;
cin >> n;
vector<int> a(n);
for (int& i:a) cin >> i;
int l = 0, r = n-1;
while (l+1<n && a[l] >= a[l+1]) l++;
while (r>=1 && a[r-1] <= a[r]) r--;
// cout << l << " " << r << endl;
if (l<r) {
int ls = 0, rs = 0;
for (int i=l+1; i<=r; i++) {
if (a[i-1] > a[i]) ls += a[i-1]-a[i];
}
for (int i=r-1; i>=l; i--) {
if (a[i] < a[i+1]) rs += a[i+1]-a[i];
}
if (a[l]>=ls && a[r]>=rs)
cout << "YES\n";
else
cout << "NO\n";
} else {
cout << "YES\n";
}
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}