1 条题解
-
0
练30.1 鸡尾酒疗法
解题思路
将第一组的有效数 b0、总数 a0 作为基准。比较两有效率差与 0.05,可交叉相乘为比较 20(ba0-b0a) 与 aa0,避免浮点误差。
正确性说明
按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。
易错点
超过 5% 才是 better/worse,等于 5% 属 same;第一组不输出。
复杂度
时间 O(n),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; long long a0, b0; cin >> a0 >> b0; for (int i = 1; i < n; i++) { long long a, b; cin >> a >> b; long long diff = 20 * (b * a0 - b0 * a), limit = a * a0; if (diff > limit) cout << "better"; else if (diff < -limit) cout << "worse"; else cout << "same"; cout << "\n"; } return 0; }
- 1