"Parity算法详解:利用sum数组判断奇偶性,通过并查集解决矛盾问题"
需积分: 20 128 浏览量
更新于2024-01-13
收藏 812KB PPT 举报
The Parity(ceoi99) algorithm, devised by Xiao Tian, involves establishing an array called sum, where sum[i] represents whether the sum from 1 to i is odd (true) or even (false), with sum[0] initialized as false. This allows any given question (a,b) to be expressed using the expression sum[b] xor sum[a-1].
Initially, the values of sum[1..n] are unknown and assumed to be false, meaning that any sum[a] and sum[b] are independent. For each question (a,b,c), we can determine the relationship between sum[b] and sum[a-1] by using the equation sum[b] xor sum[a-1]=c. This step can be performed using a disjoint-set data structure. If sum[a-1] and sum[b] do not belong to the same set for a given question, they are merged into the same set. If sum[a-1] xor sum[b] does not equal c, a contradiction is detected, and the total sentence count is output before exiting.
For a consistent sum array without contradictions, each set is divided into two parts, with one part designated as true and the other as false. This allows for the determination of the sum array. Using the equation sum[i] xor sum[i-1], we can calculate the value of the ith digit. Since there are no questions asked between different sets, this sequence of numbers represents a valid solution, proving the correctness of the algorithm. It is often used in various algorithms.
2022-07-15 上传
2021-05-27 上传
2021-05-30 上传
2021-09-29 上传
2021-05-15 上传
2021-01-27 上传
我的小可乐
- 粉丝: 26
- 资源: 2万+
最新资源
- 高清艺术文字图标资源,PNG和ICO格式免费下载
- mui框架HTML5应用界面组件使用示例教程
- Vue.js开发利器:chrome-vue-devtools插件解析
- 掌握ElectronBrowserJS:打造跨平台电子应用
- 前端导师教程:构建与部署社交证明页面
- Java多线程与线程安全在断点续传中的实现
- 免Root一键卸载安卓预装应用教程
- 易语言实现高级表格滚动条完美控制技巧
- 超声波测距尺的源码实现
- 数据可视化与交互:构建易用的数据界面
- 实现Discourse外聘回复自动标记的简易插件
- 链表的头插法与尾插法实现及长度计算
- Playwright与Typescript及Mocha集成:自动化UI测试实践指南
- 128x128像素线性工具图标下载集合
- 易语言安装包程序增强版:智能导入与重复库过滤
- 利用AJAX与Spotify API在Google地图中探索世界音乐排行榜