首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >AtCode ABC123 - D - Cake 123

AtCode ABC123 - D - Cake 123

作者头像
小码匠
发布2022-06-16 17:52:41
发布2022-06-16 17:52:41
6020
举报

AtCode ABC123 - D - Cake 123

标签

  • 全搜索、优先队列

题目地址

D - Cake 123

  • https://atcoder.jp/contests/abc123/tasks/abc123_d

问题描述

The Patisserie AtCoder sells cakes with number-shaped candles. There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively. Each cake has an integer value called deliciousness, as follows:

Input

Input is given from Standard Input in the following format:

代码语言:javascript
复制
X Y Z K
A1 A2 A3 ... AX
B1 B2 B3 ... BY
C1 C2 C3 ... CZ

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

代码语言:javascript
复制
2 2 2 8
4 6
1 5
3 8

Sample Output 1

代码语言:javascript
复制
19
17
15
14
13
12
10
8

Sample Input 2

代码语言:javascript
复制
3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

代码语言:javascript
复制
400
310
310
301
301

Sample Input 3

代码语言:javascript
复制
10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

代码语言:javascript
复制
23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.

题意

  • 比较简单:给定x,y,z三个整数作为数组a,b,c的长度,把ai,bi,ci相乘得到一个数字,共有xyz种排列方法,输出结果最大前k个值

思路

  1. 全排列很可能会超时,所以要在全排列的基础上减少push的次数,减少循环次数
  2. 将a,b,c三个数组先按降序排序确保先取到是较大的值,(a + 1) * (b + 1) * (c + 1)保证只取到k个值左右的数据,尽可能减少了循环次数

题解

  • 题解
  • AtCoder Beginner Contest 123 D

小码匠

  • 20ms
代码语言:javascript
复制
void coder_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    long long x, y, z, k;
    cin >> x >> y >> z >> k;
    vector<long long> a(x);
    vector<long long> b(y);
    vector<long long> c(z);
    for (int i = 0; i < x; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end(), greater<long long>());
    for (int i = 0; i < y; i++) {
        cin >> b[i];
    }
    sort(b.begin(), b.end(), greater<long long>());
    for (int i = 0; i < z; i++) {
        cin >> c[i];
    }
    sort(c.begin(), c.end(), greater<long long>());
    vector<long long> vll;
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) {
            for (int s = 0; s < z; s++) {
                if ((i + 1) * (j + 1) * (s + 1) <= k) {
                    vll.push_back(a[i] + b[j] + c[s]);
                } else {
                    break;
                }
            }
        }
    }
    sort(vll.begin(), vll.end(), greater<long long>());
    for (int i = 0; i < k; i++) {
        cout << vll[i] << endl;
    }
}

参考题解

  • 529ms
代码语言:javascript
复制
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll MOD = 1000000007ll;

int x, y, z, k;
ll a[1252], b[1252], c[1252];

int main() {
    scanf("%d%d%d%d", &x, &y, &z, &k);

    for (int i = 0; i < x; i++) {
        scanf("%lld", a + i);
    }

    for (int i = 0; i < y; i++) {
        scanf("%lld", b + i);
    }

    for (int i = 0; i < z; i++) {
        scanf("%lld", c + i);
    }

    multiset<ll> Q;
    Q.insert(0);
    int aaa[] = {x, y, z};
    ll *bbb[] = {a, b, c};

    for (int t = 0; t < 3; t++) {
        int n = aaa[t];
        ll *ar = bbb[t];
        multiset<ll> R;
        while (Q.size()) {
            ll v = *Q.begin();
            Q.erase(Q.begin());
            for (int i = 0; i < n; i++) {
                R.insert(v + ar[i]);
            }
            while (R.size() > k) {
                R.erase(R.begin());
            }
        }
        Q = R;
    }
    auto it = Q.rbegin();
    while (it != Q.rend()) {
        printf("%lld\n", *it);
        it++;
    }
    return 0;
}

参考题解

  • 53ms
代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
using lint = long long;
const lint mod = 256;

template<class itr>
void cins(itr first, itr last) {
    for (auto i = first; i != last; i++) {
        cin >> (*i);
    }
}

using p = pair<lint, int>;

int main() {
    int X, Y, Z, K;

    cin >> X >> Y >> Z >> K;
    lint A[X], B[Y], C[Z];
    cins(A, A + X);
    cins(B, B + Y);
    cins(C, C + Z);
    sort(C, C + Z, greater<lint>());

    priority_queue<p> pairs;

    for (int i = 0; i < X; i++) {
        for (int j = 0; j < Y; j++) {
            pairs.push({A[i] + B[j] + C[0], 0});
        }
    }

    for (int i = 0; i < K; i++) {
        auto top = pairs.top();
        pairs.pop();
        cout << top.first << endl;
        if (top.second == Z - 1) {
            continue;
        }
        pairs.push({top.first - C[top.second] + C[top.second + 1], top.second + 1});
    }
}

参考题解

  • 391ms
代码语言:javascript
复制
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
#define REP(i, m, n) for(int i=(int)(m) ; i < (int) (n) ; ++i)
#define rep(i, n) REP(i,0,n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int main() {
    ll x, y, z, k;

    cin >> x >> y >> z >> k;
    ll a[x], b[y], c[z];

    rep(i, x) cin >> a[i];
    rep(i, y) cin >> b[i];
    rep(i, z) cin >> c[i];

    vector<ll> cur;
    rep(i, x)
        rep(j, y) {
            cur.push_back(a[i] + b[j]);
        }

    sort(cur.rbegin(), cur.rend());
    if (cur.size() > k) {
        cur.resize(k);
    }
    vector<ll> res;
    rep(i, cur.size())
        rep(j, z) {
            res.push_back(cur[i] + c[j]);
        }

    sort(res.rbegin(), res.rend());
    rep(i, k) {
        cout << res[i] << endl;
    }

    return 0;
}

复盘

心得:

  • 数据结构要选用得当
  • 本题毕竟是D级别问题,全排列只适用于有部分分赛制的比赛,对于AtCode“一错满盘输”的赛制并不适用

待补知识点

  • 优先队列语法
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-08,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • AtCode ABC123 - D - Cake 123
  • 标签
  • 题目地址
  • 问题描述
    • Input
    • Output
    • Sample Input 1
    • Sample Output 1
    • Sample Input 2
    • Sample Output 2
    • Sample Input 3
    • Sample Output 3
  • 题意
  • 思路
  • 题解
    • 小码匠
    • 参考题解
    • 参考题解
    • 参考题解
  • 复盘
    • 心得:
    • 待补知识点
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档