C. Bipartize
枚举每个点的颜色,然后统计有多少条边的端点颜色相同,这就是要删除的点,取最小值即可
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)using namespace std;
using P = pair<int, int>;int main() {int n, m;cin >> n >> m;vector<P> es;rep(i, m) {int u, v;cin >> u >> v;--u; --v;es.emplace_back(u, v);}int ans = m;rep(s, 1<<n) {vector<int> col(n);rep(i, n) col[i] = s>>i&1;int now = 0;for (auto [u, v] : es) {if (col[u] == col[v]) now++;}ans = min(ans, now);}cout << ans << '\n';return 0;
}
D. The Simple Game
博弈型dp
记 dp[i][v]
表示从第 \(i\) 轮时棋子位于点 \(v\) 这个状态开始时的赢家是否是 \(\mathrm{Alice}\)
转移顺序:从后往前
转移需要用到以下性质:
一个状态是必胜状态,当且仅当它至少有一个后继是必败状态
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)using namespace std;void solve() {int n, m, k;string s;cin >> n >> m >> k >> s;vector<vector<int>> to(n);rep(i, m) {int u, v;cin >> u >> v;--u; --v;to[u].push_back(v);}vector dp(k*2+1, vector<int>(n));rep(v, n) {dp[k*2][v] = s[v] == 'A';}for (int i = k*2-1; i >= 0; --i) {rep(v, n) {dp[i][v] = 0;for (int u : to[v]) {if (!dp[i+1][u]) dp[i][v] = 1;}}}if (dp[0][0]) puts("Alice");else puts("Bob");
}int main() {int t;cin >> t;while (t--) solve();return 0;
}
E. Wind Cleaning
把“同时把所有垃圾平移一格”的动态,用一个 \(\mathrm{bfs}\) 在状态空间上跑最短路。每个状态由一个矩形区域(表示“仍然可能留在棋盘上的初始格子下标范围”)和当前 \(T\) 的位置共同描述。\(\mathrm{bfs}\) 在这些状态中按步数扩展(每一步相当于把所有垃圾朝某个方向移动一次,等价于改变 T
的相对偏移),一旦“矩形中没有任何 #
原始格”就说明所有垃圾都已经消失,此时的步数就是最终答案。
简单来说就是不断裁剪包含所有垃圾的最小矩形框,然后利用运动的是相互,保持垃圾不动,移动高桥的位置
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)using namespace std;int main() {int h, w;cin >> h >> w;vector<string> s(h);rep(i, h) cin >> s[i];int si = 0, sj = 0;rep(i, h)rep(j, w) if (s[i][j] == 'T') si = i, sj = j;using S = tuple<int, int, int, int, int, int>;map<S, int> dist;queue<S> q;auto push = [&](int li, int ri, int lj, int rj, int ti, int tj, int d) {li = max(li, ti-si); ri = min(ri, h+(ti-si));lj = max(lj, tj-sj); rj = min(rj, w+(tj-sj));if (li <= ti and ti < ri and lj <= tj and tj < rj) {if (s[ti][tj] == '#') return;}S st(li, ri, lj, rj, ti, tj);if (dist.count(st)) return;dist[st] = d;q.emplace(st);};push(0, h, 0, w, si, sj, 0);while (q.size()) {int d = dist[q.front()];auto [li, ri, lj, rj, ti, tj] = q.front(); q.pop();{int cnt = 0;for (int i = li; i < ri; ++i) {for (int j = lj; j < rj; ++j) {if (s[i][j] == '#') cnt++;}}if (cnt == 0) {cout << d << '\n';return 0;}}push(li, ri, lj, rj, ti-1, tj, d+1);push(li, ri, lj, rj, ti+1, tj, d+1);push(li, ri, lj, rj, ti, tj-1, d+1);push(li, ri, lj, rj, ti, tj+1, d+1);}puts("-1");return 0;
}
F. Not Adjacent
折半搜索
发现两边还是有 \(30\) 个数,但注意到每边最多取到 \(15\) 个数,所以直接搜就行
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)using namespace std;
using ll = long long;int main() {int n, m;cin >> n >> m;vector<int> a(n);rep(i, n) cin >> a[i];auto enumerate = [&](int l, int r) {vector<int> d0 = {0}, d1 = {0};for (int i = l; i < r; ++i) {vector<int> d2 = d1;for (int x : d0) d2.push_back((x+a[i])%m);swap(d0, d1);swap(d1, d2);}return d1;};ll ans = 0;int c = n/2;rep(ci, 2) {auto dl = enumerate(0, c-ci);auto dr = enumerate(c+1+ci, n);sort(dr.begin(), dr.end());for (int x : dl) {if (ci) x += a[c], x %= m;int y = (m-x)%m;int l = lower_bound(dr.begin(), dr.end(), y) - dr.begin();int r = upper_bound(dr.begin(), dr.end(), y) - dr.begin();ans += r-l;}}cout << ans << '\n';return 0;
}
另外,不相邻子序列的数量其实是斐波那契数列(虽然用了这个也没有变的很快)
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)using namespace std;
using ll = long long;int main() {int n, m;cin >> n >> m;vector<int> a(n);rep(i, n) cin >> a[i];vector<int> fib(n+1, 1);for (int i = 2; i <= n; ++i) fib[i] = fib[i-1]+fib[i-2];auto enumerate = [&](int l, int r) -> vector<int> {if (l >= r) return {0};int w = r-l;vector<int> res(fib[w+1]);for (int s = 1, fi = 1; s < fib[w+1]; ++s) {if (fib[fi+1] <= s) fi++;res[s] = (a[r-fi]+res[s-fib[fi]])%m;}return res;};ll ans = 0;int c = n/2;rep(ci, 2) {auto dl = enumerate(0, c-ci);auto dr = enumerate(c+1+ci, n);sort(dr.begin(), dr.end());for (int x : dl) {if (ci) x += a[c], x %= m;int y = (m-x)%m;int l = lower_bound(dr.begin(), dr.end(), y) - dr.begin();int r = upper_bound(dr.begin(), dr.end(), y) - dr.begin();ans += r-l;}}cout << ans << '\n';return 0;
}
G. Takahashi's Expectation 2
二进制分组+块间合并
直接看StarSilk的题解
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/935440.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!相关文章
从 EFI 到 GPT:深入理解现代计算机启动与磁盘分区技能
从 EFI 到 GPT:深入理解现代计算机启动与磁盘分区技能pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&q…
2025年扁钢厂家最新权威推荐榜:强度与精度双重保障的工业脊
2025年扁钢厂家最新权威推荐榜:强度与精度双重保障的工业脊梁在制造业转型升级的关键时期,扁钢作为工业领域的"骨架材料",其质量直接影响着装备制造、建筑工程、轨道交通等行业的健康发展。随着新材料技术…
GJB 438C学习
系列标准宣贯︱GJB 438C中规定软件文档的用途与编写时机长风破浪会有时,直挂云帆济沧海! 可通过下方链接找到博主 https://www.cnblogs.com/judes/p/10875138.html
typora markdown
markdown学习
标题:
+空格+标题名字
二级标题
+名称
以此类推每级加一个#
字体
hello,world! 左右 **
hello,world! *
hello,workd! ***
hello,workd! …
2025防爆数粒机厂家权威推荐榜:高效精准与安全防爆口碑之选
2025防爆数粒机厂家权威推荐榜:高效精准与安全防爆口碑之选在制药、化工、食品等工业领域,颗粒物料的精准计数与分装是生产流程中的关键环节。防爆数粒机作为特种设备,不仅需要满足高效精准的计数要求,更要具备严格…
想在浏览器里跑 AI?TensorFlow.js 硬件帮助完全指南
想在浏览器里跑 AI?TensorFlow.js 硬件帮助完全指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&quo…
Linux系统设置与理解主机名(hostname)的重要性
在Linux系统中,主机名(hostname)是区分网络中一台主机或者是服务设备的一个唯一标识。操作系统在网络通讯时会使用到主机名,它允许用户和程序方便地识别和交互。除了作为识别符,主机名在系统安全性、维护管理方面也…
CSharp: image (JPG,PNG,etc) covert webp using Aspose.Imaging
protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){// 激活Aspose//ActiveAspose(); ///无效HookManager.ShowHookDetails(true);HookManager.StartHook();// The path to the documents direct…
2025耐磨轮胎厂家最新权威推荐榜:超强抓地力与持久耐用深度
2025耐磨轮胎厂家最新权威推荐榜:超强抓地力与持久耐用深度在商用车运输领域,轮胎作为车辆与路面唯一接触的部件,其耐磨性能直接关系到运营成本与行车安全。据统计,轮胎磨损占运输企业运营成本的比重不容忽视,优质…
在Vue 3项目中集成Element Plus组件库的步骤
安装Element Plus
首先,确保你的开发环境已经安装了Node.js和npm(或yarn)。然后,在你的Vue 3项目根目录下打开终端,并执行以下命令来安装Element Plus:
npm install element-plus --save
# 或者使用yarn
yarn ad…
安装pytorch(win 11 +3060 laptop)
移除不兼容的pytorch版本运行代码时报错这个错误表明正在使用的 PyTorch 版本过旧,它不支持 torch.utils.data.DataLoader 的 persistent_workers 参数或属性。
卸载旧版
pip uninstall torch torchvision torchaudio…
MySQL数据库连接过多(Too many connections)错误处理策略
MySQL数据库遭遇“Too many connections”错误时,意味着当前所有可用的连接都已被使用,新的客户端连接无法建立。处理这一问题需要综合考虑配置调整、资源优化和代码改进等多方面因素。
首先,检查 max_connections设…
Real English Questions and Answers Practice
Real English Questions and Answers PracticeHello my friends and welcome back to Bookish English 2, the place where you live English, not just study it. Todays lesson is special. You will not read gramm…
[Python] Python配置uv环境
[Python] Python配置uv环境$(".postTitle2").removeClass("postTitle2").addClass("singleposttitle");目录01 安装uv02 创建项目03 uv安装python包3.1 uv add3.2 uv pip install(兼容…
SQL删除操作性能分析:移除300万条记录所需时间估算
数据库服务器硬件CPU: 删除操作会消耗CPU资源。如果处理器速度较慢或者负载较高,这将直接影响到删除操作的速度。
内存: 足够快速且充足量内存可以确保数据库缓冲区高效运行。
磁盘I/O: 删除大量数据会产生大量磁盘I/…
Spring Boot 基础教程 - 指南
Spring Boot 基础教程 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco"…
Linux系统监控报告CPU软锁定问题(soft lockup)诊断方法
CPU软锁定问题,即软件锁定(soft lockup),是指CPU在一段时间内被一个内核进程占用而无法处理其他任务,通常是由于某个进程或驱动程序在内核空间执行了过长时间的循环或者死锁。这种情况下,系统可能无响应或响应非…