1702F - Equate Multisets - CodeForces Solution


constructive algorithms data structures greedy math number theory *1700

Please click on ads to support us..

Python Code:

 
from collections import defaultdict,Counter
import math
import bisect
from itertools import accumulate
from math import ceil, log
 
from sys import stdin, stdout
 
def read():
    return stdin.readline().rstrip()
 
 
 
 
total = int(read())
 
 
for _ in range(total):
    s = read()
    
    
    a =  ([int(p) for p in input().split()])
    b =  ([int(p) for p in input().split()])
    
    c= defaultdict(lambda:0)
    for i in a:
        while i%2==0:
            i = i//2
        c[i] +=1
        for z in b:
        if c[z] >0:
            c[z] -=1
        else:
            while z >0:
               z = z//2
               if c[z] >0:
                   c[z] -=1
                   break
        if sum(c.values())==0:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define all(a)  (a).begin(),(a).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define eb emplace_back
#define pii pair<int,int>
#define lowbit(a) ((a)&(-a))
#define inv(x) fpow((x),(mod-2))
#define sub(x,y) ((x-y)%mod+mod)%mod
#define pt(x,m) ((x)%(m)+(m))%(m)
#define fc first
#define sc second
#define debug(a) cout<<#a<<":"<<(a)<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll mod=1e9+7;
ll fpow(ll x,ll y) {
	ll tmp=x%mod,res=1;
	while(y) {
		if(y&1)res=res*tmp%mod;
		tmp=tmp*tmp%mod;
		y>>=1;
	}
	return res;
}


inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0'){
		x=(x<<1)+(x<<3)+(c^48);
		c=getchar();
	}
	return x*f;
}


const int N=2e5+10;

void solve() {
	int n;
	n=read();
	map<int,int>a;
	unordered_map<int,int>b;
	unordered_map<int,vector<int>>e;
	vector<int>las;
	for(int i=0; i<n; i++) {
		int x;
		x=read();
		while(x&&(!(x&1))) {
			x>>=1;
		}
		a[x]++;
	}
	for(int i=0; i<n; i++) {
		int x;
		x=read();
		while(x&&!(x&1)) {
			x>>=1;
		}
		b[x]++;
		if(x==0)continue;
		int tmp=x;
		while(tmp) {
			e[tmp].eb(x);
			tmp>>=1;
		}
		e[0].eb(x);
	}
	auto iter=a.rbegin();
	while(iter!=a.rend()) {
		auto [val,cnt]=*iter;
		int chk=a[val];
		auto &v=e[val];
		for(int &i:v) {
			if(b[i]) {
				b[i]--;
				chk--;
				if(chk==0)break;
			}
		}
		if(chk)return puts("NO"),void();
		iter++; 
	}
	puts("YES");
}





int main() {
	int t;
	t=read();
	while(t--)solve();
}

















































Comments

Submit
0 Comments
More Questions

Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number