QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#135. 又一个游戏 AI

Statistics

Aoba 是一名在游戏公司工作的初级程序员。她被指派为一款新游戏中的敌方 AI(人工智能)开发战斗策略。在这个游戏中,每个角色都有两个参数:生命值($hp$)和防御力($dp$)。没有任何两个角色在同一时刻拥有相同的 $hp$ 和 $dp$。玩家通过选择一个或多个角色组成队伍来与敌人战斗。Aoba 决定开发一种策略,让 AI 攻击队伍中最弱的角色:即 AI 攻击队伍中生命值最低的角色(如果存在多个这样的角色,则攻击其中防御力最低的角色)。她编写了一个函数 selectTarget(v),该函数接收一个代表队伍的角色数组,并返回 AI 将要攻击的角色。

然而,项目经理八神女士对她的 AI 表现并不满意。八神女士说这个 AI 并不“有趣”。

Aoba 苦思冥想,最终发现如果将程序中的一个常数 $0$ 替换为常数 $C$,就会变得很有趣。重写后的程序如下所示。注意 Character 是一个表示角色的类型,拥有字段 hpdp,分别代表角色的生命值和防御力。

int C = <constant integer>;
Character selectTarget(Character v[]) {
    int n = length(v);
    int r = 0;
    for (int i = 1; i < n; i++) {
        if (abs(v[r].hp - v[i].hp) > C) {
            if (v[r].hp > v[i].hp) r = i;
        } else {
            if (v[r].dp > v[i].dp) r = i;
        }
    }
    return v[r];
}

顺便提一下,即使队伍 $v$ 包含相同的角色集合,该函数也可能根据 $v$ 中角色的顺序返回不同的角色。八神女士想知道队伍中有多少个角色可能成为新 AI 的攻击目标。Aoba 的下一个任务是编写一个程序,输入给定的队伍 $v$ 和常数 $C$,然后计算如果 $v$ 的顺序被重新排列,有多少个角色可能成为 selectTarget(v) 的返回值。

输入格式

输入包含单个测试用例。第一行包含两个整数 $N$ ($1 \le N \le 5 \cdot 10^4$) 和 $C$ ($0 \le C \le 10^9$)。第一个整数 $N$ 代表 $v$ 的大小。第二个整数 $C$ 代表 Aoba 程序中的常数 $C$。接下来的 $N$ 行中,第 $i$ 行包含两个整数 $hp_i$ 和 $dp_i$ ($0 \le hp_i, dp_i \le 10^9$)。$hp_i$ 代表 $v$ 中第 $i$ 个角色的生命值,$dp_i$ 代表 $v$ 中第 $i$ 个角色的防御力。你可以假设对于任何 $1 \le i, j \le N$,如果 $i \neq j$,则 $hp_i \neq hp_j$ 或 $dp_i \neq dp_j$。

输出格式

输出如果 $v$ 以任意顺序洗牌,可能成为 selectTarget(v) 返回值的角色数量。

样例

样例输入 1

5 3
1 5
3 4
5 3
7 2
9 1

样例输出 1

5

样例输入 2

3 2
1 2
3 1
5 1

样例输出 2

2

样例输入 3

4 1
2 0
0 4
1 1
5 9

样例输出 3

3

样例输入 4

5 9
4 10
12 4
2 14
9 19
7 15

样例输出 4

3

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.