Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.7 kB
2
Indexable
Never
// #define Many_SubTask
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(a) begin(a), end(a)
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
const int mod = 998244353;
const int inf = 1e18;
const int MAXN = 1000005;

int conflict[MAXN], a[MAXN], n, m;
unordered_set<int> cur_group;
int Group[MAXN];
unordered_set<int> group_member[MAXN];
int last_group = 1;
set<int> smallest_group;

void solve() {
    cin >> n >> m;
    int pre = inf;
    for (int i = 1; i <= m; i++) {
        int x;
        cin >> x;
        if (pre < x) {
            conflict[x] = pre;
        }
        pre = x;
        a[i] = x;
    }
    smallest_group.insert(1);
    for (int i = 1; i <= n; i++) {
        if (conflict[i]) {
            if (cur_group.count(conflict[i])) {
                group_member[last_group] = cur_group;
                last_group++;
                smallest_group.insert(last_group);
                cur_group.clear();
            }
        }
        cur_group.insert(i);
        Group[i] = last_group;
    }
    group_member[last_group] = cur_group;
    
    group_member[Group[a[1]]].erase(a[1]);
    if (group_member[Group[a[1]]].empty()) smallest_group.erase(Group[a[1]]);

    for (int i = 2; i <= m; i++) {
        if (a[i - 1] > a[i]) {
            if (Group[a[i]] == Group[a[i - 1]]) last_group++;
        }
        group_member[Group[a[i]]].erase(a[i]);
        if (group_member[Group[a[i]]].empty()) smallest_group.erase(Group[a[i]]);
    }

    cout << last_group;
}

signed main() {
    cin.sync_with_stdio(0), cin.tie(0);
    int N = 1;
#ifdef Many_SubTask
    cin >> N;
#endif
    for (int i = 1; i <= N; i++) {
        solve();
    }
}
Leave a Comment