#63199. [USACO 2023 Jan Bronze]Leaders
[USACO 2023 Jan Bronze]Leaders
暂无测试数据。
Farmer John has N cows $(2\leq N\leq 10^5)$. Each cow has a breed that is either Guernsey or Holstein. As is often the case, the cows are standing in a line, numbered 1…N in this order.
Over the course of the day, each cow writes down a list of cows. Specifically, cow i's list contains the range of cows starting with herself (cow i) up to and including cow $E_i(i\leq E_i\leq N)$.
FJ has recently discovered that each breed of cow has exactly one distinct leader. FJ does not know who the leaders are, but he knows that each leader must have a list that includes all the cows of their breed, or the other breed's leader (or both).
Help FJ count the number of pairs of cows that could be leaders. It is guaranteed that there is at least one possible pair.
译文
农夫约翰有 $N$ 头牛($2\leq N\leq 10^5$)。每头牛都有一个品种,不是更赛牛就是荷斯坦牛。通常情况下,牛站成一排,按照从 $1$ 到 $N$ 这个顺序。
在一天的过程中,每头奶牛都写下一份奶牛名单。具体来说,牛 $i$ 的列表包含了从她自己开始的奶牛(奶牛 $i$),直到并包括牛 $E_i(i\leq E_i\leq N)$.
FJ 最近发现每个品种的牛都有一个不同的领导者。FJ 不知道领导者是谁,但他知道每个领导者必须有一个列表,包括他们品种的所有奶牛,或其他品种的领导者(或两者都有)。
帮助 FJ 计算可以成为领导者的奶牛对数。它保证至少有一个可能的对。
输入格式
The first line contains N.
The second line contains a string of length N, with the ith character denoting the breed of the ith cow (G meaning Guernsey and H meaning Holstein). It is guaranteed that there is at least one Guernsey and one Holstein.
The third line contains E1…EN.
第一行包含 $N$。
第二行包含一个长度为 $N$ 的字符串,第 $i$ 个字符表示第 $i$ 头奶牛的品种($G$ 表示更赛牛,$H$ 表示荷斯坦牛)。可以保证至少有一个更赛牛和一个荷斯坦牛。
第三行包含 $E_1 \cdots E_N$。
输出格式
Output the number of possible pairs of leaders.
输出可能成为领导者的奶牛对数。
数据范围
Inputs 3-5: N≤100
Inputs 6-10: N≤3000
Inputs 11-17: No additional constraints.
4
GHHG
2 4 3 4
1
3
GGH
2 3 3
2