• Codeforces Round #598 (Div. 3)


    Codeforces Round #598 (Div. 3)

    A. Payment Without Change

    • 思路:水题 没开long long wa2(考场降智

    • 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 q, a, b, n, s, x, y;
    
    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 >> a >> b >> n >> s;
            if (a * n + b < s){
                cout << "NO
    ";
                continue;
            }
            x = s / n, y = s % n;
            if (y <= b)
                cout << "YES
    ";
            else
                cout << "NO
    ";
        }
        return 0;
    }
    

    B. Minimize the Permutation

    • 思路:模拟

    • 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], pos[N];
    bool vis[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;
            memset(vis, false, sizeof(vis));
            for (int i = 1; i <= n; i ++ ){
                cin >> a[i];
                pos[a[i]] = i;
            }
            for (int i = 1; i <= n; i ++ ){
                for (int j = pos[i]; j >= 2; j -- ){
                    if (vis[j])
                        break;
                    if (a[j] > a[j - 1])
                        break;
                    swap(a[j], a[j - 1]);
                    swap(pos[a[j]], pos[a[j - 1]]);
                    vis[j] = true;
                }
            }
            for (int i = 1; i < n; i ++ )
                cout << a[i] << " ";
            cout << a[n] << "
    ";
        }
        return 0;
    }
    

    C. Platforms Jumping

    • 思路:考场降智 当时没想出来 赛后马上想出来了 疯狂改了条件就过了

    • 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 n, m, d, cnt;
    int c[N], ans[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 >> n >> m >> d;
        for (int i = 1; i <= m; i ++ ){
            cin >> c[i];
            cnt += c[i];
        }
        int left = n - cnt;
        int div = left / (m + 1);
        int mod = left % (m + 1);
        if (mod)
            div ++ ;
        if (div >= d){
            cout << "NO
    ";
            return 0;
        }
        if (n - cnt <= m + 1){
            int tmp = n - cnt, tot = 1;
            for (int i = 1; i <= n; i ++ ){
                if (tmp){
                    i ++ ;
                    tmp -- ;
                }
                for (int j = i; j < i + c[tot]; j ++ )
                        ans[j] = tot;
                i += c[tot] - 1;
                tot ++ ;
            }
        }
        else{
            int x = (n - cnt) / (m + 1), y = (n - cnt) % (m + 1), tot = 1;
            for (int i = 1; i <= n; i ++ ){
                if (y){
                    i ++ ;
                    y -- ;
                }
                i += x;
                for (int j = i; j < i + c[tot]; j ++ )
                        ans[j] = tot;
                i += c[tot] - 1;
                tot ++ ;
            }
        }
        cout << "YES
    ";
        for (int i = 1; i < n; i ++ )
            cout << ans[i] << " ";
        cout << ans[n] << "
    ";
        return 0;
    }
    

    D. Binary String Minimizing

    • 思路:暴力 每次优先把0交换到最前面

    • 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 q, n, k, cnt1, tot;
    ll cnt[N], pos[N];
    string s;
    
    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 >> k >> s;
            cnt1 = 0, tot = 0;
            for (int i = 0; i < n; i ++ ){
                if (s[i] == '1')
                    cnt1 ++ ;
                else{
                    cnt[tot] = cnt1;
                    pos[tot] = i;
                    tot ++ ;
                }
            }
            for (int i = 0; i < tot; i ++ ){
                if (cnt[i] <= k){
                    swap(s[pos[i]], s[pos[i] - cnt[i]]);
                    k -= cnt[i];
                }
                else{
                    swap(s[pos[i]], s[pos[i] - k]);
                    break;
                }
            }
            cout << s << "
    ";
        }
        return 0;
    }
    

    F. Equalizing Two Strings

    • 思路:如果两个串有不同的元素直接输出NO 如果有出现次数大于等于2的元素 直接输出YES 然后判断两个串的逆序数之和是否为偶数即可

    • 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 merge(char *str, char *ass, int beg, int mid, int end){
        int cnt = 0;
        int i = beg, j = mid + 1, k = beg;
        while (i <= mid && j <= end){
            if (str[i] > str[j]){
                cnt += mid - i + 1;
                ass[k ++ ] = str[j ++ ];
            }
            else
                ass[k ++ ] = str[i ++ ];
        }
        while (i <= mid)
            ass[k ++ ] = str[i ++ ];
        while (j <= end)
            ass[k ++ ] = str[j ++ ];
        for (int l = beg; l < k; l ++ )
            str[l] = ass[l];
        return cnt;
    }
    
    int calc(char *str, char *ass, int beg, int end){
        int cnt = 0;
        if (end > beg){
            int mid = (end + beg) / 2;
            cnt += calc(str, ass, beg, mid);
            cnt += calc(str, ass, mid + 1, end);
            cnt += merge(str, ass, beg, mid, end);
        }
        return cnt;
    }
    
    const int N = 2e5 + 10;
    
    int q, n;
    char s[N], 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 -- ){
            cin >> n >> s >> t;
            int cnt1[30] = {0}, cnt2[30] = {0};
            for (int i = 0; i < n; i ++ ){
                cnt1[s[i] - 'a'] ++ ;
                cnt2[t[i] - 'a'] ++ ;
            }
            bool flag1 = true, flag2 = false;
            for (int i = 0; i < 26; i ++ ){
                if (cnt1[i] != cnt2[i]){
                    flag1 = false;
                    break;
                }
            }
            if (!flag1){
                cout << "NO
    ";
                continue;
            }
            for (int i = 0; i < 26; i ++ ){
                if (cnt1[i] >= 2 || cnt2[i] >= 2){
                    flag2 = true;
                    break;
                }
            }
            if (flag2){
                cout << "YES
    ";
                continue;
            }
            char tmp1[N], tmp2[N];
            int c1 = calc(s, tmp1, 0, n - 1), c2 = calc(t, tmp2, 0, n - 1);
            if ((c1 + c2) % 2 == 0)
                cout << "YES
    ";
            else
                cout << "NO
    ";
        }
        return 0;
    }
    
  • 相关阅读:
    牛客(46)孩子们的游戏(圆圈中最后剩下的数)
    牛客(45)扑克牌顺子
    牛客(44)翻转单词顺序列
    牛客(43)左旋转字符串
    牛客(42)和为S的两个数字
    牛客(41)和为S的连续正数序列
    牛客(40)数组中只出现一次的数字
    牛客(39)平衡二叉树
    牛客(38)二叉树的深度
    牛客(37)数字在排序数组中出现的次数
  • 原文地址:https://www.cnblogs.com/Misuchii/p/11801549.html
Copyright © 2020-2023  润新知