QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#189863 | #2871. Clean Up! | Beevo | TL | 3ms | 8320kb | C++20 | 3.2kb | 2023-09-28 01:03:31 | 2023-09-28 01:03:32 |
Judging History
answer
#include <bits/stdc++.h>
#define el '\n'
#define Beevo ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int P = 31, P2 = 37, M = 1e9 + 7, N = 3e5 + 5;
int pw[N], pw2[N], inv[N], inv2[N];
int add(int a, int b) {
b = (b + M) % M;
return (a + b) % M;
}
int mul(int a, int b) {
return 1LL * a * b % M;
}
int modPow(int b, int p) {
if (p == 0)
return 1;
int x = modPow(b, p / 2);
return p % 2 == 0 ? mul(x, x) : mul(b, mul(x, x));
}
int modInvFer(int n) {
return modPow(n, M - 2);
}
void pre() {
pw[0] = inv[0] = pw2[0] = inv2[0] = 1;
int invP = modInvFer(P), invP2 = modInvFer(P2);
for (int i = 1; i < N; i++) {
pw[i] = mul(pw[i - 1], P);
inv[i] = mul(inv[i - 1], invP);
pw2[i] = mul(pw2[i - 1], P2);
inv2[i] = mul(inv2[i - 1], invP2);
}
}
struct Hash {
vector<pair<int, int>> prefixHash;
Hash(string& s) {
prefixHash = vector<pair<int, int>> (s.size());
for (int i = 0; i < s.size(); i++) {
prefixHash[i].first = mul(s[i] - 'a' + 1, pw[i]);
prefixHash[i].second = mul(s[i] - 'a' + 1, pw2[i]);
if (i) {
prefixHash[i].first = add(prefixHash[i].first, prefixHash[i - 1].first);
prefixHash[i].second = add(prefixHash[i].second, prefixHash[i - 1].second);
}
}
}
pair<int, int> getHash(int l, int r) {
return {
mul(add(prefixHash[r].first, (l ? -prefixHash[l - 1].first : 0)), inv[l]),
mul(add(prefixHash[r].second, (l ? -prefixHash[l - 1].second : 0)), inv2[l])
};
}
};
map<int, set<pair<int, int>>> freqToHash;
map<pair<int, int>, set<int>> hashToString;
void add(pair<int, int> hash, int i) {
freqToHash[hashToString[hash].size()].erase(hash);
hashToString[hash].insert(i);
freqToHash[hashToString[hash].size()].insert(hash);
}
void remove(pair<int, int> hash, int i) {
freqToHash[hashToString[hash].size()].erase(hash);
hashToString[hash].erase(i);
freqToHash[hashToString[hash].size()].insert(hash);
}
void testCase() {
pre();
int n, k;
cin >> n >> k;
string s[n];
vector<Hash> hashes;
for (int i = 0; i < n; i++) {
cin >> s[i], hashes.push_back(Hash(s[i]));
for (int j = 0; j < s[i].size(); j++)
add(hashes[i].getHash(0, j), i);
}
int cnt = n, sol = 0;
while (cnt) {
sol++;
if (cnt <= k)
break;
auto up = freqToHash.upper_bound(k);
up--;
auto hash = *up->second.begin();
while (hashToString[hash].size()) {
cnt--;
int i = *hashToString[hash].begin();
for (int j = 0; j < s[i].size(); j++)
remove(hashes[i].getHash(0, j), i);
}
}
cout << sol;
}
signed main() {
Beevo
int t = 1;
// cin >> t;
while (t--)
testCase();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 8296kb
input:
4 2 a abc abd b
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 3ms
memory: 8320kb
input:
4 2 d c ab a
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 0ms
memory: 8268kb
input:
5 3 please remove all these files
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 3ms
memory: 8260kb
input:
2 3 c acbabaaccb
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 8264kb
input:
4 1 ccbc bbacb cacbbb caabcbbcba
output:
4
result:
ok single line: '4'
Test #6:
score: -100
Time Limit Exceeded
input:
10 2 c bbabcacb a cbc acccaaca abcaac abbacc ccb cacbb aaacab