省流:这题反作弊对 smart-luogu 用户还是太逆天了。
ok 我们回归正题。
题目分析
在这个问题中,我们需要模拟一场战斗过程并判断使用道具后的胜负情况。具体来说:
- 初始时,玩家和敌人的血量均为 。
- 有 n 次攻击,第 次攻击用整数 表示:
- 若 :玩家攻击敌人,敌人血量减少 。
- 若 :敌人攻击玩家,玩家血量减少 。
- 战斗在一方血量 时停止,不可能出现双方同时血量 的情况。
- 使用道具后,玩家每次攻击会额外造成 点伤害。
- 需要判断 个不同 值下的战斗结果(胜利 / 失败 / 平局)。
以上就是题目大意。
核心分析
战斗的关键是确定哪一方会先达到血量 的状态。我们需要分别计算:
- 敌人将玩家血量打到 所需的攻击次数(记为 )。
- 玩家将敌人血量打到 所需的攻击次数(记为 )。
通过比较这两个值即可判断胜负:
- 若 :玩家先击败敌人,输出 “Yes”。
- 若 :敌人先击败玩家,输出 “No”。
- 若 :双方都存活到最后,输出 “Tie”。
预处理优化
为高效计算上述两个值,我们可以前缀和预处理两类累计信息:
- 玩家攻击的累计信息(基础伤害总和与攻击次数)。
- 敌人攻击的累计信息(伤害总和与攻击次数)。
- 使用结构体数组存储这些信息,便于后续查询时快速计算。
二分查找应用
对于每个查询,我们需要找到恰好能使血量 的攻击次数。这可以通过二分查找高效实现:
- 对于玩家攻击:总伤害 = 基础伤害总和 + 攻击次数 × 额外伤害 。
- 对于敌人攻击:总伤害 = 伤害总和(不受 影响)。
于是根据这个思路,我们可以写出以下代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
int u = 0, i = 0;
};
int a[1000005] = {0};
node b[1000005];
node c[1000005];
int n, m, q;
int func(int m, node cnt[], int x) {
int l = 0, r = n + 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (cnt[mid].u + cnt[mid].i * x >= m) r = mid - 1;
else l = mid;
}
return l;
}
signed main(){
scanf("%lld%lld%lld", &n, &m, &q);
for (int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
for (int i = 1; i <= n; i ++) {
if (a[i] > 0) b[i].u = b[i - 1].u + abs(a[i]), b[i].i = b[i - 1].i + 1;
else c[i].u = c[i - 1].u + abs(a[i]), c[i].i = c[i - 1].i + 1;
if(b[i].u == 0) b[i].u = b[i - 1].u;
if(c[i].u == 0) c[i].u = c[i - 1].u;
if(b[i].i == 0) b[i].i = b[i - 1].i;
if(c[i].i == 0) c[i].i = c[i - 1].i;
}
for (int i = 1; i <= q; i ++) {
int x;
cin >> x;
int me = func(m, c, 0);
int en = func(m, b, x);
// cout << me << endl << en << endl;
if(me == en) printf("Tie\n");
if(me < en) printf("No\n");
else if (en < me) printf("Yes\n");
}
// for (int i = 1; i<=n; i ++) {
// cout << b[i].u <<b[i].i <<c[i].u <<c[i].i <<endl;
// }
return 0;
} 哎不对,为什么只有 60 分呢?
时间复杂度是 没问题啊。
这时,看到题面:本题输入输出量较大,请注意 IO 效率。
再看到时间限制:。
所以我们不能用 ,,得用效率更高的 ,。
于是, 了:
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
int u = 0, i = 0;
};
int a[1000005] = {0};
node b[1000005];
node c[1000005];
int n, m, q;
int func(int m, node cnt[], int x) {
int l = 0, r = n + 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (cnt[mid].u + cnt[mid].i * x >= m) r = mid - 1;
else l = mid;
}
return l;
}
signed main(){
scanf("%lld%lld%lld", &n, &m, &q);
for (int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
for (int i = 1; i <= n; i ++) {
if (a[i] > 0) b[i].u = b[i - 1].u + abs(a[i]), b[i].i = b[i - 1].i + 1;
else c[i].u = c[i - 1].u + abs(a[i]), c[i].i = c[i - 1].i + 1;
if(b[i].u == 0) b[i].u = b[i - 1].u;
if(c[i].u == 0) c[i].u = c[i - 1].u;
if(b[i].i == 0) b[i].i = b[i - 1].i;
if(c[i].i == 0) c[i].i = c[i - 1].i;
}
for (int i = 1; i <= q; i ++) {
int x;
scanf("%lld", &x);
int me = func(m, c, 0);
int en = func(m, b, x);
// cout << me << endl << en << endl;
if(me == en) printf("Tie\n");
if(me < en) printf("No\n");
else if (en < me) printf("Yes\n");
}
// for (int i = 1; i<=n; i ++) {
// cout << b[i].u <<b[i].i <<c[i].u <<c[i].i <<endl;
// }
return 0;
} 蒟蒻的第一篇题解,求过 awa。