[CSP-J]2023年正睿-5 比赛总结

· · 个人记录

[CSP-J]2023年正睿-5 比赛总结

比赛得分 270 pts / 400 pts,预计 260 pts

题目思路

T1

:::info[description]

:::

正常模拟即可,只不过在每次做乘法的时候用 __int128 或 unsigned long long,不然要溢出。

:::success[AC code]

#include <bits/stdc++.h>
#define int long long
using namespace std;
long long n, p, sum = 1;
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("power.in", "r", stdin);
    freopen ("power.out", "w", stdout);
    cin >> n;
    p = n % 10;
    n /= 10;
    for (int i = 1; i <= p; i++) {
        __int128 a = sum * n;
        if (a >= 1e18) {
            cout << "inf";
            return 0;
        }
        sum = a;
    }
    cout << sum;
    return 0;
}

:::

\Huge{\color{#19be6b} 100pts}

T2

:::info[description]

:::

用数组存下每个数字出现的次数及每次出现所在的位置,最后调用即可

:::success[AC code]

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, q;
struct node {
    int x;
    vector <int> pos;
} a[N];
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("query.in", "r", stdin);
    freopen ("query.out", "w", stdout);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        a[x].x++;
        a[x].pos.push_back(i);
    }
    while (q--) {
        int x, k; cin >> x >> k;
        if (x > 1000) {
            cout << "-1\n";
            continue;
        }
        if (a[x].x < k) {
            cout << "-1\n";
            continue;
        }
        cout << a[x].pos[k - 1] << '\n';
    }
    return 0;
}

:::

\Huge{\color{19be6b}{100pts}}

T3

:::info[description]

:::

用双指针,先把第二个指针持续向后移,如果每次移动后序列仍合法,则序列数加 j~-~i~+~1 ;如不合法,则将 i 持续向后移,直到该序列合法

:::success[AC code]

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, k, ans;
int a[N], cnt[N];
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("ambush.in", "r", stdin);
    freopen ("ambush.out", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int i = 1, j = 0;
    while(j < n){
        j++, cnt[a[j]]++;
        if (cnt[a[j]] < k) ans += j - i + 1;
        else {
            while (cnt[a[j]] == k) cnt[a[i]]--, i++;
            ans += j - i + 1;
        }
    }
    cout << ans << endl;
    return 0;
}

:::

:::warning[考场代码]

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, cnt, maxn;
int a[N];
vector <int> t;
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("ambush.in", "r", stdin);
    freopen ("ambush.out", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        maxn = max (maxn, a[i]);
    }
    if (k == 1) {
        cout << 0;
        return 0;
    }
    t.resize (maxn + 1);
    int mx = 1;
    for (int i = 1, j = 1; i <= n; ) {
        t[a[j]]++;
        if (t[a[j]] > mx) mx = t[a[j]];
        if (mx >= k) {
            mx = 1;
            t.clear ();
            t.resize (maxn + 1);
            i++;
            j = i;
        } else {
            j++;
            cnt++;
        }
        if (j > n) {
            mx = 1;
            t.clear ();
            t.resize (maxn + 1);
            i++;
            j = i;
        }
    }
    cout << cnt;
    return 0;
}

:::

\Huge{\color{2d8cf0}70~pts}

T4

:::info[description]

:::

DP

状态定义 dp[i][j] :以 i 结尾的子串总和为 j 的方案数

状态转移 :

:::success[AC code]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005, mod = 99824435, m = 1e4;
int n, ans;
int a[N], dp[N][m + 5];
signed main () {
    freopen ("zero.in", "r", stdin);
    freopen ("zero.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        dp[i][m / 2 - a[i]] = dp[i][m / 2 + a[i]] = 1;
        for (int j = 0; j <= m - a[i]; j++) 
            dp[i][j] = (dp[i][j] + dp[i - 1][j + a[i]]) % mod;
        for (int j = a[i]; j <= m; j++) 
            dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i]]) % mod;
        ans = (ans + dp[i][m / 2]) % mod;
    }
    cout << ans << endl;
    return 0; 
}

:::

\Huge{\color{ed3f14}0pts}

整体总结

本次总共用时1.5h(\color{white}不计算监考员操作而导致的时间的浪费),考的很(yì)好(tuó),非常满(wú)意(yǔ),especially 监考员给予我们的帮助。