当前位置: 代码迷 >> 综合 >> 【Codeforces Round#593 (Div. 2)】A. Stones 题解
  详细解决方案

【Codeforces Round#593 (Div. 2)】A. Stones 题解

热度:66   发布时间:2023-11-17 23:20:12.0

题目链接:A. Stones

题目

Alice is playing with some stones.

Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones.

Each time she can do one of two operations:

  1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);
  2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).

She wants to get the maximum number of stones, but she doesn’t know what to do. Initially, she has 0 stones. Can you help her?

输入

The first line contains one integer t (1≤t≤100) — the number of test cases. Next t lines describe test cases in the following format:

Line contains three non-negative integers a, b and c, separated by spaces (0≤a,b,c≤100) — the number of stones in the first, the second and the third heap, respectively.

In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.

输出

Print t lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer — the maximum possible number of stones that Alice can take after making some operations.

样例

input

3
3 4 5
1 0 5
5 3 2

output

9
0
6

题意

有三堆石头,两种操作:

  1. 从第一堆拿一个,从第二堆拿两个(如果第一堆石头不少于 1 个并且第二堆石头不少于 2 个就可以进行这种操作)
  2. 从第二堆拿一个,从第三堆拿两个(如果第二堆石头不少于 1 个并且第三堆石头不少于 3 个就可以进行这种操作)

问你能拿出最多的石头是多少。

解题思路

题意也很简单,看懂了就没了。结果跟中间细节的操作顺序没关系,只看数量就行了。于是两种拿法求最大即可:

  1. 先进行第一种操作,进行不下去了再进行第二种操作,记下 r1
  2. 先进行第二种操作,进行不下去了再进行第一种操作,记下 r2

结果就是 max(r1, r2)

代码

#include <iostream>using namespace std;int A(int a, int b, int c) {int r = min(a, b/2);a -= r; b -= 2*r;r += min(b, c/2);return r;
}int B(int a, int b, int c) {int r = min(b, c/2);b -= r; c -= 2*r;r += min(a, b/2);return r;
}int M(int a, int b, int c) {return max(A(a, b, c), B(a, b, c));
}int main() {ios::sync_with_stdio(0); cin.tie(0);int t; cin >> t;while (t--) {int a, b, c; cin >> a >> b >> c;cout << 3*M(a, b, c) << endl;}return 0;
}

请多多支持猹的个人博客 H_On 个人小站 啊啊啊~拜托惹 > ~ <
因为猹的小站真的还挺可的,所以那边更新的也比较勤奋,感谢关注~我会努力的(? ?_?)?

  相关解决方案