Extreme Subtraction
链接 : http://codeforces.com/contest/1442/problem/A
标签 : greedy *1800
思路 :
容易想到 : 对于不减序列(或者是不增序列), 一定可以满足题意. 所以我们不妨用x, y记录将其变成这两种序列的代价 :
\[x = x + a_i - a_{i + 1} (a_i > a_{i + 1}) \\y = y + a_{i + 1} - a_i (a_i < a_{i + 1})
\]
最后比较 x 与 a[0], y 与 a[n - 1].
代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi firsqt
#define se second
#define inf 0x3f3f3f3f
const int N = 1e5 + 7;
int a[N];
int main() {
IO;
int _;
cin >> _;
while (_--) {
int n, x = 0, y = 0;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n - 1; ++i)
if (a[i] > a[i + 1]) x += a[i] - a[i + 1];
else if (a[i] < a[i + 1]) y += a[i + 1] - a[i];
if (x <= a[0] || y <= a[n - 1]) puts("YES");
else puts("NO");
}
return 0;
}