C. Digital Logarithm

https://codeforces.com/contest/1728/problem/C

模拟题

inline void solve() {
int n;
cin >> n;

multiset a, b;
for (int i = 0; i < n; ++i) {
int t;
cin >> t;
a.insert(t);
}
for (int i = 0; i < n; ++i) {
int t;
cin >> t;
b.insert(t);
}

list ls;
for (auto x : a) {
if (b.find(x) != b.end()) {
b.erase(b.find(x));
ls.push_back(x);
}

}
while (!ls.empty()) {
a.erase(a.find(ls.back()));
ls.pop_back();
}

auto getBits = [](int x) {
int res = 0;
while (x) {
x /= 10;
res ++;
}
return res;
};

map<int, multiset> ma, mb;
for (auto x : a) {
int t = getBits(x);
ma[t].insert(x);
}
for (auto x : b) {
int t = getBits(x);
mb[t].insert(x);
}

int ans = 0;
auto cal = [&ls, &getBits, &ans](multiset& l, multiset& r, map<int, multiset>& ml, map<int, multiset>& mr) {
for (auto x : l) {
if (x < 2 || x > 9) {
continue;
}
if (!mr[x].empty()) {
auto f = *mr[x].begin();
mr[x].erase(mr[x].begin());
ls.push_back(x);
r.erase(r.find(f));
}
}
ans += ls.size();
while (!ls.empty()) {
auto x = ls.back();
l.erase(l.find(x));
ls.pop_back();
auto t = getBits(x);
ml[t].erase(ml[t].find(x));
}
};
cal(a, b, ma, mb);
cal(b, a, mb, ma);

for (int i = 2; i < 10; ++i) {
int maxn = max(ma[i].size(), mb[i].size());
ans += 2 * maxn;
}
ans += ma[1].size() + mb[1].size();
ans -= ma[1].count(1) + mb[1].count(1);

cout << ans << '\n';
}

posted @ 2025-04-17 10:50  _Yxc  阅读(19)  评论(0)    收藏  举报