跳到正文

个人记录

CSP J & S 2023

Synthesize约 32 分钟
原文
文章信息与版本

文章信息

发布
2023年11月11日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 32 分钟

版本

  1. v1当前

这不是游记,这是题解。

今天把 JT4JT4 和 ST3ST3 调出来了,所以写一下。

  • CSP J2 2023CSP\ J2\ 2023

    • T1T1

    一眼题,注意到当且仅当当前苹果数 N≡1(mod3)N\equiv1\pmod3 时取走最后一个苹果,而最开始的最后一个苹果在被拿走前永远都是最后一个苹果,每次 NN 减去 ⌈N3⌉\lceil\frac{N}{3}\rceil 即可。

    C++
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int n, ans1, ans2;
    signed main() {
        cin >> n;
        while(n) {
            ++ans1;
            if(n % 3 == 1 & !ans2) {
                ans2 = ans1;
            }
            n -= ceil(n / 3.0);
        }
        cout << ans1 << " " << ans2 << endl;
        return 0;
    }
    • T2T2

    贪心,每次买油时足够前往之后第一个比这里的油单价便宜的加油站即可。注意到达下一个加油站时可能有剩余的油。

    C++
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int n, k, dis[100005], fulldis[100005], w[1000005], nxt, ccf_is_great, aaaa;
    signed main() {
       cin >> n >> k;
       for(int i = 2; i <= n; ++i) {
           cin >> dis[i];
           fulldis[i] = fulldis[i - 1] + dis[i];
       }
       for(int i = 1; i <= n; ++i) {
           cin >> w[i];
       }
       for(int i = 1; i < n; i = nxt) {
           nxt = 0;
           for(int j = i; j <= n; ++j) {
               if(w[j] < w[i]) {
                   nxt = j;
                   break;
               }
           }
           if(!nxt) {
               nxt = n;
           }
           int needdis = fulldis[nxt] - fulldis[i];
           needdis -= aaaa;
           int needoil = ceil(1.0 * needdis / k);
           aaaa = k * needoil - needdis;
           ccf_is_great += needoil * w[i];
       }
       cout << ccf_is_great << endl;
       return 0;
    }
    • T3T3

    大模拟可爱。

    首先特判 Δ≤0\Delta\le0,<0<0 时无解,=0=0 时有理数,约分即可,注意约分后分母为 11 的情况。

    随后取大根,若 a<0a<0 则将 a,b,ca,b,c 全部取反,此时 −b+Δ2a\frac{-b+\sqrt{\Delta}}{2a} 必为较大根。

    枚举因子化简根号,若根号下为 11 时特判,根号前系数为 11 特判,若 b=0b=0 时特判,约分后分母 =1=1 还是特判。

    具体细节在代码里。(但是这是考试的时候写的代码,非常乱)

    C++
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int T, M, a, b, c, udsq, fq1u, fq1d, fq2u, fq2d;
    signed main() {
       cin >> T >> M;
       while(T--) {
           cin >> a >> b >> c;
           if(a < 0) {
               a = -a;
               b = -b;
               c = -c;
           }
           udsq = b * b - 4 * a * c;
           if(udsq < 0) {
               cout << "NO" << endl;
           }
           if(udsq == 0) {
               int dvd = abs(__gcd(b, 2 * a));
               if((2 * a) / dvd == 1) {
                   cout << (-b) / dvd << endl;
               }
               if((2 * a) / dvd > 1) {
                   cout << (-b) / dvd << "/" << (2 * a) / dvd << endl;
               }
           }
           if(udsq > 0) {
               int dvd = abs(__gcd(b, 2 * a));
               if(b != 0) {
                   fq1u = (-b) / dvd;
                   fq1d = (2 * a) / dvd;
               }
               fq2d = 2 * a;
               fq2u = 1;
               for(int i = 2; i * i <= udsq; ++i) {
                   while(udsq % (i * i) == 0) {
                       fq2u *= i;
                       udsq /= (i * i);
                   }
               }
               int dvd2 = abs(__gcd(fq2u, fq2d));
               fq2u /= dvd2;
               fq2d /= dvd2;
               if(b == 0) {
                   if(fq2u == 1) {
                       if(udsq != 1) {
                           if(fq2d == 1) {
                               cout << "sqrt(" << udsq << ")" << endl;
                           }
                           else {
                               cout << "sqrt(" << udsq << ")/" << fq2d << endl;
                           }
                       }
                       else {
                           if(fq2d == 1) {
                               cout << 1 << endl;
                           }
                           else {
                               cout << "1/" << fq2d << endl;
                           }
                       }
                   }
                   else {
                       if(udsq != 1) {
                           if(fq2d == 1) {
                               cout << fq2u << "*sqrt(" << udsq << ")" << endl;
                           }
                           else {
                               cout << fq2u << "*sqrt(" << udsq << ")/" << fq2d << endl;
                           }
                       }
                       else {
                           if(fq2d == 1) {
                               cout << fq2u << endl;
                           }
                           else {
                               cout << fq2u << "/" << fq2d << endl;
                           }
                       }
                   }
               }
               else {
                   if(udsq == 1) {
                       int nu = fq2d * fq1u + fq1d * fq2u;
                       int nd = fq2d * fq1d;
                       int dvd3 = abs(__gcd(nu, nd));
                       nu /= dvd3;
                       nd /= dvd3;
                       if(nd == 1) {
                           cout << nu << endl;
                       }
                       else {
                           cout << nu << "/" << nd << endl;
                       }
                       continue;
                   }
                   if(fq1d == 1) {
                       cout << fq1u;
                   }
                   if(fq1d > 1) {
                       cout << fq1u << "/" << fq1d;
                   }
                   if(a > 0) {
                       cout << "+";
                   }
                   else {
                       cout << "-";
                   }
                   if(fq2u == 1) {
                       if(udsq != 1) {
                           if(fq2d == 1) {
                               cout << "sqrt(" << udsq << ")" << endl;
                           }
                           else {
                               cout << "sqrt(" << udsq << ")/" << fq2d << endl;
                           }
                       }
                       else {
                           if(fq2d == 1) {
                               cout << 1 << endl;
                           }
                           else {
                               cout << "1/" << fq2d << endl;
                           }
                       }
                   }
                   else {
                       if(udsq != 1) {
                           if(fq2d == 1) {
                               cout << fq2u << "*sqrt(" << udsq << ")" << endl;
                           }
                           else {
                               cout << fq2u << "*sqrt(" << udsq << ")/" << fq2d << endl;
                           }
                       }
                       else {
                           if(fq2d == 1) {
                               cout << fq2u << endl;
                           }
                           else {
                               cout << fq2u << "/" << fq2d << endl;
                           }
                       }
                   }
               }
           }
       }
       return 0;
    }
    • T4T4

    还是一眼题,但我脑子飞了,只有 8080,痛失 AKAK。

    类似最短路,但这时不按照路程长度排序,按照大于这条路径的最早出发时间的 kk 的倍数中最小的一个加上路程长度进行排序。

    新加一条边时路径长度加一,最早出发时间为之前的最早出发时间和这条路的开放时间减去之前的路径长度的最大值。

    disdis 数组需要有两个维度 ii 和 jj,表示到第 ii 个点且路程长度  mod k=j\bmod k=j 的最短路径长度,显然答案就是 disn,0dis_{n,0}。

    C++
    #include <bits/stdc++.h>
    using namespace std;
    int n, m, k, to[40005], w[40005], hd[40005], nxt[40005], x, y, z, ndis;
    int ltt[40005][405];
    int nss, a, newt, news, t, tw;
    // This code can solve bus2.in in about 0.74s on Windows, but 0.972~1.082s on Linux.
    // So Linux is very slow :D
    // So I'm gonna TLE.
    // I hope ccf have good computers.
    struct Node {
        int dis, nd, maxx, s;
        bool operator > (Node y) const {
            return s > y.s;
        }
    };
    bool idk() {
        priority_queue <Node, vector <Node>, greater <Node> > q;
        q.push({0, 1, 0, 0});
        while(!q.empty()) {
            Node x = q.top();
            q.pop();
            a = x.dis % k;
            if(ltt[x.nd][a]) {
                continue;
            }
            ltt[x.nd][a] = x.s;
            if(ltt[n][0]) {
                cout << x.s;
                return true;
            }
            for(int i = hd[x.nd]; i; i = nxt[i]) {
                t = to[i];
                tw = w[i];
                ndis = x.dis + 1;
                if(!ltt[t][ndis % k]) {
                    newt = max(x.maxx, tw - x.dis);
                    news = ceil(newt * 1.0 / k) * k + ndis;
                    q.push({ndis, t, newt, news});
                }
            }
        }
        return false;
    }
    int main() {
        ios :: sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> m >> k;
        for(int i = 1; i <= m; ++i) {
            cin >> x >> y >> z;
            to[i] = y;
            w[i] = z;
            nxt[i] = hd[x];
            hd[x] = i;
        }
        if(!idk()) {
            cout << -1;
        }
        return 0;
    }
  • CSP S2 2023CSP\ S2\ 2023

    • T1T1

    暴力即可,枚举每一种可能情况,暴力判断就行。

    C++
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    int n, a[10][6], dig[6], ans;
    signed main() {
       cin >> n;
       for(int i = 1; i <= n; ++i) {
           for(int j = 1; j <= 5; ++j) {
               cin >> a[i][j];
           }
       }
       for(int i = 0; i <= 99999; ++i) {
           dig[5] = i / 10000;
           dig[4] = i / 1000 % 10;
           dig[3] = i / 100 % 10;
           dig[2] = i / 10 % 10;
           dig[1] = i % 10;
           bool flag = true;
           for(int j = 1; j <= n; ++j) {
               int cnt = 0, x = 0, y = 0;
               for(int k = 1; k <= 5; ++k) {
                   if(dig[k] != a[j][k]) {
                       ++cnt;
                       if(!x) {
                           x = k;
                       }
                       else {
                           y = k;
                       }
                   }
               }
               if(cnt == 0 || cnt > 2) {
                   flag = false;
               }
               if(cnt == 2) {
                   int c = (dig[x] - a[j][x] + 10) % 10;
                   int d = (dig[y] - a[j][y] + 10) % 10;
                   if(c != d || abs(y - x) != 1) {
                       flag = false;
                   }
               }
           }
           if(flag) {
               ++ans;
           }
       }
       cout << ans << endl;
       return 0;
    }
    • T2T2

    还不会,赛时 35 pts35\ pts 区间 dpdp /kk

    • T3T3

    大模拟可爱。

    赛时脑子又飞了挂掉 1515 分。

    代码里的变量名比较魔幻,提前说明一下。

    mpmp 表示变量类型名对应编号。

    mp2mp2 表示一个结构体中的变量名对应的在这个结构体中的编号。

    outeoute 表示创建的变量的名字对应的编号。

    conscons 表示这个结构体包含哪些元素类型。

    stasta 和 finfin 表示这个结构体中的每个元素分别占用的地址的起始和结尾。

    outout 表示创建的变量的类型。

    bsizebsize 表示某个结构体对齐到几字节。

    fullsizefullsize 表示某个结构体总共占用几字节。

    adad 和 adfadf 表示创建的变量的起始字节和结尾字节。

    typetype 和 namename 为第一种操作的输入。

    dvddvd 为第三种操作的输入对应的各个结构体名拆分后的数组。

    retret 为第四种操作的输出数组。

    onamonam 为创建变量的名字。

    otypotyp 为创建变量的类型(和 outout 基本相同)。

    turtur 为一个结构体所包含的元素名字。

    下面开始进行各种操作。

    操作 11

    定义一个结构体类型。

    首先将 mpmp 数组更新。

    然后对于每一个这个结构体所包含的结构体,更新 mp2,cons,bsize,turmp2,cons,bsize,tur。

    然后计算每个元素的 stasta 和 finfin,设当前地址为 nadnad,使用 bsizebsize 进行计算即可。

    最后计算这个结构体的 fullsizefullsize。

    C++
    if(op == 1) {
        string tmp;
        int x;
        cin >> tmp >> x;
        mp[tmp] = i + 4;
        int full = 0;
        for(int j = 1; j <= x; ++j) {
            cin >> type[j] >> name[j];
            mp2[i + 4][name[j]] = j;
            cons[i + 4].push_back(mp[type[j]]);
            bsize[i + 4] = max(bsize[mp[type[j]]], bsize[i + 4]);
            tur[i + 4].push_back(name[j]);
        }
        int nad = 0;
        for(int j = 1; j <= x; ++j) {
            sta[i + 4].push_back(ceil(1.0 * nad / bsize[mp[type[j]]]) * bsize[mp[type[j]]]);
            fin[i + 4].push_back(ceil(1.0 * nad / bsize[mp[type[j]]]) * bsize[mp[type[j]]] + fullsize[mp[type[j]]] - 1);
            nad = ceil(1.0 * nad / bsize[mp[type[j]]]) * bsize[mp[type[j]]] + fullsize[mp[type[j]]];
        }
        fullsize[i + 4] = ceil(1.0 * (fin[i + 4][x - 1] + 1) / bsize[i + 4]) * bsize[i + 4];
        cout << fullsize[i + 4] << " " << bsize[i + 4] << endl;
    }

    操作 22

    创建一个变量。

    类似上一种操作,更新 oute,onam,out,ad,adf,otypoute,onam,out,ad,adf,otyp 即可。

    C++
     if(op == 2) {
         cin >> type[0] >> name[0];
         oute[name[0]] = ++ocnt;
         onam[ocnt] = name[0];
         out.push_back(mp[type[0]]);
         ad[ocnt] = ceil(1.0 * nadd / bsize[mp[type[0]]]) * bsize[mp[type[0]]];
         nadd = ceil(1.0 * nadd / bsize[mp[type[0]]]) * bsize[mp[type[0]]] + fullsize[mp[type[0]]];
         adf[ocnt] = nadd - 1;
         otyp[ocnt] = mp[type[0]];
         cout << ad[oute[name[0]]] << endl;
     }

    操作 33

    查询一个变量地址。

    首先将这么长的字符串拆分成一个个变量名。

    然后循环,每次在一个结构体中,进入一个子结构体,最终结果加上 stasta 值即可。

    C++
    if(op == 3) {
        string tmp;
        cin >> tmp;
        dcnt = 1;
        dvd[1] = "";
        for(int i = 0; i < tmp.size(); ++i) {
            if(tmp[i] != '.') {
                dvd[dcnt] += tmp[i];
            }
            else {
                ++dcnt;
                dvd[dcnt] = "";
            }
        }
        int ret = ad[oute[dvd[1]]];
        int inb = otyp[oute[dvd[1]]];
        for(int i = 2; i <= dcnt; ++i) {
            ret += sta[inb][mp2[inb][dvd[i]] - 1];
            inb = cons[inb][mp2[inb][dvd[i]] - 1];
        }
        cout << ret << endl;
    }

    操作 44

    查询一个地址所对应的变量。

    类似上一个操作,但更复杂一些,因为可能输出 ERRERR。

    首先枚举外层元素,找到这个地址在哪个元素中,如果不存在则 ERRERR。

    然后循环,如果哪次找不到元素则 ERRERR,如果找到元素为基本类型则结束。

    输出时注意最后不要多输出一个点。

    C++
    if(op == 4) {
        int x;
        cin >> x;
        tp4a = 1;
        int c = x;
        int inb = 0;
        bool bigflag = true;
        for(int i = 1; i <= ocnt; ++i) {
            if(ad[i] <= x && adf[i] >= x) {
                ret[tp4a] = onam[i];
                x -= ad[i];
                inb = otyp[i];
                break;
            }
        }
        if(!inb) {
            cout << "ERR" << endl;
            continue;
        }
        while(x >= 0 && bigflag) {
            if(inb <= 4) {
                if(x >= bsize[inb]) {
                    bigflag = false;
                }
                break;
            }
            bool flag = true;
            for(int i = 0; i < sta[inb].size(); ++i) {
                if(sta[inb][i] <= x && fin[inb][i] >= x) {
                    ret[++tp4a] = tur[inb][i];
                    x -= sta[inb][i];
                    inb = cons[inb][i];
                    flag = false;
                    break;
                }
            }
            if(flag) {
                bigflag = false;
                break;
            }
        }
        if(!bigflag) {
            cout << "ERR" << endl;
        }
        else {
            for(int i = 1; i < tp4a; ++i) {
                cout << ret[i] << ".";
            }
            cout << ret[tp4a] << endl;
        }
    }

    Full codeFull\ code

    每个操作上面已经都有了。

    C++
    #include <bits/stdc++.h>
    using namespace std;
    #define int unsigned long long
    // It's very hard.
    // I think I can AC.
    // CCF is great.
    // Well I can't AC.
    // I hope CCF is great.
    map <string, int> mp, mp2[105], oute;
    vector <int> cons[105], sta[105], fin[105], out;
    int nadd, n, op, bsize[105], fullsize[105], dcnt, ocnt, tp4a, ad[105], adf[105];
    string tmp, type[105], name[105], dvd[105], ret[105], onam[105];
    int otyp[105];
    vector <string> tur[105];
    signed main() {
       mp["byte"] = 1;
       mp["short"] = 2;
       mp["int"] = 3;
       mp["long"] = 4;
       fullsize[1] = bsize[1] = 1;
       fullsize[2] = bsize[2] = 2;
       fullsize[3] = bsize[3] = 4;
       fullsize[4] = bsize[4] = 8;
       cin >> n;
       for(int i = 1; i <= n; ++i) {
           cin >> op;
           if(op == 1) {
    
           }
           if(op == 2) {
    
           }
           if(op == 3) {
    
           }
           if(op == 4) {
    
           }
       }
       return 0;
    }
    • T4T4

    不会。

原文