这个题还是不太懂,下面附上的是大佬的题解(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;
}

最新文章

  1. .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
  2. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
  3. iOS drewRect方法
  4. xcode调整debug,release模式
  5. 005_重写 Standard Delete Button
  6. python学习笔记:Day02
  7. Kafka是分布式发布-订阅消息系统
  8. 16-head 简明笔记
  9. appium + python 环境搭建
  10. H5 浏览器开发文档
  11. 如何用javascript 的eval动态执行一个需要传对象参数的函数
  12. 设置VMWARE通过桥接方式使用主机无线网卡上网(zz)
  13. 【原创】C++链表如何像Python List一样支持多种数据类型
  14. uitableview的重用重叠问题
  15. Windows Phone 8初学者开发—第4部分:XAML简介
  16. svn的基本配置及安装
  17. MySQL数据表中内容大小写区分的设置
  18. SQL Server 2008 sp3启用1433端口的方法
  19. [poj3687]Labeling Balls_拓扑排序
  20. 【C#复习总结】析构函数

热门文章

  1. python 爬虫之 urllib库
  2. npm install报错:chromedriver@2.27.2 install: node install.js
  3. [Python] 字符串加密解密
  4. 听说你想要部署 Octopress?满足你
  5. public、private、protected继承区别
  6. Nginx如何来配置隐藏入口文件index.php(代码)
  7. xxx 表 is marked as crashed and last (automatic?) repair 解决办法
  8. VulnHub靶场学习_HA: ARMOUR
  9. 打印图片的属性和实现另存图片功能以及使用numpy
  10. Linux终端命令格式