Problem
Key Idea
ν° κ°μ a, μμ κ°μ bλΌκ³ νμ.
\(dif := a - b\) μ μ΄μ μ λ§μΆλ€.
\(3 : 1\)μ λΉμ¨λ‘ νμ ꡬμ±νλ κ²½μ°, μ΄ \(dif\) κ°μμ 2μ© μ°¨κ°νλ κ²κ³Ό κ°λ€.
λ°λΌμ, \(3 : 1\)μ λΉμ¨λ‘ κ΅¬μ± κ°λ₯ν μ΅λ ν μ \(teams = \min( \lfloor {dif \over 2} \rfloor, b )\)
μ΄ \(teams\) κ°μ \(b\)μμ λΉΌμ£Όκ³ , λ¨μ μΈμ(\(b\))μ \(2 : 2\)μ λΉμ¨λ‘ νμ ꡬμ±ν΄μ£Όλ©΄ λλ€.
\(ans = \min( \lfloor {dif \over 2} \rfloor, b ) + \lfloor {b - \min( \lfloor {dif \over 2} \rfloor, b ) \over 2} \rfloor\)
- Time: \(O(1)\)
Implementation
/**
* 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();
}
Leave a comment