题解 | 气球谜题

气球谜题

https://www.nowcoder.com/practice/3b5ebe9b5f944ccda84517bb748a6c0f

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<char> balloon(n);
    for (int i = 0; i < n; ++i) {
        cin >> balloon[i];
    }
    vector<int> t(n+1);
    for (int i = 1; i <= n; ++i) {
        cin >> t[i];
    }
    vector<vector<long long>> pre(n + 1, vector<long long>(3));
    for (int i = 1; i <= n; i ++ ) {
        pre[i][0] = pre[i - 1][0];
        pre[i][1] = pre[i - 1][1];
        pre[i][2] = pre[i - 1][2];
        if (balloon[i - 1] == '0') {
            pre[i][1] += t[i];
            pre[i][2] += t[i];
        } else if (balloon[i - 1] == '1') {
            pre[i][0] += t[i];
            pre[i][2] += t[i];
        } else {
            pre[i][0] += t[i];
            pre[i][1] += t[i];
        }
    }
    long long ans = LLONG_MAX;
    vector<long long> mn(n+1);
    int a[3] = {0, 1, 2};
    do {
        mn[1] = pre[1][a[0]] - pre[1][a[1]];
        for (int i = 2; i <= n; i ++ ) {
            mn[i] = min(mn[i - 1], pre[i][a[0]] - pre[i][a[1]]);
        }
        for (int i = n + 1; i >= 1; i -- ) {
            ans = min(ans, pre[n][a[2]] - pre[i - 1][a[2]] + pre[i - 1][a[1]] + mn[i - 1]);
        }
    } while (next_permutation(a, a + 3));
    cout << ans << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论
为什么自测运行会报段错误?但是提交保存却可以通过?
点赞 回复 分享
发布于 08-15 09:42 广东

相关推荐

12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务