1728C - Digital Logarithm - CodeForces Solution


data structures greedy

Please click on ads to support us..

Python Code:

t=int(input())
for o in range(t):
  n=int(input())
  a=list(map(int,input().split()))
  b=list(map(int,input().split()))
  a=sorted(a)
  b=sorted(b)
  d=0     i=j=0
  while(i<n and j<n):
    if a[i]==b[j]:
      a[i]=0
      b[j]=0
      i+=1
      j+=1
      d+=1
    elif a[i]<b[j]:
      i+=1
    else :
      j+=1
    ans=0
  for i in range(len(a)):
    if a[i]>9:
      a[i]=len(str(a[i]))
      ans+=1
  for i in range(len(b)):
    if b[i]>9:
      b[i]=len(str(b[i]))
      ans+=1
  a=sorted(a)
  b=sorted(b)
    i=j=0
  while(i<n and j<n):
    if(a[i]==0 and b[i]==0):
      i+=1
      j+=1
    elif a[i]==b[j]:
      a[i]=0
      b[j]=0
      i+=1
      j+=1
      d+=1
    elif a[i]<b[j]:
      i+=1
    else :
      j+=1
    v=0
  for i in a: 
    if i==1:
      v+=1
  for i in b:
    if i==1:
      v+=1
  ans+=((n-d)*2)-v
  print(ans)

    
    
    
    
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
// const int N = 1000000;
// int ARR[N]efine debug(x)
ll M = 1e9 + 7;


int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        map<ll, ll>a, b;
        for (int i = 0; i < n; i++)
        {
            ll x;
            cin >> x;
            a[x]++;
        }
        for (int i = 0; i < n; i++)
        {
            ll x;
            cin >> x;
            b[x]++;
        }
        vector<ll>aa, bb;
        for (auto el : a)
        {
            ll mini = min(a[el.first], b[el.first]);
            a[el.first] -= mini;
            b[el.first] -= mini;
            for (int i = 0; i < a[el.first]; i++) aa.push_back(el.first);
            for (int i = 0; i < b[el.first]; i++)
            {
                bb.push_back(el.first);
                // b[el.first] = 0;
            }
            b[el.first] = 0;
        }
        for (auto elt : b)
        {
            for (int i = 0; i < b[elt.first]; i++)
                bb.push_back(elt.first);
        }

        // for (int i = 0; i < aa.size(); i++)
        //     cout << aa[i] << " ";
        // cout << '\n';
        // for (int i = 0; i < bb.size(); i++)
        //     cout << bb[i] << " ";
        // cout << '\n';
        ll ans = 0;
        for (int i = 0; i < aa.size(); i++)
        {
            if (aa[i] >= 10)
            {
                aa[i] = (int)(log10(aa[i]) + 1);
                ans++;
            }
        }
        for (int i = 0; i < bb.size(); i++)
        {
            if (bb[i] >= 10)
            {
                bb[i] = (int)(log10(bb[i]) + 1);
                ans++;
            }
        }
        // cout << ans << '\n';


        // for (int i = 0; i < aa.size(); i++)
        //     cout << aa[i] << " ";
        // cout << '\n';
        // for (int i = 0; i < bb.size(); i++)
        //     cout << bb[i] << " ";
        // cout << '\n';
        map<ll, ll>ma, mb;
        for (int i = 0; i < aa.size(); i++)
            ma[aa[i]]++;
        for (int i = 0; i < bb.size(); i++)
            mb[bb[i]]++;
        vector<ll>ca, cb;
        for (auto el : ma)
        {
            ll mini = min(ma[el.first], mb[el.first]);
            ma[el.first] -= mini;
            mb[el.first] -= mini;
            for (int i = 0; i < ma[el.first]; i++)
                ca.push_back(el.first);
            for (int i = 0; i < mb[el.first]; i++)
            {
                cb.push_back(el.first);
                // mb[el.first] = 0;
            }
            mb[el.first] = 0;
        }
        for (auto ab : mb)
        {
            for (int i = 0; i < mb[ab.first]; i++)
                cb.push_back(ab.first);
        }
        // for (int i = 0; i < ca.size(); i++)
        //     cout << ca[i] << " ";
        // cout << '\n';
        // for (int i = 0; i < cb.size(); i++)
        //     cout << cb[i] << " ";
        // cout << '\n';
        for (int i = 0; i < ca.size(); i++)
        {
            if (ca[i] != 1) ans++;
        }
        for (int i = 0; i < cb.size(); i++)
        {
            if (cb[i] != 1) ans++;
        }
        cout << ans << '\n';

    }
}


Comments

Submit
0 Comments
More Questions

25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals