-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedrun2.cpp
102 lines (92 loc) · 2.31 KB
/
speedrun2.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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Monster
{
int position;
int health;
};
bool canChefWin(int N, int K, vector<int> &positions, vector<int> &healths)
{
vector<Monster> monsters(N);
for (int i = 0; i < N; i++)
{
monsters[i] = {positions[i], healths[i]};
}
// Try placing the bomb at every possible position
for (int x = positions[0] - K; x <= positions[N - 1] + K; x++)
{
vector<Monster> remainingMonsters;
for (auto &monster : monsters)
{
if (monster.position < x - K || monster.position > x + K)
{
remainingMonsters.push_back(monster);
}
}
// Sort the remaining monsters by position
sort(remainingMonsters.begin(), remainingMonsters.end(), [](const Monster &a, const Monster &b)
{ return a.position < b.position; });
// Use a priority queue to handle the monsters by their closest position to 0
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (auto &monster : remainingMonsters)
{
pq.push({monster.position, monster.health});
}
int time = 0;
bool chefWins = true;
while (!pq.empty())
{
auto current = pq.top();
pq.pop();
int pos = current.first - time;
int health = current.second;
if (pos <= 0)
{
chefWins = false;
break;
}
if (--health > 0)
{
pq.push({current.first, health});
}
time++;
}
if (chefWins)
{
return true;
}
}
return false;
}
int main()
{
int T;
cin >> T;
while (T--)
{
int N, K;
cin >> N >> K;
vector<int> positions(N);
vector<int> healths(N);
for (int i = 0; i < N; i++)
{
cin >> positions[i];
}
for (int i = 0; i < N; i++)
{
cin >> healths[i];
}
if (canChefWin(N, K, positions, healths))
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
return 0;
}