competitive-programmingNov 30, 20211 min read

[Codeforces] 1611B. Team Composition: Programmers and Mathematicians Explained

Updated: Nov 30, 2021

Math
Available in:enko

#Problem

1611B. Team Composition: Programmers and Mathematicians

#Approach

Let the larger value be a and the smaller value be b.
Focus on dif:=abdif := a - b.
Forming a team with a 3:13 : 1 ratio is equivalent to subtracting 2 from this difdif value.

Therefore, the maximum number of teams that can be formed with a 3:13 : 1 ratio is teams=min(dif2,b)teams = \min( \lfloor {dif \over 2} \rfloor, b ).
Subtract this teamsteams value from bb, and form teams with the remaining people (bb) using a 2:22 : 2 ratio.

ans=min(dif2,b)+bmin(dif2,b)2ans = \min( \lfloor {dif \over 2} \rfloor, b ) + \lfloor {b - \min( \lfloor {dif \over 2} \rfloor, b ) \over 2} \rfloor

#Code

/**
 * written: 2021. 11. 30. Tue. 15:33:05 [UTC+9]
 * jooncco의 mac에서.
 **/

#include <bits/stdc++.h>
using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0),cin.tie(0)

int a,b;

void solve() {
    cin >> a >> b;
    if (a < b) swap(a,b);
    int dif= abs(a-b);
    int threeAndOne= dif/2;
    int ans= min(threeAndOne,b) + (b-min(threeAndOne,b))/2;
    cout << ans << "\n";
}

int main() {
    FAST_IO;
    int t; cin >> t;
    while (t--) solve();
}

#Complexity

  • Time: O(1)O(1)
  • Space: O(1)O(1)