fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. vector<int> solution(vector<int>& wins, vector<int>& draws, vector<int>& scored, vector<int>& conceded) {
  8. int n = wins.size();
  9.  
  10. // Each tuple: {points, goal_diff, -index}
  11. vector<tuple<int, int, int>> teams;
  12.  
  13. for (int i = 0; i < n; ++i) {
  14. int points = 3 * wins[i] + draws[i];
  15. int goal_diff = scored[i] - conceded[i];
  16. teams.push_back({points, goal_diff, -i}); // negative index for stable sort
  17. }
  18.  
  19. // Sort descending by points, then goal_diff, then original index
  20. sort(teams.rbegin(), teams.rend());
  21.  
  22. int first = -get<2>(teams[0]); // reverse the negative index
  23. int second = -get<2>(teams[1]);
  24.  
  25. return {first, second};
  26. }
  27. // wins = [2, 1, 0]
  28. // draws = [1, 5, 6]
  29. // scored = [20, 15, 10]
  30. // conceded = [20, 10, 15]
  31. int main() {
  32. vector<int> wins = {2, 1, 0};
  33. vector<int> draws = {1, 5, 6};
  34. vector<int> scored = {20, 15, 10};
  35. vector<int> conceded = {20, 10, 15};
  36.  
  37. vector<int> result = solution(wins, draws, scored, conceded);
  38. cout << "Winner Index: " << result[0] << ", Second Place Index: " << result[1] << endl;
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5224KB
stdin
Standard input is empty
stdout
Winner Index: 1, Second Place Index: 0