2023CSP-J复赛模拟总结

· · 个人记录

T1:小苹果

由于这道题不久前做过,所以印象很深,上午我讲过了,这里不再赘述。\ AC

#include <iostream>
using namespace std;
int ans1(int n)
{
    if (n == 0)
    {
        return 0;
    }
    return ans1(n - (n + 2) / 3) + 1;
}
int ans2(int n)
{
    if (n % 3 == 1)
    {
        return 1;
    }
    return ans2(n - (n + 2) / 3) + 1;
}
int main()
{
    int n;
    cin >> n;
    cout << ans1(n) << " " << ans2(n);
    return 0;
}

T2:公路

由于这一题之前也做过,当时不会,看了题解,印象也很深。\ AC

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define int long long
#define INF 1e9
main()
{
    int n, d;
    cin >> n >> d;
    int a[n], v[n];
    for (int i = 0; i < n - 1; ++i) cin >> v[i];
    for (int i = 0; i < n; ++i) cin >> a[i], a[i];
    int minPrice = INF;
    int nowPrice = 0;
    double surpV = 0;
    for (int i = 0; i < n - 1; ++i)
    {
        minPrice = min(minPrice, a[i]);
        nowPrice += ceil(v[i] * 1.0 / d - surpV) * minPrice;
        surpV = ceil(v[i] * 1.0 / d - surpV) - (v[i] * 1.0 / d - surpV);
    }
    cout << nowPrice << endl;
    return 0;
}

T3:一元二次方程

这道题之前把我搞得死去活来,这次考试时也是,就是突然看了看题目,发现根据它的思路来就能AC,后面上洛谷下了个附件,纠了几分钟的错。\ AC

#include <iostream>
#include <cmath>
#include <algorithm> 
using namespace std;
int simp(int r)
{
    for (int i = sqrt(r); i >= 1; --i)
    {
        if (r % (i * i) == 0)
        {
            return i;
        }
    }
}
int main()
{
    int t, m;
    cin >> t >> m;
    while (t--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        int delta = b * b - 4 * a * c;
        if (delta < 0)
        {
            cout << "NO\n";
            continue;
        }
        if ((int)sqrt(delta) * (int)sqrt(delta) == delta)
        {
            int q = 2 * a;
            int p;
            if (q < 0)
            {
                q *= -1;
                p = b + sqrt(delta);
            }
            else
            {
                p = -b + sqrt(delta);
            }
            int g = __gcd(p, q);
            p /= g;
            q /= g;
            if (q < 0)
            {
                p *= -1;
                q *= -1;
            }
            if (q == 1)
            {
                printf("%d", p);
            }
            else
            {
                printf("%d/%d", p, q);
            }
        }
        else
        {
            int p1, q1, p2, q2, r;
            p1 = -b;
            q1 = 2 * a;
            int g = __gcd(p1, q1);
            p1 /= g;
            q1 /= g;
            if (q1 < 0)
            {
                q1 *= -1;
                p1 *= -1;
            }
            q2 = 2 * a;
            r = delta;
            p2 = simp(r);
            r /= p2 * p2;
            g = __gcd(p2, q2);
            p2 /= g;
            q2 /= g;
            if (q2 < 0)
            {
                p2 *= -1;
                q2 *= -1;
            }
            if (p1 != 0)
            {
                if (q1 == 1)
                {
                    printf("%d+", p1);
                }
                else
                {
                    printf("%d/%d+", p1, q1);
                }
            }
            p2 = abs(p2);
            if (p2 == 1 && q2 == 1)
            {
                printf("sqrt(%d)", r);
            }
            else if (q2 == 1)
            {
                printf("%d*sqrt(%d)", p2, r);
            }
            else if (p2 == 1)
            {
                printf("sqrt(%d)/%d", r, q2);
            }
            else
            {
                printf("%d*sqrt(%d)/%d", p2, r, q2);
            }
        }
        cout << endl;
    }
    return 0;
}

T4:旅游巴士

看到后自知之明,写了一串BFS就完工了,在把k=0的时候特判。\ WA:15pts

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m, k;
#define INF 5e6
struct Node
{
    int v, a;
};
struct Node2
{
    int u, t;
};
int main()
{
    cin >> n >> m >> k;
    vector<vector<Node>> r(n + 1);
    for (int i = 0; i < m; ++i)
    {
        int u, v, a;
        cin >> u >> v >> a;
        r[u].push_back({ v, a });
    }
    queue<Node2> q;
    vector<int> vis(n + 1, false);
    q.push({ 1, 0 });
    for (int t = 1; t <= INF; ++t)
    {
        if (q.empty())
        {
            cout << "-1\n";
            return 0;
        }
        Node2 pt = q.front();
        q.pop();
        if (k == 1 && vis[pt.u])
        {
            continue;
        }
        vis[pt.u] = true;
        if (pt.u == n && pt.t % k == 0)
        {
            cout << pt.t << endl;
            return 0;
        }
        for (int i = 0; i < r[pt.u].size(); ++i)
        {
            if (r[pt.u][i].a <= pt.t)
            {
                q.push({ r[pt.u][i].v, pt.t + 1 });
            }
        }
        q.push({ 1, t * k });
    }
    cout << "-1\n";
    return 0;
}