CF1702F 题解
洛谷传送门 & CF 传送门
思路
初看这一题,这题难搞的地方就在于有两种操作,所以我们可以考虑先做完一种操作,再去单独地做第二种。我们可以发现,先把
第一种操作并不是单纯地把
第二种操作也不能普通操作,否则太慢了。我们需要操作的是所有
- 当
x>y 时,由于这时y 是单调不减的了,所以直接无解; - 当
x<y 时,我们直接把y 进行一次操作(除以2 )即可; - 当
x=y 时,这时候我们直接把x 和y 都删掉即可。
这里每次要求
代码
# include <bits/stdc++.h>
using namespace std;
int t, n, m, x;
priority_queue <int> q1, q2;
void clear (priority_queue <int>& q) {
while (! q.empty ())
q.pop ();
return ;
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> t;
while (t --) {
cin >> n;
clear (q1), clear (q2); //手动清空优先队列
for (int i = 0; i < n; ++ i) {
cin >> x;
while (! (x & 1)) //不断除以 2
x >>= 1;
q1.push (x);
}
for (int i = 0; i < n; ++ i) {
cin >> x;
while (! (x & 1))
x >>= 1;
q2.push (x);
}
while (! q1.empty ())
if (q1.top () < q2.top ())
q2.push (q2.top () >> 1), q2.pop (); //记得 pop
else if (q1.top () > q2.top ()) {
cout << "NO\n";
goto END; //goto 是个好东西,可以直接跳转到 END 继续执行
} else
q1.pop (), q2.pop ();
cout << "YES\n"; //有解
END: ;
}
return 0;
}