BestCoder Round #41 A B C

A:52张牌,枚举每种可以的情况,统计已经有x张牌了,需要换的就是5 - x张,不断维护最小值就可以了

B:败的情况只有2种,两个串奇偶性不同,两个串完全相同,所以简单统计一下就可以了,最后除上总情况C(n, 2)即可

C:这题看了官方题解才会的,dp[i][j] = dp[i - j][j] + dp[i - j][j - 1],自己也是没想到,弱爆了,具体的可以看官方题解,有的递推式子,然后滚动数组一发就可以了

代码:

A:

#include 
#include 
#include 
using namespace std;

int t;
char s[15];
int vis[105];

int gao1(int x) {
    int cnt = 5;
    for (int i = 0; i <= 4; i++) {
        if (vis[x + i]) cnt--;
    }
    return cnt;
}

int gao2(int x) {
    int cnt = 5;
    for (int i = 9; i < 13; i++) {
        if (vis[x + i]) cnt--;
    }
    if (vis[x]) cnt--;
    return cnt;
}

int cal() {
    int ans = 10;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j <= 8; j++)
            ans = min(ans, gao1(i * 13 + j));
        ans = min(ans, gao2(i * 13));
    }
    return ans;
}

int main() {
    scanf("%d", &t);
    while (t--) {
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < 5; i++) {
            scanf("%s", s);
            int tmp = (s[0] - 'A') * 13;
            int sum = 0;
            for (int i = 1; s[i]; i++)
                sum = sum * 10 + s[i] - '0';
            tmp += sum - 1;
            vis[tmp] = 1;
        }
        printf("%d\n", cal());
    }
    return 0;
}

B:

#include 
#include 
#include 
#include 
#include 
using namespace std;

const int N = 20005;

int t, n;
string str[N];
int len[N * 10];
int odd, even;

int gcd(int a, int b) {
    if (!b) return a;
    return gcd(b, a % b);
}

int main() {
    cin >> t;
    while (t--) {
        cin >> n;
        odd = even = 0;
        int Max = 0;
        memset(len, 0, sizeof(len));
        for (int i = 0; i < n; i++) {
            cin >> str[i];
            int tmp = str[i].length();
            Max = max(Max, tmp);
            if (tmp % 2) odd++;
            else even++;
            len[tmp]++;
        }
        if (n < 2)
            printf("0/1\n");
        else {
            int zi = 0;
            int mu = n * (n - 1) / 2;
            sort(str, str + n);
            for (int i = 1; i <= Max; i++) {
                if (i % 2) {
                    zi += len[i] * even;
                }
            }
            int cnt = 1;
            for (int i = 1; i < n; i++) {
                if (str[i] == str[i - 1]) {
                    cnt++;
                } else {
                    zi += cnt * (cnt - 1) / 2;
                    cnt = 1;
                }
            }
            zi += cnt * (cnt - 1) / 2;
            int d = gcd(zi, mu);
            printf("%d/%d\n", zi / d, mu / d);
        }
    }
    return 0;
}

C:

#include 
#include 
#include 
#include 
using namespace std;

const int MOD = 998244353;

int t, n, c, l, r;

int dp[505][505];

int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d%d", &n, &c, &l, &r);
        l -= c; r -= c;
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        int ans = 0;
        for (int i = 1; i <= r; i++) {
            memset(dp[i % 500], 0, sizeof(dp[i % 500]));
            for (int j = 1; j * (j + 1) / 2 <= i; j++) {
                dp[i % 500][j] = (dp[(i - j) % 500][j] + dp[(i - j) % 500][j - 1]) % MOD;
                if (i >= l) ans = (ans + dp[i % 500][j]) % MOD;
            }
        }
        if (l == 0) ans++;
        if (r == n) ans--;
        ans = (ans + MOD) % MOD;
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(BestCoder Round #41 A B C)