当前位置:网站首页>(题意+详细思路+加注释代码) 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();
}
}边栏推荐
- Solution to high collapse
- SQL anti injection regular expression
- Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
- 2022年最新文本生成图像研究 开源工作速览(Papers with code)
- Unity Huatuo revolutionary hot update series [1]
- 初识网页设计
- 关于编程的自我介绍和规划
- JS逻辑运算符
- Mechanical hard disk Selection Guide -- from the selection experience
- C语言实现小游戏【三子棋】注释详细 逻辑清晰 快来看看吧!!
猜你喜欢

Solution to high collapse

TCP's three handshakes and four waves (brief introduction)

FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s

Brief introduction of VLAN principle and specific experimental configuration

HCIA(网络初级综合实验练习)

7.13 Weilai approved the written examination in advance

NAT(网络地址转化协议)
![[explain C language in detail] takes you to play with the choice (Branch) structure](/img/ca/7ee9f62a2478785c97684c7a0cc749.png)
[explain C language in detail] takes you to play with the choice (Branch) structure

mgre的全连和星型拓扑实验

C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
随机推荐
7.13 Weilai approved the written examination in advance
[explain C language in detail] takes you to play with functions
ensp中的简单静态路由
OSPF protocol overview and basic concepts
ACM模式输入输出练习
2022最新抖音直播监控(二)直播间流媒体下载
STM32入门教程第一讲
golang 实现 tcp-聊天室
Brief introduction of VLAN principle and specific experimental configuration
JS逻辑运算符
初识C语言(2)
MySQL课程2.表的各种查询
[详解C语言]一文带你认识C语言,让你醍醐灌顶
Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)
2022年最新文本生成图像研究 开源工作速览(Papers with code)
Realize data interaction between two apps through fileprovider
【数据库课程设计】SQLServer数据库课程设计(学生宿舍管理),课设报告+源码+数据库关系图
Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction
HCIA基础知识(1)
