Submission #1002688

# Submission time Handle Problem Language Result Execution time Memory
1002688 2024-06-19T18:12:35 Z aykhn Watermelon (INOI20_watermelon) C++17
100 / 100
70 ms 22864 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define inf 0x3F3F3F3F

const int MXN = 1e5 + 5;
const int LOG = 20;

vector<int> adj[MXN];
int used[MXN];
int cyc = 0;

void dfs(int a)
{
  used[a] = 1;
  for (int &v : adj[a])
  {
    if (!used[v]) dfs(v);
    else if (used[v] == 1)
    {
      cyc = 1;
    }
  }
  used[a] = 2;
}

int n, k;
vector<int> perm, res;
int deg[MXN];
set<int> s;
int cnt = 0;


void findkth()
{
  if (perm.size() == n)
  {
    if (++cnt == k) res = perm;
    return;
  }
  for (int i = 0; i < s.size(); i++)
  {
    auto it = s.begin();
    for (int j = 0; j < i; j++) it++;
    int x = *it;
    perm.push_back(x);
    s.erase(it);
    vector<int> tmp;
    for (int &v : adj[x]) 
    {
      if (--deg[v] == 0) s.insert(v);
    }
    findkth();
    if (!res.empty()) return;
    perm.pop_back();
    s.insert(x);
    for (int &v : adj[x])
    {
      if (deg[v]++ == 0) s.erase(v);
    }
  }
}

signed main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  int a[n + 1];
  for (int i = 1; i <= n; i++) cin >> a[i];
  if (a[n] != -1)
  {
    cout << -1 << '\n';
    return 0;
  }
  for (int i = 1; i <= n; i++)
  {
    if (a[i] == -1) a[i] = inf + n - i;
  }
  vector<int> v;
  for (int i = n; i >= 1; i--)
  {
    int mx = 0;
    while (!v.empty() && a[v.back()] < a[i])
    {
      adj[v.back()].push_back(i);
      deg[i]++;
      mx = max(mx, a[v.back()]);
      v.pop_back();
    }
    if (a[i] <= n && mx + 1 != a[i])
    {
      cout << -1 << '\n';
      return 0;
    }
    if (!v.empty())
    {
      adj[i].push_back(v.back());
      deg[v.back()]++;
      if (a[v.back()] == a[i]) v.pop_back();
    } 
    v.push_back(i);
  }
  for (int i = 1; i <= n; i++) if (!used[i]) dfs(i);
  if (cyc)
  {
    cout << -1 << '\n';
    return 0;
  }
  for (int i = 1; i <= n; i++) if (!deg[i]) s.insert(i);
  findkth();
  if (res.empty())
  {
    cout << -1 << '\n';
    return 0;
  }
  int ans[n + 1];
  for (int i = 0; i < n; i++) ans[res[i]] = i + 1;
  for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
  cout << '\n';
}

Compilation message

Main.cpp: In function 'void findkth()':
Main.cpp:37:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |   if (perm.size() == n)
      |       ~~~~~~~~~~~~^~~~
Main.cpp:42:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for (int i = 0; i < s.size(); i++)
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 2 ms 3164 KB Output is correct
35 Correct 2 ms 3164 KB Output is correct
36 Correct 1 ms 2648 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 3 ms 3076 KB Output is correct
39 Correct 2 ms 3200 KB Output is correct
40 Correct 2 ms 3164 KB Output is correct
41 Correct 3 ms 3084 KB Output is correct
42 Correct 2 ms 3164 KB Output is correct
43 Correct 2 ms 3164 KB Output is correct
44 Correct 2 ms 3164 KB Output is correct
45 Correct 1 ms 3164 KB Output is correct
46 Correct 2 ms 3164 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 2 ms 2652 KB Output is correct
51 Correct 2 ms 2648 KB Output is correct
52 Correct 2 ms 2652 KB Output is correct
53 Correct 2 ms 3164 KB Output is correct
54 Correct 3 ms 3164 KB Output is correct
55 Correct 2 ms 2652 KB Output is correct
56 Correct 2 ms 2652 KB Output is correct
57 Correct 2 ms 3160 KB Output is correct
58 Correct 2 ms 3164 KB Output is correct
59 Correct 2 ms 3164 KB Output is correct
60 Correct 2 ms 3164 KB Output is correct
61 Correct 2 ms 3076 KB Output is correct
62 Correct 2 ms 3164 KB Output is correct
63 Correct 3 ms 3164 KB Output is correct
64 Correct 2 ms 2908 KB Output is correct
65 Correct 2 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 12352 KB Output is correct
2 Correct 6 ms 3164 KB Output is correct
3 Correct 17 ms 12248 KB Output is correct
4 Correct 5 ms 3272 KB Output is correct
5 Correct 16 ms 12252 KB Output is correct
6 Correct 17 ms 12380 KB Output is correct
7 Correct 18 ms 12380 KB Output is correct
8 Correct 17 ms 12376 KB Output is correct
9 Correct 16 ms 12376 KB Output is correct
10 Correct 19 ms 12584 KB Output is correct
11 Correct 35 ms 21836 KB Output is correct
12 Correct 34 ms 21856 KB Output is correct
13 Correct 32 ms 21916 KB Output is correct
14 Correct 32 ms 21952 KB Output is correct
15 Correct 6 ms 3676 KB Output is correct
16 Correct 6 ms 3568 KB Output is correct
17 Correct 7 ms 3740 KB Output is correct
18 Correct 6 ms 3676 KB Output is correct
19 Correct 6 ms 3696 KB Output is correct
20 Correct 11 ms 3928 KB Output is correct
21 Correct 6 ms 3676 KB Output is correct
22 Correct 6 ms 3676 KB Output is correct
23 Correct 11 ms 3572 KB Output is correct
24 Correct 27 ms 21448 KB Output is correct
25 Correct 29 ms 21452 KB Output is correct
26 Correct 26 ms 21448 KB Output is correct
27 Correct 27 ms 21448 KB Output is correct
28 Correct 27 ms 21448 KB Output is correct
29 Correct 38 ms 21452 KB Output is correct
30 Correct 8 ms 8284 KB Output is correct
31 Correct 14 ms 12112 KB Output is correct
32 Correct 24 ms 19668 KB Output is correct
33 Correct 44 ms 21452 KB Output is correct
34 Correct 5 ms 4444 KB Output is correct
35 Correct 6 ms 6556 KB Output is correct
36 Correct 14 ms 11996 KB Output is correct
37 Correct 35 ms 21452 KB Output is correct
38 Correct 49 ms 21200 KB Output is correct
39 Correct 38 ms 21460 KB Output is correct
40 Correct 30 ms 22616 KB Output is correct
41 Correct 24 ms 21640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 12352 KB Output is correct
2 Correct 6 ms 3164 KB Output is correct
3 Correct 17 ms 12248 KB Output is correct
4 Correct 5 ms 3272 KB Output is correct
5 Correct 16 ms 12252 KB Output is correct
6 Correct 17 ms 12380 KB Output is correct
7 Correct 18 ms 12380 KB Output is correct
8 Correct 17 ms 12376 KB Output is correct
9 Correct 16 ms 12376 KB Output is correct
10 Correct 19 ms 12584 KB Output is correct
11 Correct 35 ms 21836 KB Output is correct
12 Correct 34 ms 21856 KB Output is correct
13 Correct 32 ms 21916 KB Output is correct
14 Correct 32 ms 21952 KB Output is correct
15 Correct 6 ms 3676 KB Output is correct
16 Correct 6 ms 3568 KB Output is correct
17 Correct 7 ms 3740 KB Output is correct
18 Correct 6 ms 3676 KB Output is correct
19 Correct 6 ms 3696 KB Output is correct
20 Correct 11 ms 3928 KB Output is correct
21 Correct 6 ms 3676 KB Output is correct
22 Correct 6 ms 3676 KB Output is correct
23 Correct 11 ms 3572 KB Output is correct
24 Correct 27 ms 21448 KB Output is correct
25 Correct 29 ms 21452 KB Output is correct
26 Correct 26 ms 21448 KB Output is correct
27 Correct 27 ms 21448 KB Output is correct
28 Correct 27 ms 21448 KB Output is correct
29 Correct 38 ms 21452 KB Output is correct
30 Correct 8 ms 8284 KB Output is correct
31 Correct 14 ms 12112 KB Output is correct
32 Correct 24 ms 19668 KB Output is correct
33 Correct 44 ms 21452 KB Output is correct
34 Correct 5 ms 4444 KB Output is correct
35 Correct 6 ms 6556 KB Output is correct
36 Correct 14 ms 11996 KB Output is correct
37 Correct 35 ms 21452 KB Output is correct
38 Correct 49 ms 21200 KB Output is correct
39 Correct 38 ms 21460 KB Output is correct
40 Correct 30 ms 22616 KB Output is correct
41 Correct 24 ms 21640 KB Output is correct
42 Correct 16 ms 12380 KB Output is correct
43 Correct 6 ms 3164 KB Output is correct
44 Correct 16 ms 12440 KB Output is correct
45 Correct 4 ms 3164 KB Output is correct
46 Correct 3 ms 3164 KB Output is correct
47 Correct 4 ms 3164 KB Output is correct
48 Correct 4 ms 3164 KB Output is correct
49 Correct 16 ms 12304 KB Output is correct
50 Correct 14 ms 12380 KB Output is correct
51 Correct 14 ms 12380 KB Output is correct
52 Correct 32 ms 22004 KB Output is correct
53 Correct 31 ms 21724 KB Output is correct
54 Correct 6 ms 3672 KB Output is correct
55 Correct 6 ms 3572 KB Output is correct
56 Correct 24 ms 19412 KB Output is correct
57 Correct 38 ms 21388 KB Output is correct
58 Correct 31 ms 21460 KB Output is correct
59 Correct 25 ms 21200 KB Output is correct
60 Correct 25 ms 21456 KB Output is correct
61 Correct 38 ms 22576 KB Output is correct
62 Correct 23 ms 20692 KB Output is correct
63 Correct 44 ms 21452 KB Output is correct
64 Correct 26 ms 21456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 2 ms 3164 KB Output is correct
35 Correct 2 ms 3164 KB Output is correct
36 Correct 1 ms 2648 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 3 ms 3076 KB Output is correct
39 Correct 2 ms 3200 KB Output is correct
40 Correct 2 ms 3164 KB Output is correct
41 Correct 3 ms 3084 KB Output is correct
42 Correct 2 ms 3164 KB Output is correct
43 Correct 2 ms 3164 KB Output is correct
44 Correct 2 ms 3164 KB Output is correct
45 Correct 1 ms 3164 KB Output is correct
46 Correct 2 ms 3164 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 2 ms 2652 KB Output is correct
51 Correct 2 ms 2648 KB Output is correct
52 Correct 2 ms 2652 KB Output is correct
53 Correct 2 ms 3164 KB Output is correct
54 Correct 3 ms 3164 KB Output is correct
55 Correct 2 ms 2652 KB Output is correct
56 Correct 2 ms 2652 KB Output is correct
57 Correct 2 ms 3160 KB Output is correct
58 Correct 2 ms 3164 KB Output is correct
59 Correct 2 ms 3164 KB Output is correct
60 Correct 2 ms 3164 KB Output is correct
61 Correct 2 ms 3076 KB Output is correct
62 Correct 2 ms 3164 KB Output is correct
63 Correct 3 ms 3164 KB Output is correct
64 Correct 2 ms 2908 KB Output is correct
65 Correct 2 ms 2908 KB Output is correct
66 Correct 18 ms 12352 KB Output is correct
67 Correct 6 ms 3164 KB Output is correct
68 Correct 17 ms 12248 KB Output is correct
69 Correct 5 ms 3272 KB Output is correct
70 Correct 16 ms 12252 KB Output is correct
71 Correct 17 ms 12380 KB Output is correct
72 Correct 18 ms 12380 KB Output is correct
73 Correct 17 ms 12376 KB Output is correct
74 Correct 16 ms 12376 KB Output is correct
75 Correct 19 ms 12584 KB Output is correct
76 Correct 35 ms 21836 KB Output is correct
77 Correct 34 ms 21856 KB Output is correct
78 Correct 32 ms 21916 KB Output is correct
79 Correct 32 ms 21952 KB Output is correct
80 Correct 6 ms 3676 KB Output is correct
81 Correct 6 ms 3568 KB Output is correct
82 Correct 7 ms 3740 KB Output is correct
83 Correct 6 ms 3676 KB Output is correct
84 Correct 6 ms 3696 KB Output is correct
85 Correct 11 ms 3928 KB Output is correct
86 Correct 6 ms 3676 KB Output is correct
87 Correct 6 ms 3676 KB Output is correct
88 Correct 11 ms 3572 KB Output is correct
89 Correct 27 ms 21448 KB Output is correct
90 Correct 29 ms 21452 KB Output is correct
91 Correct 26 ms 21448 KB Output is correct
92 Correct 27 ms 21448 KB Output is correct
93 Correct 27 ms 21448 KB Output is correct
94 Correct 38 ms 21452 KB Output is correct
95 Correct 8 ms 8284 KB Output is correct
96 Correct 14 ms 12112 KB Output is correct
97 Correct 24 ms 19668 KB Output is correct
98 Correct 44 ms 21452 KB Output is correct
99 Correct 5 ms 4444 KB Output is correct
100 Correct 6 ms 6556 KB Output is correct
101 Correct 14 ms 11996 KB Output is correct
102 Correct 35 ms 21452 KB Output is correct
103 Correct 49 ms 21200 KB Output is correct
104 Correct 38 ms 21460 KB Output is correct
105 Correct 30 ms 22616 KB Output is correct
106 Correct 24 ms 21640 KB Output is correct
107 Correct 16 ms 12380 KB Output is correct
108 Correct 6 ms 3164 KB Output is correct
109 Correct 16 ms 12440 KB Output is correct
110 Correct 4 ms 3164 KB Output is correct
111 Correct 3 ms 3164 KB Output is correct
112 Correct 4 ms 3164 KB Output is correct
113 Correct 4 ms 3164 KB Output is correct
114 Correct 16 ms 12304 KB Output is correct
115 Correct 14 ms 12380 KB Output is correct
116 Correct 14 ms 12380 KB Output is correct
117 Correct 32 ms 22004 KB Output is correct
118 Correct 31 ms 21724 KB Output is correct
119 Correct 6 ms 3672 KB Output is correct
120 Correct 6 ms 3572 KB Output is correct
121 Correct 24 ms 19412 KB Output is correct
122 Correct 38 ms 21388 KB Output is correct
123 Correct 31 ms 21460 KB Output is correct
124 Correct 25 ms 21200 KB Output is correct
125 Correct 25 ms 21456 KB Output is correct
126 Correct 38 ms 22576 KB Output is correct
127 Correct 23 ms 20692 KB Output is correct
128 Correct 44 ms 21452 KB Output is correct
129 Correct 26 ms 21456 KB Output is correct
130 Correct 1 ms 2652 KB Output is correct
131 Correct 1 ms 2652 KB Output is correct
132 Correct 1 ms 2652 KB Output is correct
133 Correct 31 ms 21732 KB Output is correct
134 Correct 29 ms 21968 KB Output is correct
135 Correct 31 ms 21844 KB Output is correct
136 Correct 6 ms 3676 KB Output is correct
137 Correct 5 ms 3700 KB Output is correct
138 Correct 7 ms 3704 KB Output is correct
139 Correct 10 ms 3676 KB Output is correct
140 Correct 5 ms 3676 KB Output is correct
141 Correct 5 ms 3672 KB Output is correct
142 Correct 34 ms 21824 KB Output is correct
143 Correct 51 ms 21844 KB Output is correct
144 Correct 42 ms 21356 KB Output is correct
145 Correct 25 ms 19528 KB Output is correct
146 Correct 26 ms 21368 KB Output is correct
147 Correct 46 ms 21200 KB Output is correct
148 Correct 46 ms 22864 KB Output is correct
149 Correct 70 ms 22612 KB Output is correct
150 Correct 43 ms 22612 KB Output is correct
151 Correct 36 ms 21604 KB Output is correct
152 Correct 35 ms 21568 KB Output is correct
153 Correct 27 ms 21452 KB Output is correct
154 Correct 29 ms 21448 KB Output is correct
155 Correct 37 ms 21452 KB Output is correct
156 Correct 24 ms 21452 KB Output is correct
157 Correct 25 ms 21404 KB Output is correct
158 Correct 25 ms 21452 KB Output is correct
159 Correct 25 ms 21448 KB Output is correct
160 Correct 27 ms 21452 KB Output is correct