Skip to content

Commit

Permalink
Merge pull request #5061 from Sosuke23/a
Browse files Browse the repository at this point in the history
Cutting Out Editorial
  • Loading branch information
SansPapyrus683 authored Jan 26, 2025
2 parents ef720db + f6777b7 commit c007b03
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
3 changes: 1 addition & 2 deletions content/3_Silver/Conclusion.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
"isStarred": false,
"tags": ["Binary Search"],
"solutionMetadata": {
"kind": "autogen-label-from-site",
"site": "CF"
"kind": "internal"
}
},
{
Expand Down
101 changes: 101 additions & 0 deletions solutions/silver/cf-1077D.mdx
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>

0 comments on commit c007b03

Please sign in to comment.