Codeforces Round #578 (Div. 2)

Codeforces Round #578 (Div. 2)

A. Hotelier

  • 思路:水题

  • 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 n;
string s;
int a[10];

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 >> s;
    for (auto x: s){
        if (x == 'L'){
            for (int i = 0; i < 10; i ++ ){
                if (!a[i]){
                    a[i] = 1;
                    break;
                }
            }
        }
        else if (x == 'R'){
            for (int i = 9; i >= 0; i -- ){
                if (!a[i]){
                    a[i] = 1;
                    break;
                }
            }
        }
        else
            a[x - '0'] = 0;
    }
    for (int i = 0; i < 9; i ++ )
        cout << a[i];
    cout << a[9] << "
";
    return 0;
}

B. Block Adventure

  • 思路:贪心

  • 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 t, n, m, k;
int h[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 >> t;
    while (t -- ){
        cin >> n >> m >> k;
        for (int i = 1; i <= n; i ++ )
            cin >> h[i];
        for (int i = 1; i < n; i ++ ){
            int x = min(h[i], h[i] - (h[i + 1] - k));
            m += x;
            if (m < 0)
                break;
        }
        if (m < 0)
            cout << "NO
";
        else
            cout << "YES
";
    }
    return 0;
}

C. Round Corridor

  • 思路:思维题 圆环被分成了(gcd(n,m))块 判断是不是在同一块即可

  • 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  n, m, q, gcd_, sx, sy, ex, ey;

ll  calc(ll  x, ll  y){
    if (x == 1)
        return y / (n / gcd_);
    return y / (m / gcd_);
}

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 >> q;
    gcd_ = gcd(n, m);
    while (q -- ){
        cin >> sx >> sy >> ex >> ey;
        // cout << calc(sx, sy - 1) << " " << calc(ex, ey - 1) << "
";
        if (calc(sx, sy - 1) == calc(ex, ey - 1))
            cout << "YES
";
        else
            cout << "NO
";
    }
    return 0;
}

D. White Lines(赛后补)

  • 思路:比赛时想出来了 没时间写了 赛后十几分钟敲出来了 前缀和处理

  • 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 = 2010;

int n, k, ans, res;
int x[N][N], y[N][N], cnt_x[N][N], cnt_y[N][N], sum_x[N][N], sum_y[N][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 >> k;
    for (int i = 1; i <= n; i ++ ){
        string s;
        cin >> s;
        for (int j = 1; j <= n; j ++ ){
            cnt_x[i][j] = cnt_x[i - 1][j] + (s[j - 1] == 'B');
            cnt_y[i][j] = cnt_y[i][j - 1] + (s[j - 1] == 'B');
        }
    }
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= n - k + 1; j ++ )
            if (cnt_y[i][n] && cnt_y[i][j + k - 1] - cnt_y[i][j - 1] == cnt_y[i][n])
                y[i][j] = 1;
    for (int i = 1; i <= n - k + 1; i ++ )
        for (int j = 1; j <= n; j ++ )
            if (cnt_x[n][j] && cnt_x[i + k - 1][j] - cnt_x[i - 1][j] == cnt_x[n][j])
                x[i][j] = 1;
    for (int i = 1; i <= n; i ++ ){
        for (int j = 1; j <= n; j ++ ){
            sum_x[i][j] = sum_x[i][j - 1] + x[i][j];
            sum_y[i][j] = sum_y[i - 1][j] + y[i][j];
        }
    }
    for (int i = 1; i <= n - k + 1; i ++ )
        for (int j = 1; j <= n - k + 1; j ++ )
            ans = max(ans, sum_x[i][j + k - 1] - sum_x[i][j - 1] + sum_y[i + k - 1][j] - sum_y[i - 1][j]);
    for (int i = 1; i <= n; i ++ )
        res += (cnt_x[n][i] == 0) + (cnt_y[i][n] == 0);
    // cout << ans << " " << res << "
";
    cout << ans + res << "
";
    return 0;
}

E. Compress Words

  • 思路:KMP即可

  • 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 n, len, len_;
string s, t;

void get_nxt(string s, int *nxt){
    int j, k;
    nxt[0] = -1;
    j = 0, k = -1;
    while (j < len_){
        if (k == -1 || s[j] == s[k]){
            j ++ , k ++ ;
            nxt[j] = k;
        }
        else
            k = nxt[k];
    }
}

int kmp(string s, string t){
    int nxt[len_ + 1];
    int i, j;
    i = j = 0;
    get_nxt(t, nxt);
    while (i < s.length()){
        if (j == -1 || s[i] == t[j])
            i ++ , j ++ ;
        else
            j = nxt[j];
        if (j == len_)
            return len_;
    }
    return j;
}

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;
    cin >> s;
    for (int i = 2; i <= n; i ++ ){
        cin >> t;
        len = s.length(), len_ = t.length();
        int len__ = max(0, len - len_);
        int tmp = kmp(s.substr(len__), t);
        for (int j = tmp; j < len_; j ++ )
            s += t[j];
    }
    cout << s << "
";
    return 0;
}
原文地址:https://www.cnblogs.com/Misuchii/p/13917442.html