「杂题乱刷2」CF1702F

哎哎哎,题解区里怎么没我的做法啊 /yun。

于是就有了这篇题解。

题目链接

CF1702F Equate Multisets (luogu)

CF1702F Equate Multisets (codeforces)

解题思路

首先我们发现,a 序列中的数字的末尾的 0 是无意义的,因此我们可以将 a 序列中的每一个数字一直除以 2 直到这个数字为奇数。

我们发现,将 b 序列的数字一直乘上 2 是一定没有损失的,于是我们首先肯定是通过将 b 序列的数字乘 2 的方式与 a 序列中的数字匹配的。

处理完乘的操作,同样地,我们可以通过一直将 b 序列中的数字一直除以 2 直到匹配的方式来处理除的操作。

无解的情况很好判断,只需要判断是否最终均可以匹配即可。

时间复杂度 O(nlog2V),其中 V 为值域。

参考代码

点击查看代码
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
/*
显然先做乘法再做除法。 
*/
ll n;
ll a[1000010],b[1000010];
map<ll,ll>mp;
ll vis[1000010]; 
void solve()
{
	mp.clear();
	cin>>n;
	forl(i,1,n)
	{
		cin>>a[i];
		while(a[i]%2==0)
			a[i]/=2;
	}
	forl(i,1,n)
		cin>>b[i],vis[i]=0;
	sort(a+1,a+1+n);
	sort(b+1,b+1+n);
	forl(i,1,n)
		mp[a[i]]++;
	forl(i,1,n)
	{
		ll num=b[i],lst=-1;
		while(num<=5e9)
		{
			if(mp[num])
				lst=num;
			num*=2;
		}
		if(lst!=-1)
			mp[lst]--,vis[i]=1;
	}
	forl(i,1,n)
		if(!vis[i])
		{
			ll num=b[i],pd=0;
			while(num>0)
			{
				num/=2;
				if(mp[num])
				{
					pd=1;
					mp[num]--;
					vis[i]=1;
					break;
				}
			}
		}
	forl(i,1,n)
		if(!vis[i])
		{
			cfn;
			return ;
		}
	cfy;
}
int main()
{
	IOS;
	t=1;
 	cin>>t;
	while(t--)
		solve();
	QwQ;
}
posted @   wangmarui  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示