-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1753_maximumScore.cc
74 lines (68 loc) · 1.46 KB
/
Problem_1753_maximumScore.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 模拟
int maximumScore1(int a, int b, int c)
{
priority_queue<int> que;
que.push(a);
que.push(b);
que.push(c);
int ans = 0;
while (!que.empty())
{
int x = que.top();
que.pop();
int y = que.top();
que.pop();
if (x == 0 || y == 0)
{
break;
}
ans++;
que.push(x - 1);
que.push(y - 1);
}
return ans;
}
int maximumScore2(int a, int b, int c)
{
// 设 a <= b <= c
// 当 a + b <= c,这时用a、b去匹配c,答案为a + b
// 当 a + b > c,将c优先匹配a或b中较大的那个,然后剩下的a和b两两配对
// 设a与c配对x次,b与c配对y次,答案为x + y + ((a - x) + (b - y)) / 2,简化为(a + b + c) / 2
int sum = a + b + c;
int maxVal = std::max({a, b, c});
if (sum - maxVal < maxVal)
{
return sum - maxVal;
}
else
{
return sum / 2;
}
}
};
void testMaximumScore()
{
Solution s;
EXPECT_EQ_INT(6, s.maximumScore1(2, 4, 6));
EXPECT_EQ_INT(7, s.maximumScore1(4, 4, 6));
EXPECT_EQ_INT(8, s.maximumScore1(1, 8, 8));
EXPECT_EQ_INT(6, s.maximumScore2(2, 4, 6));
EXPECT_EQ_INT(7, s.maximumScore2(4, 4, 6));
EXPECT_EQ_INT(8, s.maximumScore2(1, 8, 8));
EXPECT_SUMMARY;
}
int main()
{
testMaximumScore();
return 0;
}