Advertisement
tepyotin2

Balanced Team Solution

Oct 10th, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int player_skill[12];
  6.  
  7. int player_team[12];
  8. int team_count[4];
  9.  
  10. int answer = -1;
  11.  
  12. void recurse(int player) {
  13.     if (player == 12) {
  14.         int team_skill[4] = {0,0,0,0};
  15.         for (int i = 0; i < 12; i++) {
  16.             team_skill[player_team[i]] += player_skill[i];
  17.         }
  18.         int S = max(max(team_skill[0], team_skill[1]),
  19.                     max(team_skill[2], team_skill[3]));
  20.         int s = min(min(team_skill[0], team_skill[1]),
  21.                     min(team_skill[2], team_skill[3]));
  22.  
  23.         if (answer == -1 || S - s < answer) {
  24.             answer = S - s;
  25.         }
  26.         return;
  27.     }
  28.  
  29.     for (int team = 0; team < 4; team++) {
  30.         if (team_count[team] < 3) {
  31.             player_team[player] = team;
  32.             team_count[team]++;
  33.  
  34.             recurse(player+1);
  35.  
  36.             team_count[team]--;
  37.         }
  38.     }
  39. }
  40.  
  41. int main() {
  42.     freopen("bteams.in", "r", stdin);
  43.     freopen("bteams.out", "w", stdout);
  44.  
  45.     for (int i = 0; i < 12; i++) {
  46.         scanf("%d", &player_skill[i]);
  47.     }
  48.  
  49.     recurse(0);
  50.  
  51.     printf("%d\n", answer);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement