zoukankan      html  css  js  c++  java
  • 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") << "
    ";
        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 << "
    ";
        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) << "
    ";
        }
        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 << "
    ";
                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 << "
    ";
        }
        return 0;
    }
    
  • 相关阅读:
    算法笔记 第3章 入门篇(1) 学习笔记
    算法笔记 上机训练实战指南 第3章 入门篇(1)--入门模拟 学习笔记 3.1简单模拟
    机器学习实战:基于Scikit-Learn和TensorFlow 第5章 支持向量机 学习笔记(硬间隔)
    机器学习实战:基于Scikit-Learn和TensorFlow 读书笔记 第6章 决策树
    算法笔记 第7章 提高篇(1)--数据结构专题(1) 学习笔记
    算法笔记 第6章 C++标准模版库(STL)介绍 学习笔记
    自然语言处理入门 何晗 读书笔记 第2章 词典分词
    自然语言处理入门 何晗 读书笔记 第1章 新手上路
    [ML]随机森林
    [ML]LightGBM论文理解
  • 原文地址:https://www.cnblogs.com/Kanoon/p/13799591.html
Copyright © 2011-2022 走看看