zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 212 Solution

    题解

    A.Alloy

    水题

    B.Weak Password

    水题 + 1;

    C.Min Difference

    首先想到排序

    接下来我们思考 如果说 ai > bj 那么ai之后的所有数都不可能列入答案,所以更新j

    否则 更新i

    遍历复杂度为O(n + m)排序复杂度为(n log n + m log m)

    #include <bits/stdc++.h>
    using namespace std;
    #define N 200010
    #define INF 1010000000
    #define rep(i, n) for(int i = 0; i < n; ++i)
    
    int n, m;
        int a[N];
        int b[N];
        int ans = INF;
    signed main() {
    
        cin >> n >> m;
        rep(i, n)cin >> a[i];
        rep(i, m)cin >> b[i];
        sort(a, a + n);
        sort(b, b + m);
        int x = 0;
        int y = 0;
        while ((x < n) && (y < m)) {
            ans = min(ans, abs(a[x] - b[y]));
            if (a[x] > b[y])y ++;
            else x ++;
        }
        cout << ans << endl;
        return 0;
    }

    D.Querying Multiset

    一道堆的裸题。

    不过使堆里每个元素加上一个值,在入堆时候的sum处理,确实很妙,学到了。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    int n;
    LL sum = 0;
    signed main() {
    
        priority_queue<LL,vector<LL>,greater<LL>> heap;
        cin >> n;
        while(n --)
        {
            int p,x;
            scanf("%lld",&p);
            if(p == 1)
            {
                scanf("%lld",&x);
                heap.push(x - sum);
            }
            else if(p == 2)
            {
                scanf("%lld",&x);
                sum += x;
            }
            else 
            {
                LL t = heap.top();
                heap.pop();
                cout << t + sum<< endl;
            }
        }
        return 0;
    }

    E - Safety Journey

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 5010;
    
    vector<int> e[N];
    
    const int mod = 998244353;
    typedef long long LL;
    #define rep(i, n) for(int i = 0; i < n; ++i)
    int n,m,k;
    LL dp[N];
    LL dp2[N],s;
    int sz;
    
    signed main() {
        cin >> n >> m >> k;
        while(m --)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            e[a - 1].push_back(b - 1);
            e[b - 1].push_back(a - 1);
        }
        for(int i = 0; i < n; ++ i) dp[i] = (LL)0;
        dp[0] = (LL)1;
        for(int i = 0; i < k; ++ i)
        {
            s = (LL)0;
            for(int j = 0; j < n; ++ j) s += dp[j];
            for(int j = 0; j < n; ++ j)
            {
                dp2[j] = s - dp[j];
                sz = e[j].size();
                for(int ii = 0; ii < sz; ++ ii)dp2[j] -= dp[e[j][ii]];
                dp2[j] %= mod;
            }
            for(int j = 0; j < n; ++ j) dp[j] = dp2[j];
        }
        cout << dp[0] << endl;
        return 0;
    }

    剩下题待更。

  • 相关阅读:
    Hologres如何支持亿级用户UV计算
    飞猪基于 Serverless 的云+端实践与思考
    高德打车构建可观测性系统实践
    程序员写好技术文章的几点小技巧
    配置审计(Config)变配报警设置
    进入中国内地第31年的麦当劳 ,为什么还能不断吸引新消费人群?
    OceanBase再破纪录!核心成员陈萌萌:坚持HTAP就是坚持我们做数据库的初心
    找出有序数组中缺失的数字
    删除值重复的结点
    想交链表----若有缘 必相见
  • 原文地址:https://www.cnblogs.com/yjyl0098/p/15085443.html
Copyright © 2011-2022 走看看