AtCoder Beginner Contest 121

目录

A - White Cells

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;

int main(){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    cout << (a - c) * (b - d) << endl;
    return 0;
}

B - Can you solve this?

#include <bits/stdc++.h>

using namespace std;

const int N = 20 + 5;
typedef long long LL;
int n, m, c;
int b[N], a[N][N];
int res = 0;
int main() {
    cin >> n >> m >> c;
    for (int i = 0; i < m; i++) cin >> b[i];
    for (int i = 0; i < n; i++) {
        int tmp = 0;
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
            tmp += a[i][j] * b[j];
        }
        if (tmp + c > 0) res++;
    }
    cout << res << endl;
    return 0;
}

C - Energy Drink Collector

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
struct node {
    LL v, num;
} a[N];
bool cmp(node a, node b) { return a.v < b.v; }
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> a[i].v >> a[i].num;
    sort(a, a + n, cmp);
    LL res = 0;
    LL num = 0;
    for (int i = 0; i < n; i++) {
        if (num + a[i].num <= m) {
            res += a[i].v * a[i].num;
            num += a[i].num;
        }
        else{
            res += a[i].v * (m - num);
            break;
        }
    }
    cout << res << endl;
    return 0;
}

D - XOR World

给出a和b,求出a异或到b的和

一个偶数和比他大1的奇数异或得到1,根据此可以将计算优化到O(1)

#include <bits/stdc++.h>

using namespace std;

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

    return 0;
}
上一篇:leetcode题目:121.买卖股票的最佳时间


下一篇:第121天:机器学习之决策树