CSP七连测Day2题解

· · 题解

T1 月名排序

简单的不能再简单,题目怎么说我们就怎么做(主要考查一个细心和阅读能力)

有人全程if打表,有点low

#include <bits/stdc++.h>
using namespace std;
string month[13] = {
    "",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"};
string new_month[13];
string in_month;
int main()
{
    for (int i = 1; i <= 12; i++)
        new_month[i] = month[i];
    sort(new_month + 1, new_month + 12 + 1);
    cin >> in_month;
    for (int i = 1; i <= 12; i++)
        if (in_month == new_month[i])
            cout << month[i];
    return 0;
}

T2 第一是谁

因为数据范围给得很小,所以完全不用什么 map 呀或者优先队列啊啥的。咱老老实实好好写好暴力就好了

#include <bits/stdc++.h>
using namespace std;
int n, m;
string nam[1005];
int score[1005][5];
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> nam[i];
    sort(nam + 1, nam + n + 1);
    while (m--)
    {
        int op, a, b;
        string s;
        cin >> op;
        if (op == 1)
        {
            cin >> s >> a >> b;
            for (int i = 1; i <= n; i++)
                if (s == nam[i])
                    score[i][a] = b;
        }
        if (op == 2)
        {
            s = nam[1];
            a = score[1][1] + score[1][2] + score[1][3] + score[1][4];
            for (int i = 2; i <= n; i++)
            {
                if (score[i][1] + score[i][2] + score[i][3] + score[i][4]>a)
                {
                    s = nam[i];
                    a = score[i][1] + score[i][2]+ score[i][3] + score[i][4];
                }
            }
            cout << s << "\n";
        }
    }
    return 0;
}

T3 随机重复

绝大多数人应该是没有做过卡空间的题目,今天这道就是了。这题空间限制只有 64 MB,开10^7个 unsigned long long 就 10^7 * 8 /1024/1024≈77 MB 了。所以不能存下来每个随机数。

注意到了这个就很容易想到每个随机数都去看看再 a1∼am 中有没有出现过就可以了。时间复杂度 O(nlogm)

#include <bits/stdc++.h>
using namespace std;
unsigned long long seed;
unsigned long long rnd()
{
    seed = seed * seed + seed + 30ull;
    return seed;
}
int n, m;
unsigned long long a[100000 + 5];
bool vis[100000 + 5];
int main()
{
    cin >> seed >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> a[i];
    sort(a + 1, a + m + 1);
    for (int i = 1; i <= n; i++)
    {
        unsigned long long now = rnd();
        int pos = lower_bound(a + 1, a + m + 1, now) - a;
        if (pos == m + 1)
            continue;
        if (a[pos] == now)
            vis[pos] = true;
    }
    int ans = 0;
    for (int i = 1; i <= m; i++)
    {
        if (vis[i])
            ans++;
        if (i != m && a[i] == a[i + 1])
            vis[i + 1] = vis[i];
    }
    cout << ans;
    return 0;
}

T4 三三音乐

读懂题意后,按题意模拟就好了。只有一个坑点,直接套数学式子算的话,万一有 20000 这个浮点数会量化编码为 2^b ,而根据题意只能量化编码为 2^b-1

#include <bits/stdc++.h>
using namespace std;
const double L = 16;
const double R = 20000;
int a, t, b, n;
double info[100000 + 5];
int info_int[100000 + 5];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> a >> t >> b;
    n = a * t;
    for (int i = 1; i <= n; i++)
        cin >> info[i];
    // 这一部分也可以直接生成 2^b 个分界点然后二分
    // 我直接数学方法算了
    double p = (R - L) / (1 << b);
    for (int i = 1; i <= n; i++)
    {
        info_int[i] = (info[i] - 16) / p;
        if (info_int[i] == (1 << b))
            info_int[i]--;
    }
    // 可以转为二进制输出
    // 我直接位运算处理了
    for (int i = 1; i <= n; i++)
        for (int j = b - 1; j >= 0; j--)
            cout << ((info_int[i] >> j) & 1);
    return 0;
}