codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2264C.md (2227B)


      1 Source: https://codeforces.com/problemset/problem/2264/C
      2 
      3 C. Madamant's Skating Dynasty
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Madam Madamant is expecting a baby and already planning a skating dynasty in which every parent is a stronger skater than their children.
     22 
     23 Formally, Madamant has $n$ labeled skaters. Skater $v$ has an integer rating $a_v$, and all ratings are pairwise distinct. A possible dynasty is represented by a rooted tree on these skaters.
     24 
     25 Let $r$ be the root of the tree. For every skater $v \ne r$, let $p_v$ be the parent of $v$. The dynasty is valid if $a_v \lt a_{p_v}$ for every $v \ne r$.
     26 
     27 The cost of a valid dynasty is $$ \sum_{v \ne r} (a_{p_v} - a_v). $$
     28 
     29 Two dynasties are different if their roots are different or if the parent of at least one skater is different.
     30 
     31 Find the sum of the costs of all valid dynasties Madamant can form, modulo $998\,244\,353$.
     32 
     33 Input
     34 
     35 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     36 
     37 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of skaters.
     38 
     39 The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — their ratings.
     40 
     41 It is guaranteed that all $a_i$ are pairwise distinct.
     42 
     43 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     44 
     45 Output
     46 
     47 For each test case, print one integer — the sum of the costs of all valid dynasties, modulo $998\,244\,353$.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 5
     55 
     56 1
     57 
     58 10
     59 
     60 3
     61 
     62 1 2 3
     63 
     64 4
     65 
     66 4 1 3 2
     67 
     68 2
     69 
     70 1 1000000000
     71 
     72 5
     73 
     74 2 7 1 10 4
     75 ```
     76 
     77 Output
     78 
     79 ```
     80 0
     81 
     82 5
     83 
     84 27
     85 
     86 1755646
     87 
     88 414
     89 ```
     90 
     91 Note
     92 
     93 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$.
     94 
     95 In the fourth test case, there is only one valid dynasty. Its cost is $10^9 - 1 = 999\,999\,999$, whose remainder modulo $998\,244\,353$ is $1\,755\,646$.