Submission details
Task:Frontier
Sender:Hornet's Multithreading
Submission time:2025-11-08 16:13:46 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.01 sdetails
#9ACCEPTED0.01 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.02 sdetails
#18ACCEPTED0.01 sdetails
#19ACCEPTED0.01 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;


const int S = 2e3 + 5;

vector<int> col;
vector<int> topo;
vector<int> pos[S];

void dfs(vector<vector<int>>& adj, int u) {
    col[u] = 1;
    for (int v : adj[u]) {
        if (!col[v]) {
            dfs(adj, v);
        }
    }
    topo.push_back(u);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int n, s;
    cin >> n >> s;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        pos[a[i]].emplace_back(i);
    }

    vector<int> st(s + 1, n + 1), en(s + 1, -1);
    {
        vector<bool> vis(s + 1);
        for (int i = 1; i + 1 < n; i++) {
            if (!vis[a[i]]) {
                st[a[i]] = i; 
                vis[a[i]] = true;
            }
        }
    }

    {
        vector<bool> vis(s + 1);
        for (int i = n - 1; i >= 1; i--) {
            if (!vis[a[i]]) {
                en[a[i]] = i;
                vis[a[i]] = true;
            }
        }
    }

    vector<vector<int>> adj(s + 1);
    for (int i = 0; i <= s; i++) {
        for (int j = i + 1; j <= s; j++) {
            if (max(st[i], st[j]) <= min(en[i], en[j])) {
                if (st[i] < st[j]) {
                    adj[i].push_back(j);
                    // if there is j i j i => false
                    if (pos[i].size() >= 2 && pos[j].size() >= 2) {
                        int cur = pos[j][0];
                        auto x = lower_bound(pos[i].begin(), pos[i].end(), cur);
                        if (x != pos[i].end()) {
                            cur = *x;
                            auto y = lower_bound(pos[j].begin(), pos[j].end(), cur);
                            if (y != pos[j].end()) {
                                cur = *y;
                                auto z = lower_bound(pos[i].begin(), pos[i].end(), cur);
                                if (z != pos[i].end()) {
                                    cout << "NO";
                                    exit(0);
                                }
                            }
                        }
                    }
                    // cerr << i << ' ' << j << '\n';
                } else {
                    adj[j].push_back(i);            
                    swap(i, j);    
                    if (pos[i].size() >= 2 && pos[j].size() >= 2) {
                        int cur = pos[j][0];
                        auto x = lower_bound(pos[i].begin(), pos[i].end(), cur);
                        if (x != pos[i].end()) {
                            cur = *x;
                            auto y = lower_bound(pos[j].begin(), pos[j].end(), cur);
                            if (y != pos[j].end()) {
                                cur = *y;
                                auto z = lower_bound(pos[i].begin(), pos[i].end(), cur);
                                if (z != pos[i].end()) {
                                    cout << "NO";
                                    exit(0);
                                }
                            }
                        }
                    }    
                    swap(i, j);
                    // cerr << j << ' ' << i << '\n';
                }
            }
        }
    }

    col.resize(s + 1);
    for (int i = 0; i <= s; i++) {
        if (!col[i]) {
            dfs(adj, i);
        }
    }

    reverse(all(topo));

    vector<int> ord(s + 1);
    for (int i = 0; i < sz(topo); i++) {
        ord[topo[i]] = i;
    }

    for (int i = 0; i <= s; i++) {
        for (int v : adj[i]) {
            if (ord[v] < ord[i]) {
                cout << "NO";
                return 0;
            }
        }
    }
    cout << "YES";
}

Test details

Test 1

Verdict: ACCEPTED

input
8 3
0 1 3 1 3 2 3 0

correct output
YES

user output
YES

Test 2

Verdict: ACCEPTED

input
3 1
0 1 0

correct output
YES

user output
YES

Test 3

Verdict: ACCEPTED

input
10 10
0 2 3 2 8 4 7 6 7 0

correct output
YES

user output
YES

Test 4

Verdict: ACCEPTED

input
18 100
0 95 64 79 64 68 64 88 58 88 8...

correct output
YES

user output
YES

Test 5

Verdict: ACCEPTED

input
105 1000
0 110 123 192 371 192 94 192 3...

correct output
YES

user output
YES

Test 6

Verdict: ACCEPTED

input
76 1000
0 615 320 101 56 683 391 350 3...

correct output
YES

user output
YES

Test 7

Verdict: ACCEPTED

input
112 2000
0 116 1100 256 324 256 876 256...

correct output
YES

user output
YES

Test 8

Verdict: ACCEPTED

input
115 2000
0 388 1955 1661 1417 1580 1053...

correct output
YES

user output
YES

Test 9

Verdict: ACCEPTED

input
92 2000
0 309 838 1212 553 863 553 141...

correct output
YES

user output
YES

Test 10

Verdict: ACCEPTED

input
18 10
0 3 4 10 9 4 2 8 6 8 2 7 2 9 1...

correct output
YES

user output
YES

Test 11

Verdict: ACCEPTED

input
53 100
0 1 3 82 100 25 27 85 61 53 61...

correct output
NO

user output
NO

Test 12

Verdict: ACCEPTED

input
94 1000
0 675 229 663 771 140 314 140 ...

correct output
YES

user output
YES

Test 13

Verdict: ACCEPTED

input
110 1000
0 493 827 157 538 946 1 736 31...

correct output
YES

user output
YES

Test 14

Verdict: ACCEPTED

input
96 1000
0 206 843 543 401 819 800 444 ...

correct output
NO

user output
NO

Test 15

Verdict: ACCEPTED

input
198 2000
0 1661 1800 19 1666 879 1021 9...

correct output
NO

user output
NO

Test 16

Verdict: ACCEPTED

input
538 2000
0 1138 1562 1509 1339 735 1993...

correct output
NO

user output
NO

Test 17

Verdict: ACCEPTED

input
98999 2000
0 1798 1467 884 1426 1191 601 ...

correct output
NO

user output
NO

Test 18

Verdict: ACCEPTED

input
3373 2000
0 121 0 1094 0 58 0 306 0 1273...

correct output
YES

user output
YES

Test 19

Verdict: ACCEPTED

input
3370 2000
0 1602 0 455 0 710 0 1514 0 84...

correct output
YES

user output
YES