zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 196 个人题解

    A - Difference Max

    区间左端减去区间右端

    int main() {
        ios_base::sync_with_stdio(false), cin.tie(0);
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        cout << b - c << endl;
        return 0;
    }
    

    B - Round Down

    一开始想错了,应该String读入,然后find('.')的位置即可,如果没有说明原来就是整数

    int main() {
        ios_base::sync_with_stdio(false), cin.tie(0);
        string s;
        cin >> s;
        int idx = s.find('.');
        if (idx == -1) cout << s;
        else
            for (int i = 0; i < idx; ++i) cout << s[i];
        return 0;
    }
    

    C - Doubled

    (N le 10^{12}) 过大,所以不可能直接通过整数解决。

    利用 字符串 的连接性质转化为 long long 进行判断即可,尽管稍微仍比较耗时但能够 AC

    using ll = long long;
    int main() {
        ios_base::sync_with_stdio(false), cin.tie(0);
        ll n, x = 0;
        cin >> n;
        while (stoll(to_string(x) + to_string(x)) <= n) x++;
        cout << x - 1 << "
    "; // 把 0 算进去了,所以要减一
        return 0;
    }
    

    **D - Hanjo **

    题意:在 HW 的面积里放置 A个 (2 imes1) 长方块榻榻米,B个 (1 imes1) 方形榻榻米。请问有多少种放置方法。

    根据官方的题解,对于这个 (HW le 16) 是约束很小的情况,所以我们可以使用 DFS 进行完全爆搜。

    从左上角的格子开始,尝试放置一个方形榻榻米垫,个长方形榻榻米垫,在左侧或下方突出;然后我们可以穷尽地搜索所有的覆盖方式。
    现在让我们估计一下时间复杂度。
    我们有左上角单元格和榻榻米方向的选择,所以我们有答案的上界 (2^A(egin{smallmatrix} HW\A end{smallmatrix}))
    由于 (2^A(egin{smallmatrix} HW\A end{smallmatrix})) 的最大值为 (5 imes10^7),所以速度足够快。

    事实上,最大答案是3388,所以它的工作速度比估计的要快得多。

    int H, W, A, B, ans = 0;
    void dfs(int i, int bit, int A, int B) {
        if (i == H * W) return (void)ans++;
        if (bit & 1 << i) return dfs(i + 1, bit, A, B);
        if (B) dfs(i + 1, bit | 1 << i, A, B - 1);
        if (A) {
            if (i % W != W - 1 && ~bit & 1 << (i + 1))
                dfs(i + 1, bit | 1 << i | 1 << (i + 1), A - 1, B);
            if (i + W < H * W) dfs(i + 1, bit | 1 << i | 1 << (i + W), A - 1, B);
        }
    }
    int main() {
        ios_base::sync_with_stdio(false), cin.tie(0);
        cin >> H >> W >> A >> B;
        dfs(0, 0, A, B);
        cout << ans << "
    ";
        return 0;
    }
    

    E - Filters

    https://atcoder.jp/contests/abc196/editorial/953

    #include <algorithm>
    #include <iostream>
    #include <numeric>
    using namespace std;
    using ll = long long;
    const ll INF = numeric_limits<ll>::max() / 4;
    void chmin(ll& a, ll b) { return (void)(a > b ? a = b : a = a); }
    void chmax(ll& a, ll b) { return (void)(a < b ? a = b : a = a); }
    int main() {
        ios_base::sync_with_stdio(false), cin.tie(0);
        ll N;
        cin >> N;
        ll low = -INF, high = INF, add = 0;
        for (ll i = 0; i < N; ++i) {
            ll A, T;
            cin >> A >> T;
            if (T == 1) low += A, high += A, add += A;
            else if (T == 2)
                chmax(low, A), chmax(high, A);
            else
                chmin(low, A), chmin(high, A);
        }
        ll Q, x;
        cin >> Q;
        while (Q--) {
            cin >> x;
            // clamp 用来判断一个值是否“夹”在另外一对值之间,如果在区间内则返回
            // x,如果大于左区间则返回左区间,不然返回右区间
    
            // cout << clamp(x + add, low, high) << '
    ';
            cout << min(high, max(low, x + add)) << "
    ";
        }
        return 0;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    驾照更换说明
    批处理创建快捷方式
    AC中保存数据与查询数据
    logger日志模块
    如何将python脚本转化为exe
    numpy学习
    request是个什么东西
    django的test文件的使用方式
    高频正则表达式
    dir 的作用
  • 原文地址:https://www.cnblogs.com/RioTian/p/14567614.html
Copyright © 2011-2022 走看看