-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
final-array-state-after-k-multiplication-operations-ii.cpp
213 lines (203 loc) · 6.86 KB
/
final-array-state-after-k-multiplication-operations-ii.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Time: O(n + (n + logr) + nlog(logr) + nlogn) = O(nlogn), assumed log(x) takes O(1) time
// Space: O(n)
// sort, two pointers, sliding window, fast exponentiation
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
static const double EPS = 1e-15;
static const int MOD = 1e9 + 7;
const auto& powmod = [&](int a, int b) {
a %= MOD;
int64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % MOD;
}
a = int64_t(a) * a % MOD;
b >>= 1;
}
return result;
};
const auto& count = [](const auto& x, int target) {
return static_cast<int>(target - x + EPS);
};
if (multiplier == 1) {
return nums;
}
using P = pair<double, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(log(nums[i]) / log(multiplier), i);
}
sort(begin(vals), end(vals));
int right = 1;
for (int left = 0; right <= static_cast<int>(vals.back().first) + 1; ++right) {
for (; left < size(vals) && count(vals[left].first, right) >= 1; ++left);
if (k - left < 0) {
--right;
break;
}
k -= left;
}
if (right == static_cast<int>(vals.back().first) + 2) {
--right;
}
vector<int64_t> nums2(cbegin(nums), cend(nums));
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
const int c = count(x, right);
if (c <= 0) {
break;
}
nums2[i] *= pow(multiplier, c);
}
using P2 = pair<int64_t, int>;
vector<P2> vals2;
for (int i = 0; i < size(nums2); ++i) {
vals2.emplace_back(nums2[i], i);
}
sort(begin(vals2), end(vals2));
const int q = k / size(nums), r = k % size(nums);
const int m = powmod(multiplier, q);
vector<int> result(size(nums));
for (int idx = 0; idx < size(vals2); ++idx) {
const auto& [x, i] = vals2[idx];
result[i] = ((x % MOD * m) % MOD * (idx < r ? multiplier : 1)) % MOD;
}
return result;
}
};
// Time: O(n + min(n, k) * log(logr) + nlog(logr) + nlogn) = O(nlogr), assumed log(x) takes O(1) time
// Space: O(n)
// binary search, sort, fast exponentiation
class Solution2 {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
static const double EPS = 1e-15;
static const int MOD = 1e9 + 7;
const auto& powmod = [&](int a, int b) {
a %= MOD;
int64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % MOD;
}
a = int64_t(a) * a % MOD;
b >>= 1;
}
return result;
};
const auto& binary_search_right = [](auto left, auto right, const auto& check) {
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
};
const auto& count = [](const auto& x, int target) {
return static_cast<int>(target - x + EPS);
};
if (multiplier == 1) {
return nums;
}
using P = pair<double, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(log(nums[i]) / log(multiplier), i);
}
sort(begin(vals), end(vals));
const auto& check = [&](const auto& target) {
int result = 0;
for (const auto& [x, i] : vals) {
const int c = count(x, target);
if (c <= 0) {
break;
}
result += c;
}
return result <= k;
};
const int target = binary_search_right(1, static_cast<int>(vals.back().first) + 1, check);
vector<int64_t> nums2(cbegin(nums), cend(nums));
for (int idx = 0; idx < size(vals); ++idx) {
const auto& [x, i] = vals[idx];
const int c = count(x, target);
if (c <= 0) {
break;
}
k -= c;
nums2[i] *= pow(multiplier, c);
}
using P2 = pair<int64_t, int>;
vector<P2> vals2;
for (int i = 0; i < size(nums2); ++i) {
vals2.emplace_back(nums2[i], i);
}
sort(begin(vals2), end(vals2));
const int q = k / size(nums), r = k % size(nums);
const int m = powmod(multiplier, q);
vector<int> result(size(nums));
for (int idx = 0; idx < size(vals2); ++idx) {
const auto& [x, i] = vals2[idx];
result[i] = ((x % MOD * m) % MOD * (idx < r ? multiplier : 1)) % MOD;
}
return result;
}
};
// Time: O(min(nlogr, k) * logn + nlogn) = O(nlogn * logr)
// Space: O(n)
// heap, sort, fast exponentiation
class Solution3 {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
static const int MOD = 1e9 + 7;
const auto& powmod = [&](int a, int b) {
a %= MOD;
int64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % MOD;
}
a = int64_t(a) * a % MOD;
b >>= 1;
}
return result;
};
if (multiplier == 1) {
return nums;
}
using P = pair<int64_t, int>;
vector<P> vals;
for (int i = 0; i < size(nums); ++i) {
vals.emplace_back(nums[i], i);
}
priority_queue<P, vector<P>, greater<P>> min_heap(cbegin(vals), cend(vals));
const int mx = ranges::max(nums);
vector<int64_t> nums2(cbegin(nums), cend(nums));
for (; k; --k) {
const auto [x, i] = min_heap.top(); min_heap.pop();
if (x >= mx) {
break;
}
nums2[i] *= multiplier;
min_heap.emplace(nums2[i], i);
}
vector<P> vals2;
for (int i = 0; i < size(nums2); ++i) {
vals2.emplace_back(nums2[i], i);
}
sort(begin(vals2), end(vals2));
const int q = k / size(nums), r = k % size(nums);
const int m = powmod(multiplier, q);
vector<int> result(size(nums));
for (int idx = 0; idx < size(vals2); ++idx) {
const auto& [x, i] = vals2[idx];
result[i] = ((x % MOD * m) % MOD * (idx < r ? multiplier : 1)) % MOD;
}
return result;
}
};