Imagine you've arrived at the annual NTHU Career & Recruitment Fair, where along a long corridor there are N drink booths arranged in a line from the entrance to the exit. Due to foot-traffic and location, prices vary significantly.
Booths near the two ends (entrance and exit) typically have higher visibility and can charge higher prices.
Mid-corridor booths generally have less concentrated traffic, leading to potentially lower prices.
Each type of drink appears at least three times, and booths selling the same drink may charge identical prices.
You wish to evaluate each drink’s popularity score and determine the number of distinct booth combinations that achieve this score, based on their price distribution as you walk by.
Suppose a drink appears at booths 1 ≤ i1 < i2 < ⋯ < im ≤ N with corresponding prices p1 , p2 , … , pm.
Define:
The popularity score S = max(pu - pv + pw) , for all u < v < w
The number of distinct triples reaching this maximum score C = (the number of (u,v,w) such that pu - pv + pw = S), counted modulo 109+7.
Intuitively, you select an early booth at price pu, a middle booth at price pv, and a later booth at price pw; higher outer prices combined with a lower middle price yield a higher popularity score.
Note: This formula does not necessarily imply mid-corridor booths are always cheaper, but it helps capture a general trend in drink popularity.
The first line contains a single integer N, the total number of drink booths arranged in a line at the NTHU Career & Recruitment Fair.
The second line contains N space-separated strings s1,s2,…,sN , where si is the name of the drink sold at booth i.
The third line contains N space-separated integers a1,a2,…,aN , where ai is the price charged by booth i.
The fourth line contains an integer Q, the number of queries you will make, and then Q more lines each containing a single string Tj ; each Tj is guaranteed to match at least one of the si.
1 ≤ N,Q ≤ 2×105
1 ≤ | si |,| Tj | ≤ 10
1 ≤ ai ≤ 108
Every drink appears at least three times.
Every queried drink Tj matches at least one of the drinks si.
Testcases 1 ~ 2: si ,Tj = "a", no duplicate prices for the same drink.(The number of triples C is always equal to 1)
Testcases 3 ~ 4: N,Q ≤ 1000, no duplicate prices for the same drink.(The number of triples C is always equal to 1)
Testcases 5 ~ 6: No duplicate prices for the same drink.(The number of triples C is always equal to 1)
Testcases 7 ~ 8: The number of triples C ≤ 10.
Testcases 9 ~ 10: No additional restrictions.
multiset
allows multiple elements with equivalent values, whereas a set
enforces uniqueness.find(val)
returns an iterator to one matching element (not all of them).ms.erase(val)
removes all elements equal to val
.
To remove only a single occurrence, use auto it = ms.find(val); if (it != ms.end()) ms.erase(it)
(best, ways)
in map<string, pair<ll,ll>> ans
so that a repeated query for the same drink returns in O(log n) time without recomputation.For each query Tj, output a single line containing two integers:
S(Tj): the maximum popularity score.
C(Tj): the number of triples that achieve this score, modulo 109+7.