当前位置:网站首页>One question per day on July 13, 2020 (structure)
One question per day on July 13, 2020 (structure)
2022-07-18 19:03:00 【Zhong Zhongzhong】
I hope I can analyze a valuable question carefully , Instead of writing a few lines of thought as before 、 Put code , I hope I can understand the idea of a class of problems through a problem
F. Equate Multisets
difficulty :1700
The question : Given two repeatable sets of elements , aggregate B You can multiply the elements in the set by 2 Or divide by 2( Rounding down ) To get and set A The same set . The number of operations is unlimited , Output YES or NO.
Ideas : The preliminary determination is a structural problem , We need to use the knowledge of number theory , Then start thinking .
First of all, you can't sort , because A、B The correspondence of elements in two sets is irregular , So turn to the nature of numbers .
1.A The number in cannot be modified ,A Medium odd numbers cannot pass B Middle number *2 obtain , Only through /2 obtain ;
2.A There may be even numbers in *2 perhaps /2 obtain , the B Uncertainty in number selection and operation
3.A Even numbers in can be divided by 2 Get an odd number , if B The middle number can be passed /2 After construction and deformation, it is odd A Same set , It satisfies the meaning of the question .
The key to this question : take A The numbers in are divided by 2 Construct a set of odd numbers .
AC Code : Changed the code thinking , use map, Because the key can reach 1e9 Power , use unordered_map()
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
unordered_map<int,int>mp;
int n,a[N],b[N];
bool flag;
int main()
{
int t;cin>>t;
while(t--)
{
mp.clear();
flag=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
while(a[i]%2==0&&a[i])
a[i]/=2;
mp[a[i]]+=1;
}
for(int i=1;i<=n;i++)
{
while(b[i])
{
if(b[i]%2&&mp[b[i]])
{
mp[b[i]]--;
break;
}
else
b[i]/=2;
}
}
for(int i=1;i<=n;i++)
if(mp[a[i]])
{
flag=1;break;
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
Code :( There is something wrong with this code , The same way of thinking , Different ways of implementation , For a long time , Didn't find out , In the 7261 There is something wrong with the group sample , I can't see the wrong example )
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,a[N],b[N],c[N];
bool vis[N],flag;
int main()
{
int t;cin>>t;
while(t--)
{
memset(vis,0,sizeof vis);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n;i++)
{
if(a[i]%2)
c[i]=a[i];
else
{
c[i]=a[i];
while(c[i]%2==0&&c[i]) c[i]/=2;
}
}
sort(c+1,c+n+1);
flag=0;
for(int i=1;i<=n;i++)
{
while(b[i])
{
if(b[i]%2)
{
int k=lower_bound(c+1,c+n+1,b[i])-c;
while(vis[k]) k++;
if(!vis[k]&&b[i]==c[k])
{
vis[k]=1;
break;
}
}
b[i]/=2;
}
if(b[i]==0)
{
flag=1;break;
}
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
边栏推荐
猜你喜欢

解决yolov7/detect.py中调用摄像头进行识别时报错“ TypeError: argument of type ‘int‘ is not iterable”

Ruffian Heng embedded bimonthly issue 58

HCIP(6)

HCIP(4)

坐标转换实例讲解

Depth of graph first traverses DFS

2022.7.4-7.10 AI行业周刊(第105期):蜗牛
![[kali] about the solution of](/img/17/9f48b6fcdb14eea2a4b9b546f6d1bc.png)
[kali] about the solution of "file size mismatch" and "the image you are using is being synchronized" in Kali update

CTF逆向(Reverse)知识点总结

剑指Offer21-调整数组顺序使奇数位于偶数前面-双指针
随机推荐
Extraction of templates and generic programming-02-iterator extraction example of fixed extraction technology
IO exception handling
HCIP(6)
SAP HCM 导出学历信息
hcip实验作业(3)
基于数据报表处理系统(VUE+SSM+MySQL)
Offline installation: how to build a secure enterprise class harbor service? The content is too detailed.
hcip实验作业(5)
Understanding and operation of array
SAP S4 Material Management 库存模块 MARD 数据库表读取技术细节介绍
Statistical inference
Extraction-01-fixed extraction technology for template and generic programming
Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?
CTF逆向(Reverse)知识点总结
【黄啊码】MySQL入门—1、SQL 的执行流程
基于Web的爬虫系统设计与实现
[ CTF ]MISC flag
Solution of eigenvalue and eigenvector
How to start neo4j on the server
Docker配置mysql以及宿主机容器目录挂载