CSP-J2022游记&题解

· · 个人记录

游记

总体来说本次比赛除了T3很恶心以外其他的题都很水。

虽然我这个蒟蒻才315(100 100 15 100),但是写题解还是够了的(

我们考场在北京十二中,系统是NOI Linux,里面有完好的装着C++插件的VSCode,可以开自动格式化,可以F5光速运行。环境比去年好多了,去年PJ我连编译器都没用上导致100 0 0 0险些爆零

T1

快速幂。

过程中判断如果res > 1e9 || a > 1e9即超过范围,否则输出答案。

时间复杂度\Theta(log_2b),b<=10^{18}

100pts Code:

#include <bits/stdc++.h>
using namespace std;
long long a, b, ans = 1;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("pow.in", "r", stdin);
    freopen("pow.out", "w", stdout);
    cin >> a >> b;
    while (b)
    {
        if (b & 1)
            ans = ans * a;
        b >>= 1;
        if (b)
            a = a * a;
        if (ans > 1e9 || a > 1e9)
        {
            cout << -1;
            return 0;
        }
    }
    cout << ans;
    return 0;
}

T2

二分。

条件是x*y=n,x+y=m(m=n-e*d+2)

总共可能的x范围为1<=x<=m/2,此时y=m-x

很显然,x,y越接近,乘积越大,满足单调性。二分查找合适的x,使x*y=n。

时间复杂度$\Theta(k*log_2m)$,$k<=10^5,m<=10^9

100pts Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
ll n, e, d, m;
bool flag;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("decode.in", "r", stdin);
    freopen("decode.out", "w", stdout);
    cin >> T;
    while (T--)
    {
        flag = false;
        cin >> n >> e >> d;
        m = n - e * d + 2;
        ll l = 0, r = m >> 1;
        while (l <= r)
        {
            ll mid = l + r >> 1, q = m - mid;
            if (mid * q == n)
            {
                flag = true;
                cout << mid << ' ' << q << '\n';
                break;
            }
            if (mid * q < n)
                l = mid + 1;
            else
                r = mid - 1;
        }
        if (!flag)
            cout << "NO\n";
    }
    return 0;
}

T3

模拟。

超级大模拟,恶心死我了!!!

思路:先处理0\&(...)和1|(...)的情况(直接全删,第二行答案对应的+1),然后处理括号,接着处理AND,最后处理OR。

时间复杂度:\Theta(|s|),|s|<=10^6

代码我不放了,就35pts

upd 11-7:突然又想到一种方法,就是先用stack把括号和优先级抹掉,然后再做。我是傻*。

后来写一半放弃了,仅供参考

T4

动态规划。

根据数据范围x,y<=10^9很显然不能把坐标作为动态规划下标。

定义f[i][j]表示第i个点之前用j次自建点后联通的最大值。

这里注意一个细节,任何一个f[i][j]都可以作为程序的结束状态。

初始化f[i][0]=1

转移方程:

(i_x>=j_x\&\&i_y>=j_y)f[i][k]=max(f[i][k],f[j][k-dis(i,j)]+dis(i,j)+1 最后输出$max\{f[i][j]+m-j\}$即可(没用完的自建点不要浪费,在结束状态后面接就行) 时间复杂度:$\Theta(nk),n<=500,k<=100

100pts Code:

#include <bits/stdc++.h>
using namespace std;
int n, m, f[505][105];
struct node
{
    int x, y;
    inline const bool operator<(const node &t) const
    {
        return x == t.x ? y < t.y : x < t.x;
    }
} e[505];
inline int dis(int x, int y)
{
    return abs(e[x].x - e[y].x) + abs(e[x].y - e[y].y) - 1;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("point.in", "r", stdin);
    freopen("point.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> e[i].x >> e[i].y;
    sort(e + 1, e + 1 + n);
    for (int i = 1; i <= n; i++)
        f[i][0] = 1;
    for (int i = 2; i <= n; i++)
        for (int j = 1; j < i; j++)
            if (e[i].x >= e[j].x && e[i].y >= e[j].y)
                for (int k = dis(i, j); k <= m; k++)
                    f[i][k] = max(f[i][k], f[j][k - dis(i, j)] + dis(i, j) + 1);
    int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= m; j++)
            ans = max(ans, f[i][j] + m - j);
    cout << ans;
    return 0;
}