CF2264C.Madamant's Skating Dynasty
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Madam Madamant is expecting a baby and already planning a skating dynasty in which every parent is a stronger skater than their children.
Formally, Madamant has n labeled skaters. Skater v has an integer rating av, and all ratings are pairwise distinct. A possible dynasty is represented by a rooted tree on these skaters.
Let r be the root of the tree. For every skater v=r, let pv be the parent of v. The dynasty is valid if av<apv for every v=r.
The cost of a valid dynasty is $$ \sum_{v \ne r} (a_{p_v} - a_v). $$
Two dynasties are different if their roots are different or if the parent of at least one skater is different.
Find the sum of the costs of all valid dynasties Madamant can form, modulo 998244353.
玛丹夫人即将迎来宝宝,并已开始规划一个滑冰王朝,其中每位父母的滑冰水平都强于其子女。
形式化地,玛丹夫人有 n 位编号的滑冰者。滑冰者 v 具有一个整数评分 av,且所有评分两两互异。一个可能的王朝由这些滑冰者构成的一棵有根树表示。
设 r 为该树的根节点。对每个滑冰者 v=r,令 pv 表示 v 的父节点。若对每个 v=r 均满足 av<apv,则该王朝是合法的。
一个合法王朝的代价定义为
v=r∑(apv−av).
若两棵王朝树的根节点不同,或至少存在一位滑冰者的父节点不同,则认为这两棵王朝树不同。
求玛丹夫人所能构造的所有合法王朝的代价之和,对 998244353 取模的结果。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the number of skaters.
The second line contains n integers a1,a2,…,an (1≤ai≤109) — their ratings.
It is guaranteed that all ai are pairwise distinct.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)——滑冰者的数量。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)——他们的评分。
保证所有 ai 两两不同。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, print one integer — the sum of the costs of all valid dynasties, modulo 998244353.
对于每个测试用例,输出一个整数——所有合法王朝的代价之和对 998244353 取模的结果。
输入输出样例
输入#1
5 1 10 3 1 2 3 4 4 1 3 2 2 1 1000000000 5 2 7 1 10 4
输出#1
0 5 27 1755646 414
说明/提示
In the second test case, the skater with rating 3 must be the root. The parent of the skater with rating 2 must be the skater with rating 3, while the skater with rating 1 can choose either of the other skaters as their parent. The two valid dynasties have costs 2 and 3, so the answer is 5.
In the fourth test case, there is only one valid dynasty. Its cost is 109−1=999999999, whose remainder modulo 998244353 is 1755646.
在第二个测试用例中,评分为 3 的滑冰者必须为根节点。评分为 2 的滑冰者的父节点必须是评分为 3 的滑冰者,而评分为 1 的滑冰者则可任选其余两名滑冰者之一作为其父节点。两种合法的家族树(dynasty)的成本分别为 2 和 3,因此答案为 5。
在第四个测试用例中,仅存在一种合法的家族树。其成本为 109−1=999999999,该值对 998244353 取模的余数为 1755646。
输入解题思路,AI测评打分。不知道怎么写?