The C++ Program: balloons_rds.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <cstdlib>
4 using namespace std;
5
6 typedef struct _Team {
7 int numBalloons;
8 int distA;
9 int distB;
10 } Team;
11
12
13 int compare (const void * ptr1, const void * ptr2) {
14 const Team* team1 = (Team*) ptr1;
15 const Team* team2 = (Team*) ptr2;
16 const int diff1 = (team1->distA - team1->distB);
17 const int diff2 = (team2->distA - team2->distB);
18 if (diff1==diff2) return 0;
19 return (diff1 > diff2)?+1:-1;
20 }
21
22 int main() {
23 int numTeams, numA, numB;
24 while (true) {
25 cin >> numTeams >> numA >> numB;
26 if (numTeams<=0) break;
27 int balloonsLeft = 0;
28 Team teams[numTeams];
29 for (int i = 0; i < numTeams; i++) {
30 Team t;
31 cin >>t.numBalloons >> t.distA >> t.distB ;
32 balloonsLeft += t.numBalloons;
33 teams[i] = t;
34 }
35 qsort (teams, numTeams, sizeof(Team), compare);
36
37 int totalDist = 0;
38 for (int i = 0; i < numTeams; i++) {
39 Team team = teams[i];
40
41
42
43
44 int teamACount = 0;
45 int teamBCount = 0;
46
47 if (team.distA <= team.distB && numA >= team.numBalloons) {
48
49
50
51
52 teamACount = team.numBalloons;
53 } else if (team.distA <= team.distB) {
54
55
56
57
58
59 teamACount = numA;
60 teamBCount = team.numBalloons - teamACount;
61 } else if (numB >= balloonsLeft) {
62
63
64
65
66
67
68 teamBCount = team.numBalloons;
69 } else {
70
71
72
73
74
75
76
77 int bAllowed = (numB - (balloonsLeft - team.numBalloons));
78 bAllowed = max(bAllowed, 0);
79 teamBCount = bAllowed;
80 teamACount = team.numBalloons - bAllowed;
81 }
82
83
84 totalDist += team.distA * teamACount;
85 totalDist += team.distB * teamBCount;
86 numA -= teamACount;
87 numB -= teamBCount;
88 balloonsLeft -= team.numBalloons;
89 }
90 cout << totalDist << endl;
91 }
92 return 0;
93 }
94
95
96
97
98
99