当前位置:网站首页>(题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets
(题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets
2022-07-26 22:49:00 【ZaneBobo】
一.题意
给你一个数组a和数组b,你可以对数组b进行如下操作多次:
①选取数组中的一个数字进行减半操作
②选取数组中的一个数字进行乘二操作
看是否可以将数组b变成数组a(这里两个数组相等条件是包含元素相同)
二.思路
我们用二进制思抽象一下对数组b的两个操作:
①去掉二进制最后一位
②在二进制末尾补上一个0
因为有②这个操作,所以其实无论在a数组每个数字后面0的个数并不影响答案是yes还是no。
所以我们先把a数组全部变为奇数,这样其实是对b数组全部逆向进行了②操作。
然后对应的通过①操作也把b数组每个数字变为奇数。
这时候②操作已经发挥完它的价值了。
接着考虑①操作
我们先把目前a和b数组中相同的元素排除掉。
然后再将b数组中剩下的每个元素不断进行①操作,直到它和a数组中某一个剩下的元素相同为止,然后分别删去b数组此元素和对应的a数组中的元素,若在此过程中b数组中有一个元素无法通过①操作变成a数组中剩余元素,则整个b数组不能变为a数组。
三.代码
学来的更简洁的代码
#include<bits/stdc++.h>
#define N 200010
using namespace std;
int t,n,a,b;
map<int,int> mp;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
mp.clear();
for(int i=1;i<=n;i++)
{
cin>>a;
while(a%2==0) a/=2;
mp[a]++;
}
int flag=0;
for(int i=1;i<=n;i++)
{
cin>>b;
while(b&&mp[b]==0) b/=2;
if(b) mp[b]--;
else flag=1;
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}自己写的代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
map<int,int> cnt;
const int N = 2e5+10;
int a[N],b[N],c[N];
void solve()
{
int n;
cin>>n;
cnt.clear();
for(int i=1;i<=n;i++)
{
cin>>a[i];
while(!(a[i]&1))
{
a[i]>>=1;//逆向使用操作②
}
cnt[a[i]]++;
}
bool flag=1;
for(int i=1;i<=n;i++)
{
cin>>b[i];
while(!(b[i]&1))
{
b[i]>>=1;//操作① 因为要和a数组一致都变为奇数
}
if(cnt[b[i]])//这个数和a数组某个元素相同
{
cnt[b[i]]--;
}
else
{
while(!cnt[b[i]]&&b[i])//直至和a数组某个元素相同或者不能再除为止
{
b[i]>>=1;
}
if(!b[i])//没找到相同元素
{
flag=0;
}
else
{
cnt[b[i]]--;
}
}
}
if(flag)puts("YES");
else puts("NO");
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}边栏推荐
- 【mysql】mysql启动关闭命令以及一些报错解决问题
- C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
- Three methods that can effectively fuse text and image information -- feature stitching, cross modal attention, conditional batch normalization
- JS -- first understand the naming rules and data types of JS and variables
- ospf协议概述以及基础概念
- Ospf基础配置应用( 综合实验: 干涉选举 缺省路由 区域汇总 认证--接口认证)
- HCIA动态路由RIP基础实验
- Experiment exercise of two-layer packaging technology (HDLC, ppp--pap\chap, GRE)
- OSPF在MGRE环境下配置及LSA的优化---减少LSA更新量(汇总、特殊区域)
- SQL anti injection regular expression
猜你喜欢

JS 99 multiplication table
![[详解C语言]一文带你认识C语言,让你醍醐灌顶](/img/37/205c1c6eb2ba704941e48ff89c6268.png)
[详解C语言]一文带你认识C语言,让你醍醐灌顶

VLAN原理简述、具体实验配置
![[MySQL] MySQL startup and shutdown commands and some error reports to solve problems](/img/23/b4c90604eba796692fbe049d2679d1.png)
[MySQL] MySQL startup and shutdown commands and some error reports to solve problems

OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)

Text to image intensive reading of paper gr-gan: gradually refine text to image generation

Is index reproduction text generation image is score quantitative experiment whole process reproduction inception score quantitative evaluation experiment step on the pit and avoid the pit process

7.13 蔚来提前批笔试

微信小程序:用户微信登录流程(附:流程图+源码)

Dynamic routing ofps protocol configuration
随机推荐
First knowledge of Web Design
OSPF在MGRE环境下的实验
TIM输出比较——PWM
Golang - sync包的使用 (WaitGroup, Once, Mutex, RWMutex, Cond, Pool, Map)
Mechanical hard disk Selection Guide -- from the selection experience
关于编程的自我介绍和规划
Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
JS logical operator
OSPF静态大实验
TCP's three handshakes and four waves (brief introduction)
Unity Huatuo hot update environment installation and sample project
RIP V2 的简单应用(v2的配置、宣告、手工汇总、RIPV2的认证、沉默接口、加快收敛)
HCIA Basics (1)
VLAN原理简述、具体实验配置
Electron FAQ 61 - must the client run with administrator privileges?
超出隐藏显示省略号
The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.
Unity Huatuo revolutionary hot update series [1]
6.30滴滴面经(一面+二面)
Flink1.13.6 detailed deployment method
