cf1574 D. The Strongest Build(BFS&STL)

题目

  1. 链接D. The Strongest Build
  2. 题意:给定n<=10组数,每组数的数量为ci<=2e5,所有ci之和也<=2e5。每组数从前到后从小到大。有m个组合不能选择,求组合大小最大的能选择的组合。
    1. 组合大小:下标对应的值的和。
      在这里插入图片描述
  3. 题解
    1. 首先应该知道是BFS,
    2. 然后就是STL的问题了——vector可以自己比较大小 & struct不用都重构一下 & 优先队列没那么难
  4. 代码
/*
题目链接:https://codeforces.com/contest/1574/problem/D
题意:
题解:见同文件夹D。
收获:
    1. BFS,主要是都忘了bfs的原理了emmm,太fw了。
        ————也不能这样说,这题确实挺难的,原来BFS这么简单的知识点都还能深入。
    2.
*/
#include <bits/stdc++.h>
// #define int long long
using namespace std;
const int N = 10 + 5;

int n, m, c[N], x;
vector<int> g[N], ans, v;
map<vector<int>, int> mp, mp1;
struct node {
    int sum;
    vector<int> v;
    bool operator<(const node &b) const {
        if (sum == b.sum) return v < b.v;
        return sum < b.sum;
    }
};
void bfs() {
    priority_queue<node> q;
    int sum = 0;
    ans.clear();
    for (int i = 1; i <= n; i++) {
        sum += g[i][c[i]];
        ans.push_back(c[i]);
    }
    q.push({sum, ans});
    mp1[ans] = 1;
    // cout << ">>>>>>>>>>>>>>>>>\n";
    // int cnt=0;
    while (!q.empty()) {
        node now = q.top();
        q.pop();
        ans = now.v, sum = now.sum;
        // for (auto i : ans) cout << i << " ";
        // cout << "=====" << sum << endl;
        if (!mp[ans]) break;
        // cnt++;
        // if(cnt==2) break;
        for (int i = 1; i <= n; i++) {
            if (ans[i - 1] > 1) {
                sum = sum - g[i][ans[i - 1]] + g[i][ans[i - 1] - 1];
                ans[i - 1]--;
                if (!mp1[ans]) {
                    q.push({sum, ans});
                    mp1[ans] = 1;
                }
                ans[i - 1]++;  //注意这里
                sum = sum + g[i][ans[i - 1]] - g[i][ans[i - 1] - 1];
                // 如果可以,还是建议直接取出来。
            }
        }
    }
}
signed main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c[i]);
        g[i].push_back(0);
        for (int j = 1; j <= c[i]; j++) {
            scanf("%d", &x);
            g[i].push_back(x);
        }
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        v.clear();
        for (int j = 1; j <= n; j++) {
            scanf("%d", &x);
            v.push_back(x);
        }
        mp[v] = 1;
    }
    bfs();

    // cout << ">>>>>>>";
    for (int i = 1; i <= n; i++) cout << ans[i - 1] << " ";
    return 0;
}
/*

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值