-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5061 from Sosuke23/a
Cutting Out Editorial
- Loading branch information
Showing
2 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
id: cf-1077D | ||
source: CF | ||
title: Cutting Out | ||
author: Rameez Parwez | ||
--- | ||
|
||
[Official Editorial (C++)] (https://codeforces.com/blog/entry/63274) | ||
|
||
## Explanation | ||
|
||
Use binary search to determine the maximum number of times the subarray $t$ can be cut out from the original array. | ||
Once it is calculated, construct the resulting array by including elements in proportion to their contributions. | ||
|
||
## Implementation | ||
|
||
**Time Complexity:** $\mathcal{O} (MAX\_N \log(N))$ | ||
|
||
<LanguageSection> | ||
<CPPSection> | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <vector> | ||
|
||
const int MAX_N = 200000; | ||
|
||
int main() { | ||
int n, k; | ||
std::cin >> n >> k; | ||
std::vector<int> freq(MAX_N + 1); | ||
for (int i = 0; i < n; i++) { | ||
int x; | ||
std::cin >> x; | ||
freq[x] += 1; | ||
} | ||
|
||
auto can = [&](int x) -> bool { | ||
int ele_num = 0; | ||
for (int i = 1; i <= MAX_N; i++) { ele_num += freq[i] / x; } | ||
return ele_num >= k; | ||
}; | ||
|
||
int lo = 0; | ||
int hi = n; | ||
while (lo < hi) { | ||
int mid = (lo + hi + 1) / 2; | ||
if (can(mid)) { | ||
lo = mid; | ||
} else { | ||
hi = mid - 1; | ||
} | ||
} | ||
|
||
std::vector<int> res; | ||
for (int i = 1; i <= MAX_N; i++) { | ||
for (int j = 0; j < freq[i] / lo; j++) { res.push_back(i); } | ||
} | ||
|
||
for (int i = 0; i < k; i++) { std::cout << res[i] << " \n"[i == k - 1]; } | ||
} | ||
``` | ||
</CPPSection> | ||
<PySection> | ||
```py | ||
MAX_N = 200000 | ||
|
||
freq = [0] * (MAX_N + 1) | ||
n, k = map(int, input().split()) | ||
for x in input().split(): | ||
freq[int(x)] += 1 | ||
|
||
|
||
def can(x: int) -> bool: | ||
ele_num = 0 | ||
for i in range(1, MAX_N + 1): | ||
ele_num += freq[i] // x | ||
return ele_num >= k | ||
|
||
|
||
lo = 0 | ||
hi = n | ||
while lo < hi: | ||
mid = (lo + hi + 1) // 2 | ||
if can(mid): | ||
lo = mid | ||
else: | ||
hi = mid - 1 | ||
|
||
res = [] | ||
for i in range(1, MAX_N + 1): | ||
for j in range(0, freq[i] // lo): | ||
res.append(i) | ||
|
||
print(*res[:k]) | ||
``` | ||
</PySection> | ||
</LanguageSection> |