489B - BerSU Ball - CodeForces Solution


dfs and similar dp graph matchings greedy sortings two pointers *1200

Please click on ads to support us..

Python Code:

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
m = int(input())
b = list(map(int, input().split()))
b.sort(reverse=True)
pointer_a = 0
pointer_b = 0
result = 0
while((pointer_a < n) and (pointer_b < m)):
    if (abs(a[pointer_a] - b[pointer_b]) <= 1):
        result += 1
        pointer_a += 1
        pointer_b += 1
    elif (a[pointer_a] > b[pointer_b]):
        pointer_a += 1
    else:
        pointer_b += 1

print(result)

C++ Code:

#include<bits/stdc++.h>
#include<cmath>
#include<iomanip>
#define pi 3.1415926535
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define ll long long int
#define ld long double
#define N endl
using namespace std;


void danial(){

ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++){
    cin>>a[i];
}
sort(a,a+n);
ll m;
cin>>m;
ll b[m];
for(ll i=0;i<m;i++){
    cin>>b[i];
}
sort(b,b+m);
ll c=0;

    for(ll j=0;j<n;j++){

        for(ll k=0;k<m;k++){

        
     if((abs(a[j]-b[k]))<=1){
        c++;
        b[k]=2222;
        break;
     }
    }
    }
    cout<<c<<N;


}






int main(){

danial();
//}
}


Comments

Submit
0 Comments
More Questions

432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins