fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5. int n, m;
  6. cin >> n >> m;
  7. vector<int> a(n), b(m);
  8. for (int i = 0; i < n; i++) cin >> a[i];
  9. cin >> b[0];
  10. bool strict_increasing = true;
  11. for (int i = 0; i < n - 1; i++) {
  12. if (a[i] > a[i + 1]) {
  13. strict_increasing = false;
  14. break;
  15. }
  16. }
  17. if (strict_increasing) {
  18. cout << "YES" << endl;
  19. return;
  20. }
  21. bool strict_decreasing = true;
  22. for (int i = 0; i < n - 1; i++) {
  23. if (a[i] < a[i + 1]) {
  24. strict_decreasing = false;
  25. break;
  26. }
  27. }
  28. if (strict_decreasing) {
  29. cout << "YES" << endl;
  30. return;
  31. }
  32.  
  33. a[0] = min(a[0], b[0] - a[0]);
  34.  
  35. for (int i = 1; i < n; i++) {
  36.  
  37. if (a[i] < a[i - 1]) {
  38. if (b[0] - a[i] < a[i - 1]) {
  39. cout << "NO" << endl;
  40. return;
  41. } else {
  42. a[i] = b[0] - a[i];
  43. }
  44. } else {
  45. if (b[0] - a[i] >= a[i - 1]) {
  46. a[i] = b[0] - a[i];
  47. }
  48. }
  49. }
  50.  
  51. cout << "YES" << endl;
  52. }
  53.  
  54. int main() {
  55. int t;
  56. cin >> t;
  57. while (t--) solve();
  58. }
Success #stdin #stdout 0.01s 5292KB
stdin
5
1 1
5
9
3 1
1 4 3
3
4 1
1 4 2 5
6
4 1
5 4 10 5
4
3 1
9 8 7
8
stdout
YES
NO
YES
NO
YES