zoukankan      html  css  js  c++  java
  • Codeforces Round #599 (Div. 2)

    Codeforces Round #599 (Div. 2)

    A. Maximum Square

    • 思路:水题

    • 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 = 1010;
    
    int k, 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 >> k;
        while (k -- ){
            cin >> n;
            int cnt = 0;
            for (int i = 1; i <= n; i ++ )
                cin >> a[i];
            sort(a + 1, a + n + 1);
            for (int i = n; i >= 1; i -- ){
                if (cnt >= a[i])
                    break;
                cnt ++ ;
            }
            cout << cnt << "
    ";
        }
        return 0;
    }
    

    B. Character Swap

    • 思路:当a[i] != a[i]时 遍历是否存在a[j] == a[i] 若存在则交换a[j]和b[i] 否则判断是否存在a[i] == b[j] 若存在则交换a[j]和b[j] a[j]和b[i]

    • 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;
    }
    
    int k, n;
    bool flag;
    string a, b;
    vector<pair<int, int> > ans;
    
    int main(){
    #ifndef ONLINE_JUDGE
        freopen("my_in.txt", "r", stdin);
    #endif
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> k;
        while (k -- ){
            flag = true;
            ans.clear();
            vector<int> cnt(26);
            cin >> n >> a >> b;
            a.insert(0, " "), b.insert(0, " ");
            for (int i = 1; i <= n; i ++ ){
                cnt[a[i] - 'a'] ++ ;
                cnt[b[i] - 'a'] ++ ;
            }
            for (auto x: cnt){
                if (x & 1){
                    flag = false;
                    break;
                }
            }
            if (!flag){
                cout << "No
    ";
                continue;
            }
            cout << "Yes
    ";
            for (int i = 1; i <= n; i ++ ){
                if (a[i] != b[i]){
                    flag = false;
                    for (int j = i + 1; j <= n; j ++ ){
                        if (a[i] == a[j]){
                            flag = true;
                            ans.emplace_back(j, i);
                            swap(a[j], b[i]);
                            break;
                        }
                    }
                    if (!flag){
                        for (int j = i + 1; j <= n; j ++ ){
                            if (a[i] == b[j]){
                                flag = true;
                                ans.emplace_back(j, j);
                                ans.emplace_back(j, i);
                                swap(a[j], b[j]);
                                swap(a[j], b[i]);
                                break;
                            }
                        }
                    }
                }
            }
            cout << ans.size() << "
    ";
            for (auto x: ans)
                cout << x.first << " " << x.second << "
    ";
        }
        return 0;
    }
    

    C. Tile Painting

    • 思路:唯一分解定理后 对每一个p求个gcd即可

    • 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 = 1e6 + 10;
    
    ll n, ans;
    vector<ll> p;
    
    void divide(ll x){
        ll i;
        for (i = 2; i * i < x; i ++ ){
            if (x % i == 0){
                p.push_back(i);
                p.push_back(x / i);
            }
        }
        if (i * i == x)
            p.push_back(i);
    }
    
    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;
        divide(n);
        p.push_back(n);
        ans = p[0];
        for (int i = 0; i < p.size(); i ++ )
            ans = gcd(ans, p[i]);
        cout << ans << "
    ";
        return 0;
    }
    

    D. 0-1 MST

    • 思路:参考其他博客的思路 bfs暴力 求出连通块的个数 答案就是连通块的个数 - 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 = 1e5 + 10;
    int n, m, ans;
    int vis[N];
    set<int> st, G[N];
    
    void bfs(int x){
        queue<int> q;
        q.push(x);
        st.erase(x);
        while (!q.empty()){
            int y = q.front();
            q.pop();
            if (vis[y])
                continue;
            vis[y] = 1;
            for (auto it = st.begin(); it != st.end(); ){
                int v = *it;
                ++ it;
                if (G[y].find(v) == G[y].end()){
                    q.push(v);
                    st.erase(v);
                }
            }
        }
    }
    
    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 >> m;
        for (int i = 1; i <= n; i ++ )
            st.insert(i);
        for (int i = 1; i <= m; i ++ ){
            int x, y;
            cin >> x >> y;
            G[x].insert(y);
            G[y].insert(x);
        }
        for (int i = 1; i <= n; i ++ ){
            if (!vis[i]){
                bfs(i);
                ans ++ ;
            }
        }
        cout << ans - 1 << "
    ";
        return 0;
    }
    
  • 相关阅读:
    【LeetCode】155. Min Stack 最小栈
    【Java】修改Eclipse默认编码
    负数取模
    【算法第四版笔记】1.1 基础编程模型
    【计算方法】02
    【计算方法】01
    【Java】Java8中List排序
    选择排序算法
    排序算法
    【Java】Eclipse修改JSP文件默认编码
  • 原文地址:https://www.cnblogs.com/Misuchii/p/11841212.html
Copyright © 2011-2022 走看看