-
Notifications
You must be signed in to change notification settings - Fork 0
/
Double_Trouble.cpp
124 lines (116 loc) · 2.3 KB
/
Double_Trouble.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <vector>
using namespace std;
bool canActivateAllBalls(int N, vector<int> &X, vector<int> &P)
{
if (N == 2)
{
return true;
}
// Check if activating from the leftmost ball covers all balls
int pos = X[0];
int power = P[0];
for (int i = 1; i < N; ++i)
{
if (pos + power >= X[i])
{
pos = X[i];
power = max(power, P[i] - (X[i] - pos));
}
else
{
break;
}
if (i == N - 1)
{
return true;
}
}
// Check if activating from the rightmost ball covers all balls
pos = X[N - 1];
power = P[N - 1];
for (int i = N - 2; i >= 0; --i)
{
if (pos - power <= X[i])
{
pos = X[i];
power = max(power, P[i] - (pos - X[i]));
}
else
{
break;
}
if (i == 0)
{
return true;
}
}
// Check if two moves can cover all balls
// Activate leftmost ball
pos = X[0];
power = P[0];
int firstUncovered = -1;
for (int i = 1; i < N; ++i)
{
if (pos + power >= X[i])
{
pos = X[i];
power = max(power, P[i] - (X[i] - pos));
}
else
{
firstUncovered = i;
break;
}
}
// Activate rightmost ball
pos = X[N - 1];
power = P[N - 1];
int lastUncovered = -1;
for (int i = N - 2; i >= 0; --i)
{
if (pos - power <= X[i])
{
pos = X[i];
power = max(power, P[i] - (pos - X[i]));
}
else
{
lastUncovered = i;
break;
}
}
if (firstUncovered != -1 && lastUncovered != -1 && firstUncovered <= lastUncovered)
{
return true;
}
return false;
}
int main()
{
int T;
cin >> T;
while (T--)
{
int N;
cin >> N;
vector<int> X(N), P(N);
for (int i = 0; i < N; ++i)
{
cin >> X[i];
}
for (int i = 0; i < N; ++i)
{
cin >> P[i];
}
if (canActivateAllBalls(N, X, P))
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
return 0;
}