Loading

AtCoder Beginner Contest 125

A - Biscuit Generator

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int a, b, t;
int main(){
    cin>>a>>b>>t;
    cout << (t / a) * b << endl;
    return 0;
}

B - Resale

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n, a[N], c[N];
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> c[i];
    int res = 0;
    for (int i = 0; i <= ((1 << n) - 1); i++) {
        int tmp = 0;
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                tmp += a[j] - c[j];
            }
        }
        res = max(res, tmp);
    }
    cout << res << endl;
    return 0;
}

C - GCD on Blackboard

给出n个数,现在可以选择一个数,将其修改为任何一个数,要求使得修改后的n个数的gcd最大,输出这个gcd

注意到答案肯定是n-1个数的gcd,题目转化为对于每个数,求除了这个数以外的n-1个数的gcd

但是直接枚举肯定超时,所以可以利用前缀后缀的gcd,处理出前缀后缀,然后扫一遍即可

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n, a[N], suml[N], sumr[N];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        suml[i] = __gcd(suml[i - 1], a[i]);
        //cout << suml[i] << ' ';
    }
    for (int i = n; i >= 1; i--) {
        sumr[i] = __gcd(sumr[i + 1], a[i]);
    }
    int res = suml[n];
    for (int i = 1; i <= n; i++) {
        res = max(res, __gcd(suml[i - 1], sumr[i + 1]));
    }
    cout << res << endl;
    return 0;
}

D - Flipping Signs

给出n个数,可以进行无限次操作,每次操作选择相邻的两个数,将其取负,问最后得到的n个数的和的最大值是多少

注意到可以多次操作,那么相当于可以随意将负号移动到任意位置,如果n个数中有0,那么可以利用0将全部的负号消除

否则如果负数的个数为奇数,那么必然要留下一个负数,那就让这个负号给绝对值最小的数即可

否则全部的负数都可以消掉

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int n;
int a[N];
int main() {
    cin >> n;
    int flag0 = 0;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] < 0) sum++;
        if (a[i] == 0) flag0 = 1;
    }
    LL res = 0;
    if (flag0) {
        for (int i = 0; i < n; i++) {
            res += abs(a[i]);
        }
        cout << res << endl;
    } else {
        if (sum & 1) {
            int minn = 0x3f3f3f3f;
            for (int i = 0; i < n; i++) {
                res += abs(a[i]);
                minn = min(minn, abs(a[i]));
            }
            res -= 2 * minn;
            cout << res << endl;
        } else {
            for (int i = 0; i < n; i++) {
                res += abs(a[i]);
            }
            cout << res << endl;
        }
    }

    return 0;
}
posted @ 2021-02-09 11:14  dyhaohaoxuexi  阅读(54)  评论(0编辑  收藏  举报