2020年10月ICPC & 天梯赛 选拔赛【ACFJ】

A. 表达式

题意

题解

将所有数字替换为A,运算符替换为O,然后不断合并(AOA),判断表达式最后是否为A即可。
注意将数字替换时判断有无前导零。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;
    bool no_lead_zero = true;
    auto isop = [](char c) {
        return c == '+' or c == '-' or c == '*' or c == '/';
    };
    auto replace_digit = [&]() {
        vector<string> v;
        for (int i = 0; i < int(s.size()); i++) {
            if (isdigit(s[i])) {
                int j = i + 1;
                while (j < int(s.size()) and isdigit(s[j])) ++j;
                v.push_back(s.substr(i, j - i));
                s.replace(i, j - i, "A");
                i = -1;
            }
        }
        for (const auto &i : v) {
            if (i[0] == '0' and i.size() > 1) no_lead_zero = false;
        }
    };
    auto replace_op = [&]() {
        for (int i = 0; i < int(s.size()); i++) {
            if (isop(s[i])) {
                s.replace(i, 1, "O");
                i = -1;
            }
        }
    };
    auto replace_AOA = [&]() {
        for (int i = s.find("(AOA)"); i != -1; i = s.find("(AOA)")) {
            s.replace(i, 5, "A");
        }
    };
    replace_digit();
    replace_op();
    replace_AOA();
    cout << (no_lead_zero and s == "A" ? "Legal" : "Illegal") << "\n";
    return 0;
}

C. 股票

题意

题解

将股值看作曲线,每次从最高峰向前加。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    sort(p.begin(), p.end(), [&](int x, int y) {
        return a[x] > a[y];
    });
    long long ans = 0;
    vector<bool> vis(n);
    for (auto i : p) {
        for (int j = i; j >= 0 and not vis[j]; j--) {
            ans += a[i] - a[j];
            vis[j] = true;
        }
    }
    cout << ans << "\n";
    return 0;
}

F. 稳定婚姻问题

题意


题解

模拟。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    multiset<string> female;
    set<int> male;
    map<int, string> match;
    vector<int> boy;
    for (int i = 0; i < 2 * n; i++) {
        string a, b, c;
        cin >> a >> b >> c;
        if (c[0] == 'F') {
            if (male.size()) {
                match[*male.begin()] = b + " " + a;
                male.erase(male.begin());
            } else {
                female.insert(b + " " + a);
            }
        } else {
            boy.push_back(i);
            if (female.size()) {
                match[i] = *female.begin();
                female.erase(female.begin());
            } else {
                male.insert(i);
            }
        }
    }
    for (const auto &i : boy) {
        const auto &s = match[i];
        int pos = s.find(' ');
        cout << s.substr(pos + 1) << ' ' << s.substr(0, pos) << "\n";
    }
    return 0;
}

J. 质因子个数

题意

题解

打表发现数据范围内的一个数最多有8个不同的素因子,那么就很好算了,计算每个数的不同素因子个数并将这个数存到对应的个数中,对于每次给出的区间二分查找左右端点即可。

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e7 + 10;
 
int p[N];
vector<vector<int>> v(9);
 
inline void Init() {
    for (int i = 2; i < N; i++) {
        if (p[i]) continue;
        for (int j = i; j < N; j += i) {
            p[j] = i;
        }
    }
    for (int i = 1; i < N; i++) {
        int n = i, cnt = 0;
        for (int j = p[n]; j != 0; j = p[n]) {
            while (n % j == 0) n /= j;
            ++cnt;
        }
        v[cnt].push_back(i);
    }
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    Init();
    int t;
    cin >> t;
    while (t--) {
        long long l, r, k;
        cin >> l >> r >> k;
        if (k > 8) {
            cout << 0 << "\n";
            continue;
        }
        int i = lower_bound(v[k].begin(), v[k].end(), l) - v[k].begin();
        int j = upper_bound(v[k].begin(), v[k].end(), r) - v[k].begin();
        cout << j - i << "\n";
    }
    return 0;
}
posted @ 2020-10-11 22:30  Kanoon  阅读(271)  评论(0编辑  收藏  举报