csp-j模拟赛正睿4 比赛总结

· · 个人记录

csp-j模拟赛正睿4 比赛总结

题号 题目 得分
T1 额外的字符 预计得分 100pts,实际得分 100pts
T2 括号 预计得分 60pts,实际得分 60pts
T3 礼盒 预计得分 100pts,实际得分 20pts
T4 共享单车 0pts

题目总结

:::info[题面]

题目限制

时间限制:1000MS 内存限制:512MB

题目描述

给定两个字符串S和T,其中T是由S插入一个字符得到的,请你输出插入的字符的位置,如果有多个位置请输出最后面的那个

输入格式

第一行输入一个字符串S

第二行输入一个字符串T

输出格式

输出一个数字表示答案

提示

数据范围

对于50%的数据,|S|, |T| \leq 5000

对于100%的数据,|S|, |T| \leq 5 \times 10^5 :::

从左到右依次遍历两个字符串,遇到第一个不一样的即为插入的位置。

:::success[ACcode]

#include <bits/stdc++.h>
using namespace std;
int main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("extra.in", "r", stdin);
    freopen ("extra.out", "w", stdout);
    string s, t;
    cin >> s >> t;
    s = s + ' ';
    int pos = 0;
    for (int i = 0; i < s.size (); i++) {
        if (s[i] != t[i]) {
            pos = i + 1;
            break;
        }
    }
    cout << pos;
    return 0;
} 

:::

\Huge \color{19be6b} 100pts

T2

:::info[题面]

题目限制

时间限制:1000MS 内存限制:512MB

题目描述

给定一个字符串,里面有多个括号,括号里面的内容会被翻转,注意括号可以包含,翻转顺序从内到外,比如:

(a(bc)d(ef)) \rightarrow (acbdfe) \rightarrow efdbca

请你输出这个字符串去掉括号的结果。

输入格式

第一行输入一个字符串S

输出格式

输出一个字符串表示结果

提示

数据范围

对于30%的数据,|S| \leq 100

对于另外30%的数据,保证只出现一对括号

对于100%的数据,|S| \leq 5 \times 10^5

保证括号从里到外是可两两配对的

:::

没写出来。

:::warning[考场代码]

#include <bits/stdc++.h>
using namespace std;
string s, ans;
stack <int> kh;
inline void reverse (const int & beg, const int & end, string & s) {
    string t = s;
    int len = end - beg + 1;
    for (int i = 1; i <= len; i++)
        s[i + beg - 1] = t[end + 1 - i];
    return;
}
int main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("brackets.in", "r", stdin);
    freopen ("brackets.out", "w", stdout);
    char c = getchar ();
    while (c == ' ' || c == '\n') c = getchar ();
    while (c != ' ' && c != '\n') {
        s += c;
        c = getchar ();
    }
    for (int i = 0; i < s.size (); i++) {
        char c = s[i];
        if (c == '(') kh.push (i);
        else if (c == ')') {
            int beg = kh.top ();
            kh.pop ();
            reverse (beg, i, s);
            //reverse (beg, i);
        }
    }
    for (int i = 0; i < s.size (); i++)
        if (s[i] != '(' && s[i] != ')') putchar (s[i]);
    return 0;
}

一个 O(n^2) 的做法,肯定会爆。

:::

\Huge \color{2d8cf0}60pts

T3

:::info[题面]

题目限制

时间限制:1000MS 内存限制:512MB

题目描述

有n组礼盒,每组有一个红礼盒和一个蓝礼盒,里面分别有价值a_i和b_i的礼品。现在你想选x个红礼盒和n - x个蓝礼盒,每组里面只能恰好选一个。问你能获得的最大价值是多少

输入格式

第一行输入两个整数n, x

接下来n行,每行输入两个整数a_i, b_i

输出格式

输出一个数表示答案

提示

数据范围

对于30%的数据,n \leq 20

对于60%的数据,n \leq 2000

对于100%的数据,n \leq 2 \times 10^5, 0 \leq x \leq n, 1 \leq a_i, b_i \leq 10^9

:::

一个看起来简单实际上却不难的贪心(虽然我也不知道我怎么没想到)

开始先全部取蓝礼盒,算出它们的和,再用一个数组存下红礼盒价值减去蓝礼盒价值的差(即将蓝礼盒替换为红礼盒增加的价值),然后再将其降序排列(为了找到将蓝礼盒替换为红礼盒增加的价值最高的前x个),然后每次加上前x个

:::success[ACcode]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 5;
int n, k, ans;
int a[N];
inline bool cmp (const int & x, const int & y) { return x > y; }
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("gift.in", "r", stdin);
    freopen ("gift.out", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        int x, y; cin >> x >> y;
        a[i] = x - y; ans += y;
    }
    stable_sort (a + 1, a + n + 1, cmp);
    for (int i = 1; i <= k; i++) ans += a[i];
    cout << ans;
    return 0;
}

:::

:::warning[考场代码]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 5;
int n, x, ans, cnta, cntb;
bool vis[N];
struct node {
    int x, num, f;
    bool operator < (const node & p) const { return x > p.x; }
}a[N], b[N], c[N];
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("gift.in", "r", stdin);
    freopen ("gift.out", "w", stdout);
    cin >> n >> x;
    for (int i = 1; i <= n; i++) {
        cin >> c[2 * i - 1].x >> c[2 * i].x;
        c[2 * i - 1].num = c[2 * i].num = i;
        c[2 * i - 1].f = 0;
        c[2 * i].f = 1;
    }
    sort (c + 1, c + 2 * n + 1);
    for (int i = 1; ; i++) {
        if (cnta == x && cntb == n - x) break;
        if (cnta < x && c[i].f == 0 && !vis[c[i].num]) {
            cnta++;
            vis[c[i].num] = 1;
            ans += c[i].x;
        } else if (cntb < n - x && c[i].f == 1 && !vis[c[i].num]) {
            cntb++;
            vis[c[i].num] = 1;
            ans += c[i].x;
        }
    }
    cout << ans;
    return 0;
}

:::

\Huge\color{2d8cf0}20pts

T4

:::info

题目限制

时间限制:1000MS 内存限制:512MB

题目描述

小明需要使用n次共享单车,第i次在第a_i分钟。现在有m种骑车卡,每种卡需要花费c_i元,从购买开始后的k_i分钟(买的时候对应的时间算第一分钟)内可以免费骑车。现在小明需要保证他在需要单车时身上至少有一种骑车卡(不关心剩余时间,可以在需要的时刻立即购买),问小明最少要花多少钱?

输入格式

第一行输入一个整数n, m

第二行n个整数a_1, a_2, \cdots, a_n

接下来m行,每行两个整数c_i, k_i

输出格式

输出一个数字表示答案

提示

数据范围

对于30%的数据,n, m \leq 100

对于60%的数据,n, m \leq 500

对于100%的数据,n \leq 10^5, m\leq 500, a_1 < a_2 < \cdots < a_n, 1 \leq c_i, k_i \leq 10^9

:::

二分+动态规划。

dp[i] 表示前 i 次骑车所用的最小费用。

对于第 i 次骑车的时候购买第 j 种骑车卡的情况,令 k 是这张骑车卡失效后第一次骑车的序号

状态转移:

\Huge dp_{k−1}=min(dp_{k−1},dp_{i−1}+c_{j})

:::success[ACcode]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, M = 505;
int n, m;
int a[N], c[M], k[M], dp[N], len[N];
signed main () {
    ios::sync_with_stdio (NULL);
    cin.tie (nullptr); cout.tie (nullptr);
    freopen ("bike.in", "r", stdin);
    freopen ("bike.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) cin >> c[i] >> k[i];
    for (int i = 1; i <= n; i++) {
        dp[i] = 1e18;
        for (int j = 1; j <= m; j++) {
            while (a[i] - a[len[j] + 1] + 1> k[j]) len[j]++;
            dp[i] = min (dp[i], dp[len[j]] + c[j]);
        }
    }
    cout << dp[n];
    return 0;
}

:::

\Huge \color{ed3f14}0pts

总体评价

本次基本都在做题并试图打部分分,\color{white}{实际上玩了1.5h的html},但分数与预估的差距太远,下次做贪心的题还是自己多出几个数据(大样例都过了但就是PAC)。