Problem E. Balanced Game (The 2016 ACM-ICPC Asia Qingdao Regional Contest, Online)

本文探讨了如何判断一个由N种形状组成的游戏是否为平衡游戏。基于经典游戏'石头-剪刀-布'及其扩展版,文章分析了游戏平衡性的条件,并提供了一个简单的算法实现。

Problem E. Balanced Game
Time limit: 1s
Color of balloons: 32768K
Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each player simultane-
ously forms one of three shapes with an outstretched hand. These shapes are “rock”, “paper”, and “scissors”. The
game has only three possible outcomes other than a tie: a player who decides to play rock will beat another player
who has chosen scissors (“rock crushes scissors”) but will lose to one who has played paper (“paper covers rock”);
a play of paper will lose to a play of scissors (“scissors cut paper”). If both players choose the same shape, the
game is tied and is usually immediately replayed to break the tie.
Recently, there is a upgraded edition of this game: rock-paper-scissors-Spock-lizard, in which there are totally five
shapes. The rule is simple: scissors cuts paper; paper covers rock; rock crushes lizard; lizard poisons Spock; Spock
smashes scissors; scissors decapitates lizard; lizard eats paper; paper disproves Spock; Spock vaporizes rock; and
as it always has, rock crushes scissors.
Both rock-paper-scissors and rock-paper-scissors-Spock-lizard are balanced games. Because there does not exist a
strategy which is better than another. In other words, if one chooses shapes randomly, the possibility he or she
wins is exactly 50% no matter how the other one plays (if there is a tie, repeat this game until someone wins).
Given an integer N, representing the count of shapes in a game. You need to find out if there exist a rule to make
this game balanced.
Input
The first line of input contains an integer t, the number of test cases. t test cases follow. For each test case, there
is only one line with an integer N (2 N 1000), as described above.
Here is the sample explanation.
In the first case, donate two shapes as A and B. There are only two kind of rules: A defeats B, or B defeats A.
Obviously, in both situation, one shapes is better than another. Consequently, this game is not balanced.
In the second case, donate two shapes as A, B and C. If A defeats B, B defeats C, and C defeats A, this game is
balanced. This is also the same as rock-paper-scissors.
In the third case, it is easy to set a rule according to that of rock-paper-scissors-Spock-lizard.
Output
For each test cases, output “Balanced” if there exist a rule to make the game balanced, otherwise output “Bad”.
Sample
standard input standard output
3
2
3
5


Bad
Balanced
Balanced
 

1分析:

     n个状态的游戏,问是否为平衡游戏。题目中定义Balanced Game中每种状态胜率都是50% (ock-paper-scissors-Spock-lizard are balanced games. Because there does not exist a strategy which is better than another. In other words, if one chooses shapes randomly, the possibility he or she wins is exactly 50% ),这是非常关键的一个信息。要想达到平衡状态,对于每一个玩家来说,他胜负的概率都为1/2。

   以n个人玩游戏为例:除去玩家自己,对于剩下的(n-1)人,玩家必须打败(n-1)/2,且又被(n-1)/2人打败,此时才将达到题目所说平衡状态。也就是(n-1)为偶数(n为奇数)的时候,游戏将处于平衡状态。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
int main()
{
	cin>>n;
	int m;
	while(n--)
	{
		cin>>m;
		if(m%2==0)
		  cout<<"Bad"<<endl;
		else
		  cout<<"Balanced"<<endl;
	}
	return 0;
}

 

<think>首先,用户的问题是关于pm.dexopt相关参数的含义、作用及配置信息。用户提到了具体的参数,如pm.dexopt.first-boot=verify和pm.dexopt.boot-after-ota=verify等。 根据系统级指令,我需要: - 所有行内数学表达式使用$...$格式。 - 独立公式使用$$...$$格式并单独成段。 - 使用中文回答。 - 生成相关问题。 - 在回答中引用的段落末尾自然地添加引用标识,如[^1]。 - 参考用户上一次的问题和我的回答,但在这个对话中,这是第一个用户消息,所以没有历史。用户提到了一些引用,但这些引用是参考站内引用,我应该参考它们。 用户提供了引用: - [^1]: LOCAL_DEX_PREOPT 参数。 - [^2]: Android 6.0 的开机 dexopt。 - [^3]: dex2oat 编译参数。 - [^4]: dexopt 服务的调度。 - [^5]: JIT 和 AOT 编译。 用户的问题是针对pm.dexopt参数,所以我需要解释这些参数的含义和作用。 回想一下pm.dexopt参数: - pm.dexopt 是 Android 系统中的属性,用于控制 dex 文件的优化行为。 - 这些属性通常在设备启动时设置,影响如何优化应用。 具体参数: - pm.dexopt.first-boot: 控制第一次启动时的优化行为。 - pm.dexopt.boot-after-ota: 控制 OTA 更新的优化行为。 - 其他可能包括 pm.dexopt.shared, pm.dexopt.install 等。 含义: - pm.dexopt.first-boot=verify: 在第一次启动时,对 dex 文件进行验证而不是完全优化。 - 类似地,boot-after-ota=verify 表示在 OTA 更新后启动时验证。 作用: - verify 选项意味着 dex 文件只被验证,而不是编译成机器码。这可以减少启动时间,但可能影响运行时性能。 - 其他值包括 speed, interpret-only, etc. 配置信息: - 这些属性可以通过 adb shell setprop 命令设置,或者在构建时在 build.prop 中定义。 - 它们影响系统服务如 PackageManagerService 的行为。 - 在引用中,[^2]提到了 performBootDexOpt,这与启动时的优化相关。[^3]提到了 dex2oat 编译参数,这可能与优化级别相关。[^4]提到了调度优化作业。 - [^5]讨论了 JIT 和 AOT,AOT 是 dexopt 的一部分。 结构化我的回答: 1. 介绍 pm.dexopt 参数。 2. 解释每个参数的含义和作用。 3. 描述相关配置信息。 4. 自然地引用提供的参考资料。 5. 最后生成相关问题。 使用中文回答: - 确保整个回答用中文。 生成相关问题: - 在回答结束时添加相关问题部分。 引用: - 在回答中引用段落时添加 [^n]。 - 例如,当提到第一个启动优化时,引用 [^2]。 - 提到编译参数时引用 [^3]。 参数列表: - 用户指定了 pm.dexopt.first-boot=verify, pm.dexopt.boot-after-ota=verify,但可能还有其他相关参数。 - 常见参数: - pm.dexopt.first-boot: 第一次启动时的优化。 - pm.dexopt.boot-after-ota: OTA 更新后启动时的优化。 - pm.dexopt.shared: 共享库的优化。 - pm.dexopt.install: 安装时的优化。 - pm.dexopt.bg-dexopt: 后台优化的模式。 值: - verify: 只验证 dex 文件。 - interpret-only: 只解释执行。 - speed: 优化为速度优先。 - balanced: 平衡优化。 - speed-profile: 基于性能数据分析优化(从引用[^3])。 在 Android 中,这些属性设置 dex2oat 的编译过滤器(compiler-filter)。 从引用: - [^1]: LOCAL_DEX_PREOPT,但这是构建时的,不是运行时的 pm.dexopt。 - [^2]: performBootDexOpt(),用于启动时优化。 - [^3]: dex2oat 参数 --compiler-filter。 - [^4]: 调度后台优化作业。 - [^5]: JIT vs AOT。 所以,我可以联系这些来解释。 回答大纲: 1. 介绍 pm.dexopt 参数:它们是 Android 系统属性,用于控制 dex 文件的优化行为。 2. 解释具体参数: - pm.dexopt.first-boot: 设置第一次设备启动时的优化模式。例如,verify 表示只验证 dex 文件而不编译。 - pm.dexopt.boot-after-ota: 设置 OTA 更新后启动时的优化模式。 - 其他类似参数:pm.dexopt.install, pm.dexopt.shared, pm.dexopt.bg-dexopt。 3. 作用:这些参数决定了优化级别,影响启动时间、应用性能和存储空间。 4. 配置信息:可以通过 adb shell setprop 命令设置,或在系统构建时定义。 5. 相关引用:例如,启动时优化引用了[^2]。 生成相关问题: - 例如: 1. 如何设置 pm.dexopt 参数? 2. verify 模式与其他模式的区别? 3. dexopt 优化如何影响应用性能? 确保行内数学:如果有变量等,使用 $...$,但这里可能没有太多数学表达式。参数名如 pm.dexopt.first-boot 不需要数学格式,除非是数学变量。 独立公式:如果有方程,用 $$...$$,但这里可能不需要。 最终回答结构: - 开头介绍。 - 分解参数。 - 配置信息。 - 结束。 - 相关问题。 </think>### pm.dexopt 参数详解 在 Android 系统中,`pm.dexopt` 是一组系统属性,用于控制 dex 文件的优化行为。dex 文件是 Dalvik/ART 虚拟机执行的应用字节码文件,优化过程(如 dexopt)旨在提升运行时性能。这些参数通常在设备启动或更新时生效,通过设置不同的优化模式(如 `verify`、`speed` 等)来平衡启动时间、内存占用和应用性能。下面我将基于您提供的引用信息,详细解释主要参数的含义、作用及配置方式。 #### 主要参数含义与作用 以下是常见的 `pm.dexopt` 参数及其作用: 1. **pm.dexopt.first-boot=verify** - **含义**:指定设备第一次启动时对 dex 文件的优化模式。`verify` 模式表示只验证 dex 文件的完整性而不进行编译或优化。 - **作用**:这减少了首次启动时的优化开销,加速启动过程。验证过程检查 dex 文件是否有效,但不生成优化的机器码(.odex 或 .art 文件)。引用 [^2] 中提到,Android 6.0 的开机优化调用 `performBootDexOpt()` 方法,这通常在首次启动时触发;`verify` 模式可以避免不必要的编译,提升启动速度。 - **典型值**:除了 `verify`,其他值如 `interpret-only`(仅解释执行)、`speed`(速度优先优化)等也可用。默认值通常是 `verify` 或 `speed`,取决于设备配置。 2. **pm.dexopt.boot-after-ota=verify** - **含义**:设置 OTA(空中下载)更新后启动时的优化模式。`verify` 选项同样只执行验证,不进行深度优化。 - **作用**:OTA 更新通常会改变系统文件,此参数确保更新后快速启动,避免因重优化所有应用导致的延迟。它类似于首次启动优化,但针对更新场景。引用 [^2] 中的 `performBootDexOpt()` 在 OTA 后也可能被调用,`verify` 模式减少了编译时间。 3. **pm.dexopt.install=[mode]** - **含义**:控制应用安装时的优化模式(例如 `verify`、`speed-profile` 等)。引用 [^3] 提到 `compiler-filter` 参数(如 `speed-profile`),这直接影响优化级别。 - **作用**:安装时优化能提升首次启动性能,但会增加安装时间。例如,`speed-profile` 模式基于性能分析数据优化,平衡速度和资源消耗;`verify` 模式则仅验证文件,适合存储受限场景。 4. **pm.dexopt.bg-dexopt=[mode]** - **含义**:设置后台优化任务的模式(如 `speed-profile` 或 `balanced`)。引用 [^4] 讨论了后台优化作业(如 `JobScheduler`),它定期运行以优化热应用。 - **作用**:在设备空闲时优化 dex 文件,提升运行时性能而不影响用户体验。`speed-profile` 模式(引用 [^3])利用性能数据,优化频繁执行的代码。 5. **pm.dexopt.shared=[mode]** - **含义**:针对共享库的优化模式(例如 `speed` 或 `verify`)。 - **作用**:共享库(如系统框架)被多个应用使用,优化后能提高整体性能。`verify` 模式减少优化时间,但可能牺牲长期运行效率。 这些参数的核心作用是控制 dex2oat 编译器的行为(引用 [^3]),影响编译过滤器的设置: - `verify`:仅验证 dex 文件,不编译,启动快但运行时性能较低。 - `speed`:完全编译为机器码,运行时快但编译耗时。 - `speed-profile`:基于性能数据进行优化(需要 .prof 文件),在性能和开销间平衡(引用 [^3])。 - 其他值如 `interpret-only` 仅依赖解释器执行,`balanced` 介于速度和资源消耗之间。 引用 [^5] 中提到,ART 使用 AOT(预先优化)替代 Dalvik 的 JIT,而 `pm.dexopt` 参数直接控制 AOT 的优化策略。例如,`verify` 模式可能延迟优化到后台任务(引用 [^4]),提升系统响应性。 #### 配置信息 - **配置方式**: - **运行时设置**:通过 adb 命令动态修改,例如: ```bash adb shell setprop pm.dexopt.first-boot verify # 设置首次启动模式 adb shell setprop pm.dexopt.boot-after-ota speed-profile # OTA 后使用性能分析优化 ``` 这些属性在设备重启后生效。 - **构建时设置**:在系统构建文件(如 `build.prop` 或 makefiles)中定义。例如,引用 [^1] 中的 `LOCAL_DEX_PREOPT` 构建参数可以启用或停用预优化;类似地,`PRODUCT_DEX_PREOPT_BOOT_FLAGS` 可以覆盖默认值。 - 示例构建配置: ```makefile PRODUCT_SYSTEM_PROPERTIES += pm.dexopt.bg-dexopt=speed-profile ``` - **默认值**:这些参数通常在设备出厂时由厂商预设。默认值取决于设备性能;高性能设备可能默认 `speed`,而低端设备可能用 `verify` 以减少启动时间。 - **影响范围**:优化模式影响存储空间和性能。例如,`verify` 模式不生成 .odex 文件,节省空间(引用 [^1] 提到文件剥离),但运行时依赖解释器或 JIT(引用 [^5])。`verify` 模式在签名验证场景下有用,避免文件修改(引用 [^1])。 - **最佳实践**:在快速启动场景(如首次启动或 OTA)使用 `verify` 模式;在性能敏感应用中使用 `speed-profile`。后台优化(引用 [^4])可以自动处理长期优化。 #### 总结 `pm.dexopt` 参数是 Android 优化系统的关键部分,通过调整编译过滤器来控制 dex 文件处理。理解这些参数能帮助开发者优化启动时间和运行时性能。如果您有具体设备日志或场景,我可以提供进一步的诊断建议。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值