zoukankan      html  css  js  c++  java
  • Codeforces Round #595 (Div. 3)

    Codeforces Round #595 (Div. 3)

    A. Yet Another Dividing into Teams

    • 思路:排序后找是否有两个相邻的数差值为1即可

    • AC代码


    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <map>
    #include <math.h>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    
    ll mult_mod(ll x, ll y, ll mod){
        return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
    }
    
    ll pow_mod(ll a, ll b, ll p){
        ll res = 1;
        while (b){
            if (b & 1)
                res = mult_mod(res, a, p);
            a = mult_mod(a, a, p);
            b >>= 1;
        }
        return res % p;
    }
    
    ll gcd(ll a, ll b){
        return b ? gcd(b, a % b) : a;
    }
    
    const int N = 110;
    
    int q, n;
    int a[N];
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("my_in.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> q;
        while (q -- ){
            cin >> n;
            for (int i = 1; i <= n; i ++ )
                cin >> a[i];
            sort(a + 1, a + n + 1);
            bool flag = false;
            for (int i = 1; i < n; i ++ ){
                if (a[i] == a[i + 1] - 1){
                    flag = true;
                    break;
                }
            }
            if (flag)
                cout << 2 << "
    ";
            else
                cout << 1 << "
    ";
        }
        return 0;
    }
    

    B2. Books Exchange (hard version)

    • 思路:dfs找环

    • AC代码


    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <map>
    #include <math.h>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    
    ll mult_mod(ll x, ll y, ll mod){
        return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
    }
    
    ll pow_mod(ll a, ll b, ll p){
        ll res = 1;
        while (b){
            if (b & 1)
                res = mult_mod(res, a, p);
            a = mult_mod(a, a, p);
            b >>= 1;
        }
        return res % p;
    }
    
    ll gcd(ll a, ll b){
        return b ? gcd(b, a % b) : a;
    }
    
    ll lcm(ll a, ll b){
        return a / gcd(a, b) * b;
    }
    
    const int N = 2e5 + 10;
    
    int q, n;
    int p[N], ans[N];
    
    int calc(int st, int now, int to, int step){
        if (st == to)
            return ans[now] = step;
        return ans[now] = calc(st, to, p[to], step + 1);
    }
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("my_in.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> q;
        while (q -- ){
            memset(ans, 0, sizeof(ans));
            cin >> n;
            for (int i = 1; i <= n; i ++ )
                cin >> p[i];
            for (int i = 1; i <= n; i ++ )
                if (!ans[i])
                    calc(i, i, p[i], 1);
            for (int i = 1; i <= n; i ++ ){
                if (i != n)
                    cout << ans[i] << " ";
                else
                    cout << ans[i] << "
    ";
            }
        }
        return 0;
    }
    
    

    C2. Good Numbers (hard version)

    • 思路:转换为三进制 每一位(geq2)的当前位清零 下一位(+1) 并标记 然后把标记位之前的全部清零 再转换成十进制就行了 没写标记位直接将最高位之前清零导致wa6(逃

    • AC代码


    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <map>
    #include <math.h>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    
    ll mult_mod(ll x, ll y, ll mod){
        return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
    }
    
    ll pow_mod(ll a, ll b, ll p){
        ll res = 1;
        while (b){
            if (b & 1)
                res = mult_mod(res, a, p);
            a = mult_mod(a, a, p);
            b >>= 1;
        }
        return res % p;
    }
    
    ll gcd(ll a, ll b){
        return b ? gcd(b, a % b) : a;
    }
    
    const int N = 100;
    
    int q;
    ll n, ans;
    int t[N];
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("my_in.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> q;
        while (q -- ){
            memset(t, 0, sizeof(t));
            ans = 0;
            cin >> n;
            int cnt = 0;
            ll tmp = n;
            while (tmp){
                tmp /= 3;
                t[cnt ++ ] = n - 3 * tmp;
                n = tmp;
            }
            int flag = -1;
            for (int i = 0; i < cnt; i ++ ){
                if (t[i] >= 2){
                    flag = i;
                    t[i] = 0;
                    t[i + 1] ++ ;
                }
            }
            if (flag != -1){
                for (int i = 0; i < flag; i ++ )
                    t[i] = 0;
            }
            ll fac = 1;
            for (int i = 0; i <= cnt; i ++ ){
                ans += t[i] * fac;
                fac *= 3;
            }
            cout << ans << "
    ";
        }
        return 0;
    }
    

    D2. Too Many Segments (hard version)

    • 思路:看了其他dalao博客的思路 将所有线段用set维护 然后从小到大枚举端点 将左端点覆盖到这个点的线段加入set中 然后线段数量大于k时移除右端点最大的线段 最后将右端点等于这个点的线段全部移除 (太妙了

    • AC代码


    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <map>
    #include <math.h>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    
    ll mult_mod(ll x, ll y, ll mod){
        return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
    }
    
    ll pow_mod(ll a, ll b, ll p){
        ll res = 1;
        while (b){
            if (b & 1)
                res = mult_mod(res, a, p);
            a = mult_mod(a, a, p);
            b >>= 1;
        }
        return res % p;
    }
    
    ll gcd(ll a, ll b){
        return b ? gcd(b, a % b) : a;
    }
    
    ll lcm(ll a, ll b){
        return a / gcd(a, b) * b;
    }
    
    typedef pair<ll, ll> pii;
    
    const int N = 2e5 + 10;
    
    int n, k, l, r;
    vector<pii> vec[N];
    vector<int> ans;
    set<pii> st;
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("my_in.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> k;
        for (int i = 1; i <= n; i ++ ){
            cin >> l >> r;
            vec[l].push_back({r, i});
        }
        for (int i = 0; i < N; i ++ ){
            for (int j = 0; j < vec[i].size(); j ++ )
                st.insert(vec[i][j]);
            while (st.size() > k){
                ans.push_back(st.rbegin() -> second);
                st.erase( -- st.end());
            }
            while (!st.empty() && st.begin() -> first == i){
                st.erase(st.begin());
            }
        }
        cout << ans.size() << "
    ";
        for (int i = 0; i < ans.size(); i ++ ){
            if (i != ans.size() - 1)
                cout << ans[i] << " ";
            else
                cout << ans[i] << "
    ";
        }
        return 0;
    }
    
    /*
    7 2
    11 11
    9 11
    7 8
    8 9
    7 8
    9 11
    7 9
    
    3
    7 4 6
    */
    
    /*
    5 1
    29 30
    30 30
    29 29
    28 30
    30 30
    
    
    3
    4 1 5
    */
    
    /*
    6 1
    2 3
    3 3
    2 3
    2 2
    2 3
    2 3
    
    4
    6 5 3 1
    */
    

    E. By Elevator or Stairs?

    • 思路:比较简单的dp 详细见代码

    • AC代码


    #include <algorithm>
    #include <iomanip>
    #include <iostream>
    #include <map>
    #include <math.h>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    
    ll mult_mod(ll x, ll y, ll mod){
        return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
    }
    
    ll pow_mod(ll a, ll b, ll p){
        ll res = 1;
        while (b){
            if (b & 1)
                res = mult_mod(res, a, p);
            a = mult_mod(a, a, p);
            b >>= 1;
        }
        return res % p;
    }
    
    ll gcd(ll a, ll b){
        return b ? gcd(b, a % b) : a;
    }
    
    ll lcm(ll a, ll b){
        return a / gcd(a, b) * b;
    }
    
    const int N = 2e5 + 10;
    const int M = 3;
    
    int n, c;
    int a[N], b[N];
    int dp[N][M];
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("my_in.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> c;
        memset(dp, 0x3f, sizeof(dp));
        dp[1][1] = 0, dp[1][2] = c;
        for (int i = 1; i < n; i ++ )
            cin >> a[i];
        for (int i = 1; i < n; i ++ )
            cin >> b[i];
        for (int i = 1; i < n; i ++ ){
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + a[i]);
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][2] + a[i]);
            dp[i + 1][2] = min(dp[i + 1][2], dp[i][1] + c + b[i]);
            dp[i + 1][2] = min(dp[i + 1][2], dp[i][2] + b[i]);
        }
        for (int i = 1; i <= n; i ++ ){
            if (i != n)
                cout << min(dp[i][1], dp[i][2]) << " ";
            else
                cout << min(dp[i][1], dp[i][2]) << "
    ";
        }
        return 0;
    }
    /*
    0 2
    7 8
    13 17
    31 18
    24 28
    40 35
    53 36
    37 46
    54 40
    57 45
    */
    /*
    0 1
    3 2
    4 4
    7 7
    8 11
    11 13
    14 13
    14 15
    18 16
    17 19
    */
    
  • 相关阅读:
    SQL Server 2019 安装错误
    SQL Server Cardinality Estimation 简介
    SQL Server 要避免的编程坏习惯
    SQL Server 中SET XACT_ABORT设置的作用
    SQL Server nested transaction try...catch 处理模板
    SVN迁移至gitlab
    安装单机版RabbitMQ
    校验MySQL主从数据并修复
    使用Xtrabackup对数据库进行部分备份恢复
    MySQL之Xtrabackup备份还原与binlog恢复
  • 原文地址:https://www.cnblogs.com/Misuchii/p/11734343.html
Copyright © 2011-2022 走看看