补题Codeforces 1102E. Monotonic Renumeration
這個題還是不太懂,下面附上的是大佬的題解(https://zhanghuimeng.github.io/post/codeforces-1102e-monotonic-renumeration/)
E. Monotonic Renumeration
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers. Let’s denote monotonic renumeration of array a as an array b consisting of n integers such that all of the following conditions are met:
b1=0;
for every pair of indices i and j such that 1≤i,j≤n, if ai=aj, then bi=bj (note that if ai≠aj, it is still possible that bi=bj);
for every index i∈[1,n?1] either bi=bi+1 or bi+1=bi+1.
For example, if a=[1,2,1,2,3], then two possible monotonic renumerations of a are b=[0,0,0,0,0] and b=[0,0,0,0,1].
Your task is to calculate the number of different monotonic renumerations of a. The answer may be large, so print it modulo 998244353.
Input
The first line contains one integer n (2≤n≤2?105) — the number of elements in a.
The second line contains n integers a1,a2,…,an (1≤ai≤109).
Output
Print one integer — the number of different monotonic renumerations of a, taken modulo 998244353.
Examples
inputCopy
5
1 2 1 2 3
outputCopy
2
inputCopy
2
100 1
outputCopy
2
inputCopy
4
1 3 3 7
outputCopy
4
題意
給定一個長度為n的數組a,要求為a生成一個對應的數組b,滿足:
b[0] = 0
對于任意0 <= i < j <= n,如果滿足a[i] == a[j],必有b[i] == b[j](不過a[i] != a[j]時也可能有b[i] == b[j])
任取0 <= i < n - 1,必有b[i] = b[i+1]或b[i] + 1 = b[i+1]
問共有多少種可能的b。
分析
顯然b[i]是一個遞增序列,因此可以自然推出,若a[i] == a[j],則必有b[i] == b[i+1] == … = b[j],也就是說,對于a中任意位置兩個相等的元素,它們在b中對應的是一整段相等的元素。顯然這種元素相等是可能會發生重疊的,因此一個自然的想法就是,把重復的元素建模成線段,然后合并發生overlap的線段以得到相等元素的最長長度。
我的做法是,從后向前遍歷a,如果發現當前元素和后面的元素重復了,則取index最靠后的元素,組成一條線段,插入到棧中與其他元素合并;否則把它自己的index作為一條線段插入到棧中。最后棧中留下的就是幾條互不相交(且并組成了整個區間)的線段。
對于(除了第一條之外)每條線段,我們可以選擇讓它的值和前一條相等,也可以選擇讓它的值是前一條+1。每種選擇都會導致生成一種新的b。于是結果是2^{線段數-1}。
例子:對于a = {1, 2, 1, 2, 3},1對應的線段是[0, 2],2對應的線段是[1, 3],3對應的線段是[4, 4];合并之后得到兩條線段,[0, 3]和[1, 4];只有兩種b,分別是{0, 0, 0, 0, 0}和{0, 0, 0, 0, 1}。
總結
以上是生活随笔為你收集整理的补题Codeforces 1102E. Monotonic Renumeration的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Entities、pads、links
- 下一篇: 图解 eclicpse 智能提示 配置