当前位置:网站首页>MEX有关的学习
MEX有关的学习
2022-07-06 07:35:00 【eva_can(not)survive】
MEX是一段区间内未出现的最小正整数。
所以向该区间加一个数只有加入该区间的mex值时才能增加;
我们来看两个题目
Problem - C - Codeforces
https://codeforces.com/contest/1699/problem/C首先我们可以思考一个区间【L,R】的mex是x,说明【0,x-1】肯定都出现过了,所以如果x的位置在该区间内部则可以在区间内任意一个未被使用的位置,如果不在该区间内就只能在那个位置不动了,因为x一旦动那么该序列的mex值肯定会受到影响。
int a[MAXN];
int pos[MAXN];
const int mod = 1e9 + 7;
void solve() {
int n;
ll ans = 1;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
pos[a[i]] = i;
}
int mx = -INF, mn = INF;
for (int i = 0; i < n; i++) {
if (pos[i] > mn && pos[i] < mx) {
ans *= (mx - mn + 1 - i);
ans %= mod;
}
mn = min(mn, pos[i]);
mx = max(mx, pos[i]);
}
printf("%lld\n", ans);
}然后是这一题 需要求我们能得出得最大字典序序列,所以我们b序列中得到得元素肯定是能选到得最大得mex,所以我们可以维护一个前缀mex和后缀mex,当我们的前缀mex此时等于最大的mex时候就可以加入答案数组,然后直接从下一点开始把mex值替换成后缀mex的值
struct MEX {
int cot[MAXN];
vector<int> res;
int id = 0;
void add(int x) {
cot[x]++;
res.push_back(x);
}
int mex() {
while (cot[id])
id++;
return id;
}
void clear() {
for (int v : res) {
cot[v]--;
}
id = 0;
res.clear();
}
};
MEX now, t;
int a[MAXN];
int last[MAXN];
void solve() {
now.clear();
t.clear();
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = n; i >= 1; i--) {
t.add(a[i]);
last[i] = t.mex();
}
vector<int> ans;
int mx = last[1];
for (int i = 1; i <= n; i++) {
now.add(a[i]);
if (mx == now.mex()) {
ans.push_back(mx);
mx = last[i + 1];
now.clear();
}
}
printf("%d\n", ans.size());
for (int v : ans) {
printf("%d ", v);
}
putchar('\n');
}
边栏推荐
- Go learning --- use reflection to judge whether the value is valid
- NiO programming introduction
- TypeScript接口与泛型的使用
- CF1036C Classy Numbers 题解
- Leecode-c language implementation -15 Sum of three ----- ideas to be improved
- Sharing of source code anti disclosure scheme under burning scenario
- Iterator Foundation
- Jerry's general penetration test - do data transmission with app Communication [article]
- 杰理之AD 系列 MIDI 功能说明【篇】
- How Navicat imports MySQL scripts
猜你喜欢

Bugku CTF daily question: do you want seeds? Blackmailed

TS 类型体操 之 循环中的键值判断,as 关键字使用

Summary of Digital IC design written examination questions (I)

Do you really think binary search is easy
![Jerry's ad series MIDI function description [chapter]](/img/28/e0f9b41db597ff3288af431c677001.png)
Jerry's ad series MIDI function description [chapter]

软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历

How Navicat imports MySQL scripts

Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
随机推荐
实现精细化生产, MES、APS、ERP必不可少
TypeScript void 基础类型
Significance and measures of encryption protection for intelligent terminal equipment
Methods for JS object to obtain attributes (. And [] methods)
Seriously recommend several machine learning official account
GET/POST/PUT/PATCH/DELETE含义
烧录场景下的源代码防泄密方案分享
xpath中的position()函数使用
Simulation of Teman green interferometer based on MATLAB
[MySQL learning notes 29] trigger
TypeScript 函数定义
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]
http缓存,强制缓存,协商缓存
Get the path of edge browser
Typescript void base type
Excel的相关操作
Ble of Jerry [chapter]
ORACLE列转行--某字段按指定分隔符转多行
杰理之AD 系列 MIDI 功能说明【篇】
QT color is converted to string and uint
https://codeforces.com/contest/1629/problem/C