2212. 射箭比赛中的最大得分 状态压缩

状态压缩

class Solution {
public:
    vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
        int msk = 0;
        int maxScore = 0;

        // i: 遍历2^12种结果
        for (int i = 1; i < (1 << 12); i++) {
            int count = 0; // 弓箭数
            int score = 0; // 得分
            for (int j = 0; j < 12; j++) {
                if (i & (1 << j)) {
                    count += aliceArrows[j] + 1;
                    score += j;
                }
            }
            if (count <= numArrows && score > maxScore) {
                msk = i;
                maxScore = score;
            }
        }

        vector <int> ans(12);
        for (int j = 0; j < 12; j++) {
            if (msk & (1 << j)) {
                ans[j] = aliceArrows[j] + 1;
                numArrows -= aliceArrows[j] + 1;
            }
        }
        ans[0] += numArrows;

        return ans;
    }
};
posted @ 2022-03-25 10:59  _西瓜不甜  阅读(25)  评论(0编辑  收藏  举报