Loading

AtCoder Beginner Contest 118

A - B +/- A

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int a,b;
int main(){
    cin >> a >> b;
    if (b % a == 0) cout << a + b;
    else
        cout << b - a;
    return 0;
}

B - Foods Loved by Everyone

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int sum[35];
int n, m;
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;
        while (k--) {
            int x;
            cin >> x;
            sum[x]++;
        }
    }
    int res = 0;
    for (int i = 1; i <= m;i++)
        if (sum[i] == n) res++;
    cout << res << endl;
    return 0;
}

C - Monsters Battle Royale

给出n个怪兽,让他们互相攻击,如果a攻击b,那么b的生命值就要减去a的生命值,问最后只剩下一个怪兽,它的生命值最少可以为多少

猜结论:求n个数的gcd即可

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n;
int main(){
    cin >> n;
    int res = 0;
    while(n--){
        int x;
        cin >> x;
        res = __gcd(res, x);
    }
    cout << res << endl;
    return 0;
}

D - Match Matching

给出两个数n和m,问利用m种数字,组成的数字最大是多少,给出了每种数字每个需要消耗多少火柴:1,2,3,4,5,6,7,8,9 分别需要2,5,5,4,5,6,3,7,6个,也要求必须正好使用n个火柴

完全背包,利用字符串记录路径

注意需要先将m个种类排序,先填较大的

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n, m, vul[15] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

struct node {
    int num, v;
} a[N];
string Max(string a, string b) {
    if (a.size() > b.size()) return a;
    if (a.size() < b.size()) return b;
    return a > b ? a : b;
}
bool cmp(node a, node b) { return a.num > b.num; }
int res[N];
int main() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        a[i].num = x;
        a[i].v = vul[x];
    }
    sort(a, a + m, cmp);
    vector<string> dp(n + 10, "-");
    dp[0] = "";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j <= n; j++) {
            if (dp[j] == "-") continue;
            if (j + a[i].v <= n) {
                dp[j + a[i].v] = Max(dp[j + a[i].v], dp[j] + char(a[i].num+'0'));
            }
        }
    }
    cout << dp[n] << endl;
    return 0;
}
posted @ 2021-02-14 01:06  WinterFa1L  阅读(56)  评论(0编辑  收藏  举报