日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

补题Codeforces 1102E. Monotonic Renumeration

發布時間:2023/12/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 补题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}。

#include <iostream> #include <vector> #include <map> using namespace std; int a[200005]; int n;typedef long long int LL; const LL P = 998244353;LL pow2(LL x) {LL pow = 2, ans = 1;while (x > 0) {if (x & 1)ans = (ans * pow) % P;pow = (pow * pow) % P;x >>= 1;}return ans; }int main() {map<int, int> indMap;vector<pair<int, int>> s;cin >> n;for (int i = 0; i < n; i++) {cin >> a[i];if (indMap.find(a[i]) == indMap.end()) {indMap[a[i]] = i;}}for (int i = n - 1; i >= 0; i--) {pair<int, int> interval;if (indMap.find(a[i]) != indMap.end() && indMap[a[i]] < i) {interval = make_pair(indMap[a[i]], i);}else {interval = make_pair(i, i);}if (!s.empty() && s.back().first <= interval.first && s.back().second >= interval.second)continue;if (!s.empty() && interval.second >= s.back().first) {interval.second = s.back().second;s.pop_back();s.push_back(interval);}if (s.empty() || interval.second < s.back().first)s.push_back(interval);}int cnt = 0;if (!s.empty() && s.front().second < n - 1) cnt++;if (!s.empty() && s.back().first > 0) cnt++;for (int i = 0; i < s.size(); i++) {cnt++;// 本條線段和前一條線段之間的間隔if (i > 0 && s[i - 1].second < s[i].first - 1)cnt++;}cout << pow2(cnt - 1) << endl;return 0; }

總結

以上是生活随笔為你收集整理的补题Codeforces 1102E. Monotonic Renumeration的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。