/* * > CPP Code Snippet < * > Powered by Microsoft Visual Studio Code < * * @Author FrankWKD (wangkedi01) * @Date 2025-07-15 * @Network "https://oj.czos.cn/contest/problem?id=25573&pid=1" * @License GNU General Public License 2.0 * @Platform [Frank]iMac Ubuntu Pro 24.04 LTS * @FileName T2.cpp * @FilePath /media/frank/FrankW/_default/_Mine!/Working/code-spaces/czos/CSP-S_Practise/Lesson2/T2.cpp * @Solution -- */
// #pragma GCC optimize(3) #include<bits/stdc++.h> usingnamespace std; int xi, yi, ans, n, m; int f[1010100]; intfind(int x){ return f[x] == x ? x : f[x] = find(f[x]); } voidmerge(int x, int y){ f[find(x)] = f[find(y)]; } intmain(){ // freopen("sample.in","r",stdin); // freopen("sample.out","w",stdout); // ios::sync_with_stdio(false); // cin.tie(0); cout.tie(0);
cin >> n >> m; for (int i = 1; i <= n; i++) { f[i] = i; } for (int i = 1; i <= m; i++) { int mi; cin >> xi >> yi >> mi; merge(xi, yi); } for (int i = 1; i <= n; i++) { if (find(f[i]) == i) ans++; } cout << ans << endl;
/* * > CPP Code Snippet < * > Powered by Microsoft Visual Studio Code < * * @Author FrankWKD (wangkedi01) * @Date 2025-07-18 * @Network "https://oj.czos.cn/contest/problem?id=25573&pid=3&_pjax=%23p0" * @License GNU General Public License 2.0 * @Platform [Frank]iMac Ubuntu Pro 24.04 LTS * @FileName T4[100pts].cpp * @FilePath /media/frank/FrankW/_default/_Mine!/Working/code-spaces/czos/CSP-S_Practise/Lesson2/T4[100pts].cpp * @Solution -- */
// #pragma GCC optimize(3) #include<bits/stdc++.h> usingnamespace std; constint N = 5e5 + 10; int n, m, q, w[N], x, y, t; longlong din[N], t_din[N], tot, ans; mt19937 rnd(time(0)); intmain(){ cin >> n >> m; for (int i = 1; i <= n; i++) ans += (w[i] = rnd()); for (int i = 1; i <= m; i++) { cin >> x >> y; din[y] += w[x],t_din[y] = din[y],tot += w[x]; } cin >> q; for (int i = 1; i <= q; i++) { cin >> t >> x; if (t == 1) { cin >> y; din[y] -= w[x],tot -= w[x]; } elseif (t == 2) tot -= din[x],din[x] = 0; elseif (t == 3) { cin >> y; din[y] += w[x],tot += w[x]; } elseif (t == 4) tot += t_din[x] - din[x],din[x] = t_din[x]; if (tot == ans) cout << "YES\n"; else cout << "NO\n"; } return0; }
T5 P2668 [NOIP 2015 提高组] 斗地主
题目背景
NOIP2015 Day1T3
题目描述
牛牛最近迷上了一种叫斗地主的扑克游戏。斗地主是一种使用黑桃、红心、梅花、方片的 A 到 K 加上大小王的共 54 张牌来进行的扑克牌游戏。在斗地主中,牌的大小关系根据牌的数码表示如下:3<4<5<6<7<8<9<10<J<Q<K<A<2<小王<大王,而花色并不对牌的大小产生影响。每一局游戏中,一副手牌由 n 张牌组成。游戏者每次可以根据规定的牌型进行出牌,首先打光自己的手牌一方取得游戏的胜利。
/* * > CPP Code Snippet < * > Powered by Microsoft Visual Studio Code < * * @Author FrankWKD (wangkedi01) * @Date 2025-07-18 * @Network "https://oj.czos.cn/contest/problem?id=25573&pid=4" * @License GNU General Public License 2.0 * @Platform [Frank]iMac Ubuntu Pro 24.04 LTS * @FileName T5.cpp * @FilePath /media/frank/FrankW/_default/_Mine!/Working/code-spaces/czos/CSP-S_Practise/Lesson2/T5.cpp * @Solution -- */
// #pragma GCC optimize(3) #include<bits/stdc++.h> usingnamespace std; int T, n; int ans, s[22], c[5]; intnum(){ int tot = 0; memset(c, 0, sizeof c); int t = (s[0] == 2); // 判断是否有双王 for (int i = 0; i <= 14; i++) { c[s[i]]++; // 有 4 张牌的种类数,有 1 张牌的种类数,有 2 张牌的种类数。.. } while (c[4] > 0and c[2] - t >= 2) { c[4]--; c[2] -= 2; tot++; } while (c[4] > 0and c[1] >= 2) { c[4]--; c[1] -= 2; tot++; } while (c[4] > 0and c[2] > 0) { c[4]--; c[2] -= 1; tot++; } while (c[3] > 0and c[2] - t > 0) { c[3]--; c[2] -= 1; tot++; } while (c[3] > 0and c[1] > 0) { c[3]--; c[1] -= 1; tot++; } return tot + c[1] + c[2] + c[3] + c[4]; } voiddfs(int x){ // x: 出顺子的数量 if (x >= ans) return; // 最优性剪枝 ans = min(ans, x + num()); // 更新答案 for (int i = 3; i > 0; i--) { for (int j = 3; j <= 14; j++) { int p = j; while (s[p] >= i and p <= 14) { if ((i == 3and p - j + 1 >= 2) or (i == 2and p - j + 1 >= 3) or (i == 1and p - j + 1 >= 5)) { for (int k = j; k <= p; k++) s[k] -= i; dfs(x + 1); // 搜索~ for (int k = j; k <= p; k++) s[k] += i; } p++; // warning! } } } }
intmain(){ // freopen("sample.in","r",stdin); // freopen("sample.out","w",stdout); // ios::sync_with_stdio(false); // cin.tie(0); cout.tie(0); cin >> T >> n; while (T--) { ans = n; memset(s, 0, sizeof s); for (int i = 1; i <= n; i++) { int a, b; cin >> a >> b; if (a == 1) a = 14; s[a]++; } dfs(0); cout << ans << endl; } // fclose(stdin); // fclose(stdout); return0; }
T6 P5665 [CSP-S2019] 划分
题目描述
2048 年,第三十届 CSP 认证的考场上,作为选手的小明打开了第一题。这个题的样例有 n 组数据,数据从 1∼n 编号,i 号数据的规模为 ai。
小明对该题设计出了一个暴力程序,对于一组规模为 u 的数据,该程序的运行时间为 u2。然而这个程序运行完一组规模为 u 的数据之后,它将在任何一组规模小于u 的数据上运行错误。样例中的 ai 不一定递增,但小明又想在不修改程序的情况下正确运行样例,于是小明决定使用一种非常原始的解决方案:将所有数据划分成若干个数据段,段内数据编号连续,接着将同一段内的数据合并成新数据,其规模等于段内原数据的规模之和,小明将让新数据的规模能够递增。
/* * > CPP Code Snippet < * > Powered by Microsoft Visual Studio Code < * * @Author FrankWKD * @Date 2025-08-06 * @Network "https://oj.czos.cn/contest/problem?id=25573&pid=5" * @License GNU General Public License 2.0 * @Platform [Frank]iMac Ubuntu Pro 24.04 LTS * @Name T6.cpp * @Path /media/frank/FrankW1/_default/Mine/Working/code-spaces/OI/OJ/czos/CSP-S_Practise/Lesson2/T6.cpp * @Sol -- */
// #pragma GCC optimize(3) #define _for(cc, ba, ab) for (register int cc = (ba); cc <= (ab); cc++) #define for_(cc, ba, ab) for (register int cc = (ba); cc >= (ab); cc--) #include<bits/stdc++.h> usingnamespace std; constint N = 4e7 + 10; constint M = 1e5 + 10; constint mod = (1 << 30); #define i2 __int128 // MLE Warning int n, type, x, y, z, m, a[N], b[N], p[M], l[M], r[M]; int pre[N];
longlong sum[N];
int h, t, q[N]; longlongget(longlong x){ return2 * sum[x] - sum[pre[x]]; } voidprlonglong(i2 ans){ string r = ""; while (ans) { r = to_string((longlong)(ans % 10)) + r; ans /= 10; } cout << r; } signedmain(){ // freopen("sample.in","r",stdin); // freopen("sample.out","w",stdout); // ios::sync_with_stdio(false); // cin.tie(0); cout.tie(0); cin >> n >> type; if (type == 1) { cin >> x >> y >> z >> b[1] >> b[2] >> m; _for(i, 1, m) cin >> p[i] >> l[i] >> r[i]; _for(i, 3, n) b[i] = ((longlong)b[i - 1] * x + (longlong)b[i - 2] * y + z) % mod; _for(i, 1, m) _for(j, p[i - 1] + 1, p[i]) a[j] = ((longlong)b[j] % (r[i] - l[i] + 1)) + l[i], sum[j] = sum[j - 1] + a[j]; } else _for(i, 1, n) cin >> a[i], sum[i] = sum[i - 1] + a[i]; h = 0, t = 0; _for(i, 1, n) { while (h <= t and sum[i] >= get(q[h])) h++; h--; pre[i] = q[h]; while (h <= t andget(i) <= get(q[t])) t--; q[++t] = i; } i2 ans = 0; for (longlong i = n; i; i = pre[i]) { i2 s = sum[i] - sum[pre[i]]; ans += s * s; } prlonglong(ans); return0; }