-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
66 lines (53 loc) · 1.62 KB
/
test.cpp
File metadata and controls
66 lines (53 loc) · 1.62 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
// Function to check if a certain day count k satisfies the conditions
bool canSatisfyConditions(int k, const vector<int>& h, const vector<int>& a, const vector<int>& t) {
int N = h.size();
vector<pair<long long, int>> heights(N);
// Calculate heights after k days
for (int i = 0; i < N; ++i) {
heights[i] = {h[i] + static_cast<long long>(a[i]) * k, i};
}
// Sort heights in ascending order
sort(heights.begin(), heights.end());
// Check if each plant i has exactly t[i] plants taller than it
for (int i = 0; i < N; ++i) {
if (t[heights[i].second] != N - 1 - i) {
return false;
}
}
return true;
}
int minDaysToSatisfyConditions(int N, const vector<int>& h, const vector<int>& a, const vector<int>& t) {
long long left = 0, right = 1e9;
int result = -1;
// Binary search over days
while (left <= right) {
int mid = (left + right) / 2;
if (canSatisfyConditions(mid, h, a, t)) {
result = mid;
right = mid - 1; // Try to find a smaller k
} else {
left = mid + 1;
}
}
return result;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
vector<int> h(N), a(N), t(N);
for (int i = 0; i < N; ++i) cin >> h[i];
for (int i = 0; i < N; ++i) cin >> a[i];
for (int i = 0; i < N; ++i) cin >> t[i];
int result = minDaysToSatisfyConditions(N, h, a, t);
cout << result << endl;
}
return 0;
}