Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Letters can be rearranged freely, so the order of characters inside each street name is irrelevant. Replace every string by its 26 letter counts.
Fix the lost street ell. For one letter, let be how many times that letter appears in s_ell, and let be how many times it appears in all the other street names combined.
For that one letter, the available amount is . If we make copies of every non-lost street plus one copy of the lost street, the needed amount is .
So every letter must satisfy , or equivalently . Instead of maximizing , minimize .
For each letter with , we need . If and , it is impossible. Otherwise the smallest valid is the maximum of those 26 ceilings, and the answer is if nonnegative.
First, delete the mental image of streets and signs. Cute story, but the whole thing is just 26 independent resource constraints. Each capital letter is a resource, and names are just bundles of those resources.
Let be the number of times character appears in street name . Let
be the total count of character across all street names once.
Now fix which order item was lost: .
The plant still delivers copies of every street name except . So for a character , the available number of letters is
If Al makes copies of every non-lost street name and also one copy of the lost street name, then the needed number of this same character is
So for every character , we need
Define
as the amount of this letter in all other names, and
as the amount required by the lost name.
The condition becomes
Rearrange it:
This is the key move. Maximizing is the same as minimizing
Then each character demands
There are two cases.
Case 1:
No other street name contains this letter.
If , then the lost street needs a letter that literally does not exist in the remaining order. Not "hard", not "needs binary search", just impossible. Answer is .
If , this character does not matter.
Case 2:
We need
Since all 26 letters must work at the same time, the minimum valid is
over all characters where the denominator is positive.
Then the best possible value is
If , even is impossible, because we cannot make one copy of the lost street from the leftover letters. So the answer is .
Otherwise the answer is .
Notice that this also handles duplicate names correctly. If the lost street name equals another street name, that other copy still contributes letters through . No special casing, no weird string matching nonsense.
Algorithm
The ceiling division is
with integer division.
Complexity
Counting all strings costs
Then we check 26 letters for each of streets, so
The memory usage is for the per-string counts. With , this is completely fine. 1300-rated problem, not a boss fight.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n;
ll m;
cin >> n >> m;
vector<array<ll, 26>> cnt(n);
array<ll, 26> total{};
for (int i = 0; i < n; i++) {
string s;
cin >> s;
cnt[i].fill(0);
for (char ch : s) {
int c = ch - 'A';
cnt[i][c]++;
total[c]++;
}
}
for (int i = 0; i < n; i++) {
bool bad = false;
ll need = 0;
for (int c = 0; c < 26; c++) {
ll y = cnt[i][c];
ll x = total[c] - y;
if (x == 0) {
if (y > 0) bad = true;
continue;
}
need = max(need, (y + x - 1) / x);
}
ll ans = (!bad && need <= m) ? (m - need) : -1;
if (i) cout << ' ';
cout << ans;
}
cout << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n;
ll m;
cin >> n >> m;
vector<array<ll, 26>> cnt(n);
array<ll, 26> total{};
for (int i = 0; i < n; i++) {
string s;
cin >> s;
cnt[i].fill(0);
for (char ch : s) {
int c = ch - 'A';
cnt[i][c]++;
total[c]++;
}
}
for (int i = 0; i < n; i++) {
bool bad = false;
ll need = 0;
for (int c = 0; c < 26; c++) {
ll y = cnt[i][c];
ll x = total[c] - y;
if (x == 0) {
if (y > 0) bad = true;
continue;
}
need = max(need, (y + x - 1) / x);
}
ll ans = (!bad && need <= m) ? (m - need) : -1;
if (i) cout << ' ';
cout << ans;
}
cout << '\n';
return 0;
}