题意
定义f(x) = x在10进制下的位数,比如f(10)=2, f(233)=3。
给定2个长度为n的数组a,b
给定操作,从数组a或者b中,选择一个元素x,将它修改为f(x)。
问最小需要修改多少次,才能使得a和b数组在升序排序后,是完全相等的。
1<=ai,bi<=1e9
思路
由于f函数降得很快,最多只需要3次就可以变成1。我们可以模拟着去做。
从当前所有元素中找出最大的数x,如果另一个数组,没有和x相等的,则将x变为f(x)。
模拟过程可以借用优队下,详见代码。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;
//#define debug
int n, x;
void solve() {
scanf("%d", &n);
priority_queue<int> qa, qb;
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
qa.push(x);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
qb.push(x);
}
int res = 0;
while (!qa.empty() && !qb.empty()) {
int a = qa.top(), b = qb.top();
// printf("{%d %d}\n", a, b);
if (a == b) {
qa.pop();
qb.pop();
continue;
}
++res;
if (a > b) {
qa.pop();
qa.push(to_string(a).length());
} else {
qb.pop();
qb.push(to_string(b).length());
}
}
printf("%d\n", res);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
GZH
对方正在debug