第一周题单
C - Rock Paper Scissors (CodeForces - 103117D)
BaoBao and DreamGrid are playing a card game. Each player has n
cards in the beginning and there are three types of cards: rock, paper, and scissors.
The game consists of n rounds. In each round, BaoBao will first play one of his remaining cards (this card is shown to both players). After that, DreamGrid can choose one of his remaining cards and play it (also shown to both players). The score of this round is calculated by referring to the following table:
DreamGrid↓ BaoBao→ | Rock | Paper | Scissors |
---|---|---|---|
Rock | 0 | -1 | 1 |
Paper | 1 | 0 | -1 |
Scissors | -1 | 1 | 0 |
After the round, the two played cards are removed from the game. The score of the whole game is the sum of the score of each round.
BaoBao aims at minimizing the score of the whole game, while DreamGrid aims at maximizing it. Both players know the number of cards of each type his opponent and himself holds in the beginning. What’s the final score of the game given that both of them take the best strategy?
Input
There are multiple test cases. The first line of the input contains an integer T ( 1 ≤ T ≤ 1 0 3 ) T(1≤T≤10^3) T(1≤T≤103) indicating the number of test cases. For each test case:
The first line contains three integers b r b_r br, b p b_p bp and b s ( 0 ≤ b r , b p , b s ≤ 1 0 9 ) b_s(0≤b_r,b_p,b_s≤10^9) bs(0≤br,bp,bs≤109), indicating the number of rock, paper and scissors cards BaoBao has.
The second line contains three integers d r d_r dr, d p d_p dp and d s ( 0 ≤ d r , d p , d s ≤ 1 0 9 ) d_s(0≤d_r,d_p,d_s≤10^9) ds(0≤dr,dp,ds≤109), indicating the number of rock, paper and scissors cards DreamGrid has.
It’s guaranteed that b r + b p + b s = d r + d p + d s b_r+b_p+b_s=d_r+d_p+d_s br+bp+bs=dr+dp+ds.
Output
For each test case output one line containing one integer indicating the final score of game.
Sample1
Input
4
4 4 2
10 0 0
0 10 0
2 4 4
1 2 3
3 2 1
10 10 10
10 10 10
Output
-2
2
5
30
题目大意
DreamGrid与BaoBao猜拳,BaoBao先手,BaoBao采取让自己的得分最少的策略,DreamGrid采取让自己的分最多的策略(即让DreamGrid尽可能地多赢),最后的总得分为DreamGrid的得分(从表格中可知)
题解过程
先计算DreamGrid能赢的回合得到的分数,再算平手的回合,最后在算输的回合损失的分数,得到总得分
最后别忘记考虑得分大于int取值范围的可能
#include <iostream>
using namespace std;
void round_win(int &a, int &b, long long &sum)
{
if (a >= b)
{
sum += b;
a -= b;
b = 0;
}
else
{
sum += a;
b -= a;
a = 0;
}
}
void round_draw(int &a, int &b)
{
if (a > 0 && b > 0) // 先判断是否两个人都有牌
{
if (a >= b)
{
a -= b;
b = 0;
}
else
{
b -= a;
a = 0;
}
}
}
void solution()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int br, bp, bs, dr, dp, ds;
long long sum = 0;
cin >> br >> bp >> bs;
cin >> dr >> dp >> ds;
// 处理d能赢b的情况
round_win(dr, bs, sum); // d的石头比b的剪刀多
round_win(ds, bp, sum); // d的剪刀比b的布多
round_win(dp, br, sum); // d的布比b的石头多
// 处理d与b打平的情况
round_draw(dr, br); // 石头
round_draw(ds, bs); // 剪刀
round_draw(dp, bp); // 布
// 处理输了的情况
sum -= dr + ds + dp;
cout << sum << endl;
}
}
int main(int argc, char const *argv[])
{
solution();
return 0;
}
刚开始学习,欢迎指正