D - Exact Change

题意: n个价值为ai的物品。有1、2、3三种硬币,能表示出每个ai的价格最少需要多少枚硬币

Solution: 首先1和2的个数都不可能超过3个,因为3个的话显然用面额3表示价格更大。因此枚举使用的1和2的数量,再判断3的数量即可。

#include <bits/stdc++.h>
using namespace std;
inline void read(int &a) {a=0;int c=getchar(),b=1; while(c>'9'||c<'0') {if(c=='-')b=-1;c=getchar();} while(c>='0'&&c<='9') a=(a<<3)+(a<<1)+c-48,c=getchar();a*=b; }
/**/
const int N = 105;
int t, n, a[N], ans;
/**/
int main()
{
    read(t);
    while(t--)
    {
        read(n);
        for(int i = 1; i <= n; i++) read(a[i]);
        ans = 0x7f7f7f7f;
        for(int one = 0; one <= 2; one++)
        {
            for(int two = 0; two <= 2; two++)
            {
                int three = 0;
                for(int i = 1; i <= n; i++)
                {
                    int minnum = 0x7f7f7f7f;
                    for(int use1 = 0; use1 <= one; use1++)
                    {
                        for(int use2 = 0; use2 <= two; use2++)
                        {
                            int res = a[i] - use1 - use2 * 2;
                            if(res >= 0 && res % 3 == 0)
                            {
                                minnum = min(minnum, res / 3);
                            }
                        }
                    }
                    three = max(three, minnum);
                }
                ans = min(ans, three + one + two);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}