比赛记录 - Codeforces Round #842 (Div. 2)

比赛链接

进度: 6 / 6

A - Greatest Convex

You are given an integer \(k\). Find the largest integer \(x\), where \(1 \le x < k\), such that \(x! + (x - 1)!^\dagger\) is a multiple of \(^\ddagger\) \(k\), or determine that no such \(x\) exists

\(^\dagger\) \(y!\) denotes the factorial of \(y\), which is defined recursively as \(y! = y \cdot (y-1)!\) for \(y \geq 1\) with the base case of \(0! = 1\). For example, \(5! = 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 \cdot 0! = 120\)

\(^\ddagger\) If \(a\) and \(b\) are integers, then \(a\) is a multiple of \(b\) if there exists an integer \(c\) such that \(a = b \cdot c\). For example, \(10\) is a multiple of \(5\) but \(9\) is not a multiple of \(6\)

Input

The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows

The only line of each test case contains a single integer \(k\) (\(2 \le k \le 10^9\))

Output

For each test case output a single integer — the largest possible integer \(x\) that satisfies the conditions above

If no such \(x\) exists, output \(-1\)

Example

input

1
2
3
4
5
4
3
6
8
10

output

1
2
3
4
2
5
7
9

Note

In the first test case, \(2! + 1! = 2 + 1 = 3\), which is a multiple of \(3\)

In the third test case, \(7! + 6! = 5040 + 720 = 5760\), which is a multiple of \(8\)

代码参考

Show code

CodeForces_1768Aview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
struct CustomHash {
static constexpr uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static constexpr size_t append(size_t x, size_t y) {
return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1));
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <class Tp, class Up>
size_t operator()(std::pair<Tp, Up> const &p) const {
return append((*this)(p.first), (*this)(p.second));
}
template <typename... Ts>
size_t operator()(std::tuple<Ts...> const &tp) const {
size_t ret = 0;
std::apply(
[&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); },
tp);
return ret;
}
template <
class Tp,
std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()),
typename Tp::iterator>::value &&
std::is_same<decltype(std::declval<Tp>().end()),
typename Tp::iterator>::value> * = nullptr>
size_t operator()(Tp const &tp) const {
size_t ret = 0;
for (auto &&i : tp) ret = append(ret, (*this)(i));
return ret;
}
};
using i64 = int64_t;
#define read_var_(type, name) \
type name; \
std::cin >> name
template <class Tp>
constexpr auto chkmin(Tp &a, Tp b) -> bool {
return b < a ? a = b, true : false;
}
template <class Tp>
constexpr auto chkmax(Tp &a, Tp b) -> bool {
return a < b ? a = b, true : false;
}
template <class Tp>
constexpr auto ispow2(Tp i) -> bool {
return i && (i & -i) == i;
}
#define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>>
namespace tuple_detail_ {
template <std::size_t Begin, class Tuple, std::size_t... Is>
constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is + Begin>(t)...);
}
template <class Tuple, class BinOp, std::size_t... Is>
constexpr auto
apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) {
return std::make_tuple(
std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...);
}
} // namespace tuple_detail_
template <std::size_t Begin, std::size_t Len, class Tuple>
constexpr auto subtuple(Tuple &&t) {
static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) &&
Begin + Len <= TPL_SIZE_(Tuple),
"Out of range");
return tuple_detail_::subtuple_impl_<Begin>(t,
std::make_index_sequence<Len>());
}
template <std::size_t Pos, class Tp, class Tuple>
constexpr auto tuple_push(Tp &&v, Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
std::make_tuple(v),
subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t));
}
template <class Tp, class Tuple>
constexpr auto tuple_push_front(Tp &&v, Tuple &&t) {
return tuple_push<0>(v, t);
}
template <class Tp, class Tuple>
constexpr auto tuple_push_back(Tp &&v, Tuple &&t) {
return tuple_push<TPL_SIZE_(Tuple)>(v, t);
}
template <std::size_t Pos, class Tuple>
constexpr auto tuple_pop(Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t));
}
template <class Tuple>
constexpr auto tuple_pop_front(Tuple &&t) {
return tuple_pop<0>(t);
}
template <class Tuple>
constexpr auto tuple_pop_back(Tuple &&t) {
return tuple_pop<TPL_SIZE_(Tuple) - 1>(t);
}
template <class Tuple, class BinOp>
constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) {
return tuple_detail_::apply2_impl_(
f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>());
}
#define OO_PTEQ_(op) \
template <class Tp, class Up> \
constexpr auto operator op(std::pair<Tp, Up> lhs, \
const std::pair<Tp, Up> &rhs) { \
return std::pair<Tp, Up>{lhs.first op rhs.first, \
lhs.second op rhs.second}; \
} \
template <class... Ts> \
constexpr auto operator op(std::tuple<Ts...> const &lhs, \
std::tuple<Ts...> const &rhs) { \
return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \
} \
template <class Tp, class Up> \
constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \
const std::pair<Tp, Up> &rhs) { \
lhs.first op## = rhs.first; \
lhs.second op## = rhs.second; \
return lhs; \
} \
template <class... Ts> \
constexpr auto operator op##=(std::tuple<Ts...> &lhs, \
const std::tuple<Ts...> &rhs) { \
return lhs = lhs op rhs; \
}
OO_PTEQ_(+)
OO_PTEQ_(-)
OO_PTEQ_(*)
OO_PTEQ_(/)
OO_PTEQ_(%)
OO_PTEQ_(&)
OO_PTEQ_(|)
OO_PTEQ_(^)
OO_PTEQ_(<<)
OO_PTEQ_(>>)
#undef OO_PTEQ_
#undef TPL_SIZE_
template <class Tp, class Up>
std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) {
return is >> p.first >> p.second;
}
template <class Tp, class Up>
std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) {
return os << p.first << ' ' << p.second;
}
template <typename... Ts>
std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) {
std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p);
return is;
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) {
std::apply(
[&](Ts const &...targs) {
std::size_t n{0};
((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...);
},
p);
return os;
}
template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os;
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' ';
os << x.back();
return os;
}
using namespace std;
auto solve([[maybe_unused]] int t_ = 0) -> void {
read_var_(i64, k);
cout << k - 1 << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int i_ = 0;
int t_ = 0;
std::cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

B - Quick Sort

You are given a permutation\(^\dagger\) \(p\) of length \(n\) and a positive integer \(k \le n\)

In one operation, you:

  • Choose \(k\) distinct elements \(p_{i_1}, p_{i_2}, \ldots, p_{i_k}\)
  • Remove them and then add them sorted in increasing order to the end of the permutation

For example, if \(p = [2,5,1,3,4]\) and \(k = 2\) and you choose \(5\) and \(3\) as the elements for the operation, then \([2, {\color{red}5}, 1, {\color{red}3}, 4] \rightarrow [2, 1, 4, {\color{red}3},{\color{red}5}]\)

Find the minimum number of operations needed to sort the permutation in increasing order. It can be proven that it is always possible to do so

\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array)

Input

The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows

The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le n\))

The second line of each test case contains \(n\) integers \(p_1,p_2,\ldots, p_n\) (\(1 \le p_i \le n\)). It is guaranteed that \(p\) is a permutation

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\)

Output

For each test case output a single integer — the minimum number of operations needed to sort the permutation. It can be proven that it is always possible to do so

Example

input

1
2
3
4
5
6
7
8
9
4
3 2
1 2 3
3 1
3 1 2
4 2
1 3 2 4
4 2
2 3 1 4

output

1
2
3
4
0
1
1
2

Note

In the first test case, the permutation is already sorted

In the second test case, you can choose element \(3\), and the permutation will become sorted as follows: \([{\color{red}3}, 1, 2] \rightarrow [1, 2, {\color{red}3}]\)

In the third test case, you can choose elements \(3\) and \(4\), and the permutation will become sorted as follows: \([1, {\color{red}3}, 2, {\color{red}4}] \rightarrow [1, 2, {\color{red}3},{\color{red}4}]\)

In the fourth test case, it can be shown that it is impossible to sort the permutation in \(1\) operation. However, if you choose elements \(2\) and \(1\) in the first operation, and choose elements \(3\) and \(4\) in the second operation, the permutation will become sorted as follows: \([{\color{red}2}, 3, {\color{red}1}, 4] \rightarrow [{\color{blue}3}, {\color{blue}4}, {\color{red}1}, {\color{red}2}] \rightarrow [1,2, {\color{blue}3}, {\color{blue}4}]\)

代码参考

Show code

CodeForces_1768Bview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
template <class Tp>
using vc = std::vector<Tp>;
struct CustomHash {
static constexpr uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static constexpr size_t append(size_t x, size_t y) {
return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1));
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <class Tp, class Up>
size_t operator()(std::pair<Tp, Up> const &p) const {
return append((*this)(p.first), (*this)(p.second));
}
template <typename... Ts>
size_t operator()(std::tuple<Ts...> const &tp) const {
size_t ret = 0;
std::apply(
[&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); },
tp);
return ret;
}
template <
class Tp,
std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()),
typename Tp::iterator>::value &&
std::is_same<decltype(std::declval<Tp>().end()),
typename Tp::iterator>::value> * = nullptr>
size_t operator()(Tp const &tp) const {
size_t ret = 0;
for (auto &&i : tp) ret = append(ret, (*this)(i));
return ret;
}
};
#define foreach_ref_(i, container) for (auto &i : (container))
#define read_var_(type, name) \
type name; \
std::cin >> name
#define read_container_(type, name, size) \
type name(size); \
foreach_ref_(i, name) std::cin >> i
template <class Tp>
constexpr auto chkmin(Tp &a, Tp b) -> bool {
return b < a ? a = b, true : false;
}
template <class Tp>
constexpr auto chkmax(Tp &a, Tp b) -> bool {
return a < b ? a = b, true : false;
}
template <class Tp>
constexpr auto ispow2(Tp i) -> bool {
return i && (i & -i) == i;
}
#define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>>
namespace tuple_detail_ {
template <std::size_t Begin, class Tuple, std::size_t... Is>
constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is + Begin>(t)...);
}
template <class Tuple, class BinOp, std::size_t... Is>
constexpr auto
apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) {
return std::make_tuple(
std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...);
}
} // namespace tuple_detail_
template <std::size_t Begin, std::size_t Len, class Tuple>
constexpr auto subtuple(Tuple &&t) {
static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) &&
Begin + Len <= TPL_SIZE_(Tuple),
"Out of range");
return tuple_detail_::subtuple_impl_<Begin>(t,
std::make_index_sequence<Len>());
}
template <std::size_t Pos, class Tp, class Tuple>
constexpr auto tuple_push(Tp &&v, Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
std::make_tuple(v),
subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t));
}
template <class Tp, class Tuple>
constexpr auto tuple_push_front(Tp &&v, Tuple &&t) {
return tuple_push<0>(v, t);
}
template <class Tp, class Tuple>
constexpr auto tuple_push_back(Tp &&v, Tuple &&t) {
return tuple_push<TPL_SIZE_(Tuple)>(v, t);
}
template <std::size_t Pos, class Tuple>
constexpr auto tuple_pop(Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t));
}
template <class Tuple>
constexpr auto tuple_pop_front(Tuple &&t) {
return tuple_pop<0>(t);
}
template <class Tuple>
constexpr auto tuple_pop_back(Tuple &&t) {
return tuple_pop<TPL_SIZE_(Tuple) - 1>(t);
}
template <class Tuple, class BinOp>
constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) {
return tuple_detail_::apply2_impl_(
f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>());
}
#define OO_PTEQ_(op) \
template <class Tp, class Up> \
constexpr auto operator op(std::pair<Tp, Up> lhs, \
const std::pair<Tp, Up> &rhs) { \
return std::pair<Tp, Up>{lhs.first op rhs.first, \
lhs.second op rhs.second}; \
} \
template <class... Ts> \
constexpr auto operator op(std::tuple<Ts...> const &lhs, \
std::tuple<Ts...> const &rhs) { \
return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \
} \
template <class Tp, class Up> \
constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \
const std::pair<Tp, Up> &rhs) { \
lhs.first op## = rhs.first; \
lhs.second op## = rhs.second; \
return lhs; \
} \
template <class... Ts> \
constexpr auto operator op##=(std::tuple<Ts...> &lhs, \
const std::tuple<Ts...> &rhs) { \
return lhs = lhs op rhs; \
}
OO_PTEQ_(+)
OO_PTEQ_(-)
OO_PTEQ_(*)
OO_PTEQ_(/)
OO_PTEQ_(%)
OO_PTEQ_(&)
OO_PTEQ_(|)
OO_PTEQ_(^)
OO_PTEQ_(<<)
OO_PTEQ_(>>)
#undef OO_PTEQ_
#undef TPL_SIZE_
template <class Tp, class Up>
std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) {
return is >> p.first >> p.second;
}
template <class Tp, class Up>
std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) {
return os << p.first << ' ' << p.second;
}
template <typename... Ts>
std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) {
std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p);
return is;
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) {
std::apply(
[&](Ts const &...targs) {
std::size_t n{0};
((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...);
},
p);
return os;
}
template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os;
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' ';
os << x.back();
return os;
}
using namespace std;
auto solve([[maybe_unused]] int t_ = 0) -> void {
read_var_(int, n);
read_var_(int, k);
read_container_(vc<int>, p, n);
int last = 1;
int cnt = 0;
for (auto now : p) {
if (now > last) ++cnt;
else if (now == last) ++last;
}
cout << (cnt + k - 1) / k << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int i_ = 0;
int t_ = 0;
std::cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

C - Elemental Decompress

You are given an array \(a\) of \(n\) integers

Find two permutations\(^\dagger\) \(p\) and \(q\) of length \(n\) such that \(\max(p_i,q_i)=a_i\) for all \(1 \leq i \leq n\) or report that such \(p\) and \(q\) do not exist

\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array)

Input

The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows

The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\))

The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \leq a_i \leq n\)) — the array \(a\)

It is guaranteed that the total sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\)

Output

For each test case, if there do not exist \(p\) and \(q\) that satisfy the conditions, output "NO" (without quotes)

Otherwise, output "YES" (without quotes) and then output \(2\) lines. The first line should contain \(n\) integers \(p_1,p_2,\ldots,p_n\) and the second line should contain \(n\) integers \(q_1,q_2,\ldots,q_n\)

If there are multiple solutions, you may output any of them

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response)

Example

input

1
2
3
4
5
6
7
3
1
1
5
5 3 4 2 5
2
1 1

output

1
2
3
4
5
6
7
YES
1
1
YES
1 3 4 2 5
5 2 3 1 4
NO

Note

In the first test case, \(p=q=[1]\). It is correct since \(a_1 = max(p_1,q_1) = 1\)

In the second test case, \(p=[1,3,4,2,5]\) and \(q=[5,2,3,1,4]\). It is correct since:

  • \(a_1 = \max(p_1, q_1) = \max(1, 5) = 5\),
  • \(a_2 = \max(p_2, q_2) = \max(3, 2) = 3\),
  • \(a_3 = \max(p_3, q_3) = \max(4, 3) = 4\),
  • \(a_4 = \max(p_4, q_4) = \max(2, 1) = 2\),
  • \(a_5 = \max(p_5, q_5) = \max(5, 4) = 5\)

In the third test case, one can show that no such \(p\) and \(q\) exist

代码参考

Show code

CodeForces_1768Cview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
template <class Tp>
using vc = std::vector<Tp>;
template <class Tp>
using vvc = std::vector<std::vector<Tp>>;
struct CustomHash {
static constexpr uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static constexpr size_t append(size_t x, size_t y) {
return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1));
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <class Tp, class Up>
size_t operator()(std::pair<Tp, Up> const &p) const {
return append((*this)(p.first), (*this)(p.second));
}
template <typename... Ts>
size_t operator()(std::tuple<Ts...> const &tp) const {
size_t ret = 0;
std::apply(
[&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); },
tp);
return ret;
}
template <
class Tp,
std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()),
typename Tp::iterator>::value &&
std::is_same<decltype(std::declval<Tp>().end()),
typename Tp::iterator>::value> * = nullptr>
size_t operator()(Tp const &tp) const {
size_t ret = 0;
for (auto &&i : tp) ret = append(ret, (*this)(i));
return ret;
}
};
#define for_(i, l, r, vars...) \
for (std::make_signed_t<decltype(l + r)> i = (l), i##end = (r), ##vars; \
i <= i##end; \
++i)
#define rfor_(i, r, l, vars...) \
for (std::make_signed_t<decltype(r - l)> i = (r), i##end = (l), ##vars; \
i >= i##end; \
--i)
#define foreach_ref_(i, container) for (auto &i : (container))
#define run_exec_(expressions, post_process) \
{ \
expressions; \
post_process; \
}
#define run_return_void_(expressions) run_exec_(expressions, return)
#define read_var_(type, name) \
type name; \
std::cin >> name
#define read_container_(type, name, size) \
type name(size); \
foreach_ref_(i, name) std::cin >> i
template <class Tp>
constexpr auto chkmin(Tp &a, Tp b) -> bool {
return b < a ? a = b, true : false;
}
template <class Tp>
constexpr auto chkmax(Tp &a, Tp b) -> bool {
return a < b ? a = b, true : false;
}
template <class Tp>
constexpr auto ispow2(Tp i) -> bool {
return i && (i & -i) == i;
}
#define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>>
namespace tuple_detail_ {
template <std::size_t Begin, class Tuple, std::size_t... Is>
constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is + Begin>(t)...);
}
template <class Tuple, class BinOp, std::size_t... Is>
constexpr auto
apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) {
return std::make_tuple(
std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...);
}
} // namespace tuple_detail_
template <std::size_t Begin, std::size_t Len, class Tuple>
constexpr auto subtuple(Tuple &&t) {
static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) &&
Begin + Len <= TPL_SIZE_(Tuple),
"Out of range");
return tuple_detail_::subtuple_impl_<Begin>(t,
std::make_index_sequence<Len>());
}
template <std::size_t Pos, class Tp, class Tuple>
constexpr auto tuple_push(Tp &&v, Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
std::make_tuple(v),
subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t));
}
template <class Tp, class Tuple>
constexpr auto tuple_push_front(Tp &&v, Tuple &&t) {
return tuple_push<0>(v, t);
}
template <class Tp, class Tuple>
constexpr auto tuple_push_back(Tp &&v, Tuple &&t) {
return tuple_push<TPL_SIZE_(Tuple)>(v, t);
}
template <std::size_t Pos, class Tuple>
constexpr auto tuple_pop(Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t));
}
template <class Tuple>
constexpr auto tuple_pop_front(Tuple &&t) {
return tuple_pop<0>(t);
}
template <class Tuple>
constexpr auto tuple_pop_back(Tuple &&t) {
return tuple_pop<TPL_SIZE_(Tuple) - 1>(t);
}
template <class Tuple, class BinOp>
constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) {
return tuple_detail_::apply2_impl_(
f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>());
}
#define OO_PTEQ_(op) \
template <class Tp, class Up> \
constexpr auto operator op(std::pair<Tp, Up> lhs, \
const std::pair<Tp, Up> &rhs) { \
return std::pair<Tp, Up>{lhs.first op rhs.first, \
lhs.second op rhs.second}; \
} \
template <class... Ts> \
constexpr auto operator op(std::tuple<Ts...> const &lhs, \
std::tuple<Ts...> const &rhs) { \
return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \
} \
template <class Tp, class Up> \
constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \
const std::pair<Tp, Up> &rhs) { \
lhs.first op## = rhs.first; \
lhs.second op## = rhs.second; \
return lhs; \
} \
template <class... Ts> \
constexpr auto operator op##=(std::tuple<Ts...> &lhs, \
const std::tuple<Ts...> &rhs) { \
return lhs = lhs op rhs; \
}
OO_PTEQ_(+)
OO_PTEQ_(-)
OO_PTEQ_(*)
OO_PTEQ_(/)
OO_PTEQ_(%)
OO_PTEQ_(&)
OO_PTEQ_(|)
OO_PTEQ_(^)
OO_PTEQ_(<<)
OO_PTEQ_(>>)
#undef OO_PTEQ_
#undef TPL_SIZE_
template <class Tp, class Up>
std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) {
return is >> p.first >> p.second;
}
template <class Tp, class Up>
std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) {
return os << p.first << ' ' << p.second;
}
template <typename... Ts>
std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) {
std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p);
return is;
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) {
std::apply(
[&](Ts const &...targs) {
std::size_t n{0};
((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...);
},
p);
return os;
}
template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os;
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' ';
os << x.back();
return os;
}
const std::string RES_YN[2] = {"NO", "YES"};
using namespace std;
vc<int> _(1);
struct Node {
vc<int>::iterator pos;
int lim;
Node(vc<int>::iterator pos, int lim): pos(pos), lim(lim) {}
bool operator<(Node const &rhs) const { return lim < rhs.lim; }
};
auto solve([[maybe_unused]] int t_ = 0) -> void {
read_var_(int, n);
read_container_(vc<int>, a, n);
vc<int> p(n), q(n);
vvc<int> idx(n + 1);
for (auto &i : idx) i.reserve(2);
for_(i, 0, n - 1) {
if (idx[a[i]].size() >= 2) run_return_void_(cout << RES_YN[0] << '\n');
idx[a[i]].push_back(i);
}
vvc<int> cat(3);
rfor_(i, n, 1) cat[idx[i].size()].push_back(i);
for (auto v : cat[2]) p[idx[v][0]] = q[idx[v][1]] = v;
for (auto v : cat[1]) p[idx[v][0]] = q[idx[v][0]] = v;
multiset<Node> rest;
for_(i, 0, n - 1)
if (!p[i]) rest.emplace(p.begin() + i, q[i]);
else if (!q[i]) rest.emplace(q.begin() + i, p[i]);
for (auto v : cat[0])
for_(__, 0, 1) {
auto it = rest.lower_bound(Node{_.begin(), v});
if (it == rest.end() || it->lim < v)
run_return_void_(cout << RES_YN[0] << '\n');
*(it->pos) = v;
rest.erase(it);
}
cout << RES_YN[1] << '\n' << p << '\n' << q << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int i_ = 0;
int t_ = 0;
std::cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

D - Lucky Permutation

You are given a permutation\(^\dagger\) \(p\) of length \(n\)

In one operation, you can choose two indices \(1 \le i < j \le n\) and swap \(p_i\) with \(p_j\)

Find the minimum number of operations needed to have exactly one inversion\(^\ddagger\) in the permutation

\(^\dagger\) A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array)

\(^\ddagger\) The number of inversions of a permutation \(p\) is the number of pairs of indices \((i, j)\) such that \(1 \le i < j \le n\) and \(p_i > p_j\)

Input

The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows

The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\))

The second line of each test case contains \(n\) integers \(p_1,p_2,\ldots, p_n\) (\(1 \le p_i \le n\)). It is guaranteed that \(p\) is a permutation

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\)

Output

For each test case output a single integer — the minimum number of operations needed to have exactly one inversion in the permutation. It can be proven that an answer always exists

Example

input

1
2
3
4
5
6
7
8
9
4
2
2 1
2
1 2
4
3 4 1 2
4
2 4 3 1

output

1
2
3
4
0
1
3
1

Note

In the first test case, the permutation already satisfies the condition

In the second test case, you can perform the operation with \((i,j)=(1,2)\), after that the permutation will be \([2,1]\) which has exactly one inversion

In the third test case, it is not possible to satisfy the condition with less than \(3\) operations. However, if we perform \(3\) operations with \((i,j)\) being \((1,3)\),\((2,4)\), and \((3,4)\) in that order, the final permutation will be \([1, 2, 4, 3]\) which has exactly one inversion

In the fourth test case, you can perform the operation with \((i,j)=(2,4)\), after that the permutation will be \([2,1,3,4]\) which has exactly one inversion

代码参考

Show code

CodeForces_1768Dview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
template <class Tp>
using vc = std::vector<Tp>;
struct CustomHash {
static constexpr uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static constexpr size_t append(size_t x, size_t y) {
return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1));
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <class Tp, class Up>
size_t operator()(std::pair<Tp, Up> const &p) const {
return append((*this)(p.first), (*this)(p.second));
}
template <typename... Ts>
size_t operator()(std::tuple<Ts...> const &tp) const {
size_t ret = 0;
std::apply(
[&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); },
tp);
return ret;
}
template <
class Tp,
std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()),
typename Tp::iterator>::value &&
std::is_same<decltype(std::declval<Tp>().end()),
typename Tp::iterator>::value> * = nullptr>
size_t operator()(Tp const &tp) const {
size_t ret = 0;
for (auto &&i : tp) ret = append(ret, (*this)(i));
return ret;
}
};
#define for_(i, l, r, vars...) \
for (std::make_signed_t<decltype(l + r)> i = (l), i##end = (r), ##vars; \
i <= i##end; \
++i)
#define all_(a) (a).begin(), (a).end()
#define run_exec_(expressions, post_process) \
{ \
expressions; \
post_process; \
}
#define run_return_void_(expressions) run_exec_(expressions, return)
#define run_break_(expressions) run_exec_(expressions, break)
#define read_var_(type, name) \
type name; \
std::cin >> name
template <class Tp>
constexpr auto chkmin(Tp &a, Tp b) -> bool {
return b < a ? a = b, true : false;
}
template <class Tp>
constexpr auto chkmax(Tp &a, Tp b) -> bool {
return a < b ? a = b, true : false;
}
template <class Tp>
constexpr auto ispow2(Tp i) -> bool {
return i && (i & -i) == i;
}
#define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>>
namespace tuple_detail_ {
template <std::size_t Begin, class Tuple, std::size_t... Is>
constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is + Begin>(t)...);
}
template <class Tuple, class BinOp, std::size_t... Is>
constexpr auto
apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) {
return std::make_tuple(
std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...);
}
} // namespace tuple_detail_
template <std::size_t Begin, std::size_t Len, class Tuple>
constexpr auto subtuple(Tuple &&t) {
static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) &&
Begin + Len <= TPL_SIZE_(Tuple),
"Out of range");
return tuple_detail_::subtuple_impl_<Begin>(t,
std::make_index_sequence<Len>());
}
template <std::size_t Pos, class Tp, class Tuple>
constexpr auto tuple_push(Tp &&v, Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
std::make_tuple(v),
subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t));
}
template <class Tp, class Tuple>
constexpr auto tuple_push_front(Tp &&v, Tuple &&t) {
return tuple_push<0>(v, t);
}
template <class Tp, class Tuple>
constexpr auto tuple_push_back(Tp &&v, Tuple &&t) {
return tuple_push<TPL_SIZE_(Tuple)>(v, t);
}
template <std::size_t Pos, class Tuple>
constexpr auto tuple_pop(Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t));
}
template <class Tuple>
constexpr auto tuple_pop_front(Tuple &&t) {
return tuple_pop<0>(t);
}
template <class Tuple>
constexpr auto tuple_pop_back(Tuple &&t) {
return tuple_pop<TPL_SIZE_(Tuple) - 1>(t);
}
template <class Tuple, class BinOp>
constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) {
return tuple_detail_::apply2_impl_(
f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>());
}
#define OO_PTEQ_(op) \
template <class Tp, class Up> \
constexpr auto operator op(std::pair<Tp, Up> lhs, \
const std::pair<Tp, Up> &rhs) { \
return std::pair<Tp, Up>{lhs.first op rhs.first, \
lhs.second op rhs.second}; \
} \
template <class... Ts> \
constexpr auto operator op(std::tuple<Ts...> const &lhs, \
std::tuple<Ts...> const &rhs) { \
return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \
} \
template <class Tp, class Up> \
constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \
const std::pair<Tp, Up> &rhs) { \
lhs.first op## = rhs.first; \
lhs.second op## = rhs.second; \
return lhs; \
} \
template <class... Ts> \
constexpr auto operator op##=(std::tuple<Ts...> &lhs, \
const std::tuple<Ts...> &rhs) { \
return lhs = lhs op rhs; \
}
OO_PTEQ_(+)
OO_PTEQ_(-)
OO_PTEQ_(*)
OO_PTEQ_(/)
OO_PTEQ_(%)
OO_PTEQ_(&)
OO_PTEQ_(|)
OO_PTEQ_(^)
OO_PTEQ_(<<)
OO_PTEQ_(>>)
#undef OO_PTEQ_
#undef TPL_SIZE_
template <class Tp, class Up>
std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) {
return is >> p.first >> p.second;
}
template <class Tp, class Up>
std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) {
return os << p.first << ' ' << p.second;
}
template <typename... Ts>
std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) {
std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p);
return is;
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) {
std::apply(
[&](Ts const &...targs) {
std::size_t n{0};
((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...);
},
p);
return os;
}
template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os;
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' ';
os << x.back();
return os;
}
using namespace std;
vc<int> _(1);
struct Node {
vc<int>::iterator pos;
int lim;
Node(vc<int>::iterator pos, int lim): pos(pos), lim(lim) {}
bool operator<(Node const &rhs) const { return lim < rhs.lim; }
};
auto solve([[maybe_unused]] int t_ = 0) -> void {
read_var_(int, n);
vc<int> a(n + 1);
for_(i, 1, n) cin >> a[i];
if (is_sorted(all_(a))) run_return_void_(cout << "1\n");
int ans = 0;
vc<bool> vis(n + 1);
bool f = 0;
for_(i, 1, n) {
if (vis[i]) continue;
int now = i;
vc<int> p;
while (!vis[now]) {
vis[now] = 1;
p.push_back(now);
now = a[now];
}
if (p.size() < 2) continue;
ans += p.size() - 1;
sort(all_(p));
if (!f)
for_(j, 0, p.size() - 2)
if (abs(p[j] - p[j + 1]) == 1) run_break_(f = 1);
}
cout << ans + (f ? -1 : 1) << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int i_ = 0;
int t_ = 0;
std::cin >> t_;
for (i_ = 0; i_ < t_; ++i_) solve(i_);
return 0;
}

E - Partial Sorting

Consider a permutation\(^\dagger\) \(p\) of length \(3n\). Each time you can do one of the following operations:

  • Sort the first \(2n\) elements in increasing order
  • Sort the last \(2n\) elements in increasing order

We can show that every permutation can be made sorted in increasing order using only these operations. Let's call \(f(p)\) the minimum number of these operations needed to make the permutation \(p\) sorted in increasing order

Given \(n\), find the sum of \(f(p)\) over all \((3n)!\) permutations \(p\) of size \(3n\)

Since the answer could be very large, output it modulo a prime \(M\)

\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array)

Input

The only line of input contains two numbers \(n\) and \(M\) (\(1 \leq n \leq 10^6\), \(10^8 \leq M \leq 10^9\)). It is guaranteed that \(M\) is a prime number

Output

Output the answer modulo \(M\)

Examples

input

1
1 100009067

output

1
9

input

1
2 100000357

output

1
1689

input

1
69 999900997

output

1
193862705

Note

In the first test case, all the permutations are:

  • \([1, 2, 3]\), which requires \(0\) operations;
  • \([1, 3, 2]\), which requires \(1\) operation;
  • \([2, 1, 3]\), which requires \(1\) operation;
  • \([2, 3, 1]\), which requires \(2\) operations;
  • \([3, 1, 2]\), which requires \(2\) operations;
  • \([3, 2, 1]\), which requires \(3\) operations

Therefore, the answer is \(0+1+1+2+2+3=9\)

代码参考

Show code

CodeForces_1768Eview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
template <class Tp>
using vc = std::vector<Tp>;
struct CustomHash {
static constexpr uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static constexpr size_t append(size_t x, size_t y) {
return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1));
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <class Tp, class Up>
size_t operator()(std::pair<Tp, Up> const &p) const {
return append((*this)(p.first), (*this)(p.second));
}
template <typename... Ts>
size_t operator()(std::tuple<Ts...> const &tp) const {
size_t ret = 0;
std::apply(
[&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); },
tp);
return ret;
}
template <
class Tp,
std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()),
typename Tp::iterator>::value &&
std::is_same<decltype(std::declval<Tp>().end()),
typename Tp::iterator>::value> * = nullptr>
size_t operator()(Tp const &tp) const {
size_t ret = 0;
for (auto &&i : tp) ret = append(ret, (*this)(i));
return ret;
}
};
using u32 = uint32_t;
#define for_(i, l, r, vars...) \
for (std::make_signed_t<decltype(l + r)> i = (l), i##end = (r), ##vars; \
i <= i##end; \
++i)
#define rfor_(i, r, l, vars...) \
for (std::make_signed_t<decltype(r - l)> i = (r), i##end = (l), ##vars; \
i >= i##end; \
--i)
template <class Tp>
constexpr auto chkmin(Tp &a, Tp b) -> bool {
return b < a ? a = b, true : false;
}
template <class Tp>
constexpr auto chkmax(Tp &a, Tp b) -> bool {
return a < b ? a = b, true : false;
}
template <class Tp>
constexpr auto ispow2(Tp i) -> bool {
return i && (i & -i) == i;
}
#define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>>
namespace tuple_detail_ {
template <std::size_t Begin, class Tuple, std::size_t... Is>
constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is + Begin>(t)...);
}
template <class Tuple, class BinOp, std::size_t... Is>
constexpr auto
apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) {
return std::make_tuple(
std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...);
}
} // namespace tuple_detail_
template <std::size_t Begin, std::size_t Len, class Tuple>
constexpr auto subtuple(Tuple &&t) {
static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) &&
Begin + Len <= TPL_SIZE_(Tuple),
"Out of range");
return tuple_detail_::subtuple_impl_<Begin>(t,
std::make_index_sequence<Len>());
}
template <std::size_t Pos, class Tp, class Tuple>
constexpr auto tuple_push(Tp &&v, Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
std::make_tuple(v),
subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t));
}
template <class Tp, class Tuple>
constexpr auto tuple_push_front(Tp &&v, Tuple &&t) {
return tuple_push<0>(v, t);
}
template <class Tp, class Tuple>
constexpr auto tuple_push_back(Tp &&v, Tuple &&t) {
return tuple_push<TPL_SIZE_(Tuple)>(v, t);
}
template <std::size_t Pos, class Tuple>
constexpr auto tuple_pop(Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t));
}
template <class Tuple>
constexpr auto tuple_pop_front(Tuple &&t) {
return tuple_pop<0>(t);
}
template <class Tuple>
constexpr auto tuple_pop_back(Tuple &&t) {
return tuple_pop<TPL_SIZE_(Tuple) - 1>(t);
}
template <class Tuple, class BinOp>
constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) {
return tuple_detail_::apply2_impl_(
f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>());
}
#define OO_PTEQ_(op) \
template <class Tp, class Up> \
constexpr auto operator op(std::pair<Tp, Up> lhs, \
const std::pair<Tp, Up> &rhs) { \
return std::pair<Tp, Up>{lhs.first op rhs.first, \
lhs.second op rhs.second}; \
} \
template <class... Ts> \
constexpr auto operator op(std::tuple<Ts...> const &lhs, \
std::tuple<Ts...> const &rhs) { \
return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \
} \
template <class Tp, class Up> \
constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \
const std::pair<Tp, Up> &rhs) { \
lhs.first op## = rhs.first; \
lhs.second op## = rhs.second; \
return lhs; \
} \
template <class... Ts> \
constexpr auto operator op##=(std::tuple<Ts...> &lhs, \
const std::tuple<Ts...> &rhs) { \
return lhs = lhs op rhs; \
}
OO_PTEQ_(+)
OO_PTEQ_(-)
OO_PTEQ_(*)
OO_PTEQ_(/)
OO_PTEQ_(%)
OO_PTEQ_(&)
OO_PTEQ_(|)
OO_PTEQ_(^)
OO_PTEQ_(<<)
OO_PTEQ_(>>)
#undef OO_PTEQ_
#undef TPL_SIZE_
template <class Tp, class Up>
std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) {
return is >> p.first >> p.second;
}
template <class Tp, class Up>
std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) {
return os << p.first << ' ' << p.second;
}
template <typename... Ts>
std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) {
std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p);
return is;
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) {
std::apply(
[&](Ts const &...targs) {
std::size_t n{0};
((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...);
},
p);
return os;
}
template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os;
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' ';
os << x.back();
return os;
}
using namespace std;
namespace MODINT {
constexpr int64_t safe_mod(int64_t x, int64_t m) {
return (x %= m) < 0 ? x + m : x;
}
constexpr std::pair<int64_t, int64_t> invgcd(int64_t a, int64_t b) {
if ((a = safe_mod(a, b)) == 0) return {b, 0};
int64_t s = b, m0 = 0;
for (int64_t q = 0, _ = 0, m1 = 1; a;) {
_ = s - a * (q = s / a);
s = a;
a = _;
_ = m0 - m1 * q;
m0 = m1;
m1 = _;
}
return {s, m0 + (m0 < 0 ? b / s : 0)};
}
struct Barrett_ {
uint32_t m_;
uint64_t im;
constexpr explicit Barrett_(uint32_t m = 998244353)
: m_(m), im((uint64_t)(-1) / m + 1) {}
constexpr uint32_t umod() const { return m_; }
constexpr uint32_t mul(uint32_t a, uint32_t b) const {
uint64_t z = a;
z *= b;
uint64_t x = (uint64_t)(((__uint128_t)z * im) >> 64);
uint32_t v = (uint32_t)(z - x * m_);
return v + (m_ <= v ? m_ : 0);
}
} bt_;
template <ptrdiff_t ID = -1>
class DyMint {
using self = DyMint<ID>;

protected:
uint32_t v_;

public:
static constexpr uint32_t mod() { return bt_.umod(); }
static constexpr void set_mod(uint32_t m) {
assert(1 <= m);
bt_ = Barrett_(m);
}
static constexpr self raw(uint32_t v) {
self x;
x.v_ = v;
return x;
}
constexpr DyMint(): v_(0) {}
template <class T,
std::enable_if_t<std::is_integral<T>::value &&
std::is_signed<T>::value> * = nullptr>
constexpr DyMint(T v): DyMint() {
int64_t x = (int64_t)(v % (int64_t)mod());
v_ = (uint32_t)(x + (x < 0 ? mod() : 0));
}
template <class T,
std::enable_if_t<std::is_integral<T>::value &&
std::is_unsigned<T>::value> * = nullptr>
constexpr DyMint(T v): v_((uint32_t)(v % mod())) {}
friend std::istream &operator>>(std::istream &is, self &x) {
int64_t xx;
is >> xx;
xx %= mod();
x.v_ = (uint32_t)(xx + (xx < 0 ? mod() : 0));
return is;
}
friend std::ostream &operator<<(std::ostream &os, const self &x) {
return os << x.v_;
}
constexpr const uint32_t &val() const { return v_; }
constexpr explicit operator uint32_t() const { return val(); }
constexpr uint32_t &data() { return v_; }
constexpr self &operator++() {
if (++v_ == mod()) v_ = 0;
return *this;
}
constexpr self &operator--() {
if (!v_) v_ = mod();
--v_;
return *this;
}
constexpr self operator++(int) {
self result = *this;
++*this;
return result;
}
constexpr self operator--(int) {
self result = *this;
--*this;
return result;
}
constexpr self &operator+=(const self &rhs) {
v_ += rhs.v_;
if (v_ >= mod()) v_ -= mod();
return *this;
}
constexpr self &operator-=(const self &rhs) {
v_ -= rhs.v_;
if (v_ >= mod()) v_ += mod();
return *this;
}
constexpr self &operator*=(const self &rhs) {
v_ = bt_.mul(v_, rhs.v_);
return *this;
}
constexpr self &operator/=(const self &rhs) {
return *this = *this * inverse(rhs);
}
constexpr self operator+() const { return *this; }
constexpr self operator-() const { return self() - *this; }
constexpr friend self pow(self x, uint64_t y) {
self res(1);
for (; y; y >>= 1, x *= x)
if (y & 1) res *= x;
return res;
}
constexpr friend self inverse(const self &x) {
auto &&_ = invgcd(x.v_, self::mod());
if (_.first != 1) throw std::runtime_error("Inverse not exist");
return _.second;
}
constexpr friend self operator+(self lhs, const self &rhs) {
return lhs += rhs;
}
constexpr friend self operator-(self lhs, const self &rhs) {
return lhs -= rhs;
}
constexpr friend self operator*(self lhs, const self &rhs) {
return lhs *= rhs;
}
constexpr friend self operator/(self lhs, const self &rhs) {
return lhs /= rhs;
}
constexpr friend bool operator==(const self &lhs, const self &rhs) {
return lhs.v_ == rhs.v_;
}
constexpr friend bool operator!=(const self &lhs, const self &rhs) {
return lhs.v_ != rhs.v_;
}
};
} // namespace MODINT
using MODINT::DyMint;
using dmint = DyMint<>;
auto solve([[maybe_unused]] int t_ = 0) -> void {
u32 n, mod;
cin >> n >> mod;
dmint::set_mod(mod);
vc<dmint> fact(n * 3 + 1), inv_fact(n * 3 + 1);
fact[0] = 1;
for_(i, 1, n * 3) fact[i] = fact[i - 1] * i;
inv_fact[n * 3] = 1 / fact[n * 3];
rfor_(i, n * 3, 1) inv_fact[i - 1] = inv_fact[i] * i;
dmint ans = 0;
ans += fact[n * 3] * 3;
ans -= fact[n * 2] * inv_fact[n] * fact[n * 2] * 2;
for_(i, 0, n) {
auto _ = fact[n] * inv_fact[i] * inv_fact[n - i] * fact[n];
ans += _ * _ * fact[n * 2 - i] * inv_fact[n - i];
}
ans -= fact[n * 2] * 2 - fact[n];
ans -= 1;
cout << ans.data();
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int i_ = 0;
solve(i_);
return 0;
}

F - Wonderful Jump

You are given an array of positive integers \(a_1,a_2,\ldots,a_n\) of length \(n\)

In one operation you can jump from index \(i\) to index \(j\) (\(1 \le i \le j \le n\)) by paying \(\min(a_i, a_{i + 1}, \ldots, a_j) \cdot (j - i)^2\) eris

For all \(k\) from \(1\) to \(n\), find the minimum number of eris needed to get from index \(1\) to index \(k\)

Input

The first line contains a single integer \(n\) (\(2 \le n \le 4 \cdot 10^5\))

The second line contains \(n\) integers \(a_1,a_2,\ldots a_n\) (\(1 \le a_i \le n\))

Output

Output \(n\) integers — the \(k\)-th integer is the minimum number of eris needed to reach index \(k\) if you start from index \(1\)

Examples

input

1
2
3
2 1 3

output

1
0 1 2

input

1
2
6
1 4 1 6 3 2

output

1
0 1 2 3 6 8

input

1
2
2
1 2

output

1
0 1

input

1
2
4
1 4 4 4

output

1
0 1 4 8

Note

In the first example:

  • From \(1\) to \(1\): the cost is \(0\),
  • From \(1\) to \(2\): \(1 \rightarrow 2\) — the cost is \(\min(2, 1) \cdot (2 - 1) ^ 2=1\),
  • From \(1\) to \(3\): \(1 \rightarrow 2 \rightarrow 3\) — the cost is \(\min(2, 1) \cdot (2 - 1) ^ 2 + \min(1, 3) \cdot (3 - 2) ^ 2 = 1 + 1 = 2\)

In the fourth example from \(1\) to \(4\): \(1 \rightarrow 3 \rightarrow 4\) — the cost is \(\min(1, 4, 4) \cdot (3 - 1) ^ 2 + \min(4, 4) \cdot (4 - 3) ^ 2 = 4 + 4 = 8\)

代码参考

Show code

CodeForces_1768Fview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* @Author: Tifa
* @Description: From <https://github.com/Tiphereth-A/CP-archives>
* !!! ATTENEION: All the context below is licensed under a
* GNU Affero General Public License, Version 3.
* See <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
#include <bits/stdc++.h>
template <class Tp>
using vc = std::vector<Tp>;
struct CustomHash {
static constexpr uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
static constexpr size_t append(size_t x, size_t y) {
return x ^ (y >> 1) ^ ((y & 1) << (sizeof(size_t) * 8 - 1));
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <class Tp, class Up>
size_t operator()(std::pair<Tp, Up> const &p) const {
return append((*this)(p.first), (*this)(p.second));
}
template <typename... Ts>
size_t operator()(std::tuple<Ts...> const &tp) const {
size_t ret = 0;
std::apply(
[&](Ts const &...targs) { ((ret = append(ret, (*this)(targs))), ...); },
tp);
return ret;
}
template <
class Tp,
std::enable_if_t<std::is_same<decltype(std::declval<Tp>().begin()),
typename Tp::iterator>::value &&
std::is_same<decltype(std::declval<Tp>().end()),
typename Tp::iterator>::value> * = nullptr>
size_t operator()(Tp const &tp) const {
size_t ret = 0;
for (auto &&i : tp) ret = append(ret, (*this)(i));
return ret;
}
};
using i64 = int64_t;
#define for_(i, l, r, vars...) \
for (std::make_signed_t<decltype(l + r)> i = (l), i##end = (r), ##vars; \
i <= i##end; \
++i)
#define rfor_(i, r, l, vars...) \
for (std::make_signed_t<decltype(r - l)> i = (r), i##end = (l), ##vars; \
i >= i##end; \
--i)
#define foreach_ref_(i, container) for (auto &i : (container))
#define read_var_(type, name) \
type name; \
std::cin >> name
#define read_container_(type, name, size) \
type name(size); \
foreach_ref_(i, name) std::cin >> i
template <class Tp>
constexpr auto chkmin(Tp &a, Tp b) -> bool {
return b < a ? a = b, true : false;
}
template <class Tp>
constexpr auto chkmax(Tp &a, Tp b) -> bool {
return a < b ? a = b, true : false;
}
template <class Tp>
constexpr auto ispow2(Tp i) -> bool {
return i && (i & -i) == i;
}
#define TPL_SIZE_(Tuple) std::tuple_size_v<std::remove_reference_t<Tuple>>
namespace tuple_detail_ {
template <std::size_t Begin, class Tuple, std::size_t... Is>
constexpr auto subtuple_impl_(Tuple &&t, std::index_sequence<Is...>) {
return std::make_tuple(std::get<Is + Begin>(t)...);
}
template <class Tuple, class BinOp, std::size_t... Is>
constexpr auto
apply2_impl_(BinOp &&f, Tuple &&lhs, Tuple &&rhs, std::index_sequence<Is...>) {
return std::make_tuple(
std::forward<BinOp>(f)(std::get<Is>(lhs), std::get<Is>(rhs))...);
}
} // namespace tuple_detail_
template <std::size_t Begin, std::size_t Len, class Tuple>
constexpr auto subtuple(Tuple &&t) {
static_assert(Begin <= TPL_SIZE_(Tuple) && Len <= TPL_SIZE_(Tuple) &&
Begin + Len <= TPL_SIZE_(Tuple),
"Out of range");
return tuple_detail_::subtuple_impl_<Begin>(t,
std::make_index_sequence<Len>());
}
template <std::size_t Pos, class Tp, class Tuple>
constexpr auto tuple_push(Tp &&v, Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
std::make_tuple(v),
subtuple<Pos, TPL_SIZE_(Tuple) - Pos>(t));
}
template <class Tp, class Tuple>
constexpr auto tuple_push_front(Tp &&v, Tuple &&t) {
return tuple_push<0>(v, t);
}
template <class Tp, class Tuple>
constexpr auto tuple_push_back(Tp &&v, Tuple &&t) {
return tuple_push<TPL_SIZE_(Tuple)>(v, t);
}
template <std::size_t Pos, class Tuple>
constexpr auto tuple_pop(Tuple &&t) {
static_assert(TPL_SIZE_(Tuple) > 0, "Pop from empty tuple");
return std::tuple_cat(subtuple<0, Pos>(t),
subtuple<Pos + 1, TPL_SIZE_(Tuple) - Pos - 1>(t));
}
template <class Tuple>
constexpr auto tuple_pop_front(Tuple &&t) {
return tuple_pop<0>(t);
}
template <class Tuple>
constexpr auto tuple_pop_back(Tuple &&t) {
return tuple_pop<TPL_SIZE_(Tuple) - 1>(t);
}
template <class Tuple, class BinOp>
constexpr auto apply2(BinOp &&f, Tuple &&lhs, Tuple &&rhs) {
return tuple_detail_::apply2_impl_(
f, lhs, rhs, std::make_index_sequence<TPL_SIZE_(Tuple)>());
}
#define OO_PTEQ_(op) \
template <class Tp, class Up> \
constexpr auto operator op(std::pair<Tp, Up> lhs, \
const std::pair<Tp, Up> &rhs) { \
return std::pair<Tp, Up>{lhs.first op rhs.first, \
lhs.second op rhs.second}; \
} \
template <class... Ts> \
constexpr auto operator op(std::tuple<Ts...> const &lhs, \
std::tuple<Ts...> const &rhs) { \
return apply2([](auto &&l, auto &&r) { return l op r; }, lhs, rhs); \
} \
template <class Tp, class Up> \
constexpr std::pair<Tp, Up> &operator op##=(std::pair<Tp, Up> &lhs, \
const std::pair<Tp, Up> &rhs) { \
lhs.first op## = rhs.first; \
lhs.second op## = rhs.second; \
return lhs; \
} \
template <class... Ts> \
constexpr auto operator op##=(std::tuple<Ts...> &lhs, \
const std::tuple<Ts...> &rhs) { \
return lhs = lhs op rhs; \
}
OO_PTEQ_(+)
OO_PTEQ_(-)
OO_PTEQ_(*)
OO_PTEQ_(/)
OO_PTEQ_(%)
OO_PTEQ_(&)
OO_PTEQ_(|)
OO_PTEQ_(^)
OO_PTEQ_(<<)
OO_PTEQ_(>>)
#undef OO_PTEQ_
#undef TPL_SIZE_
template <class Tp, class Up>
std::istream &operator>>(std::istream &is, std::pair<Tp, Up> &p) {
return is >> p.first >> p.second;
}
template <class Tp, class Up>
std::ostream &operator<<(std::ostream &os, const std::pair<Tp, Up> &p) {
return os << p.first << ' ' << p.second;
}
template <typename... Ts>
std::istream &operator>>(std::istream &is, std::tuple<Ts...> &p) {
std::apply([&](Ts &...targs) { ((is >> targs), ...); }, p);
return is;
}
template <typename... Ts>
std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &p) {
std::apply(
[&](Ts const &...targs) {
std::size_t n{0};
((os << targs << (++n != sizeof...(Ts) ? " " : "")), ...);
},
p);
return os;
}
template <
class Ch,
class Tr,
class Ct,
std::enable_if_t<std::is_same<decltype(std::declval<Ct>().begin()),
typename Ct::iterator>::value &&
std::is_same<decltype(std::declval<Ct>().end()),
typename Ct::iterator>::value> * = nullptr>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os,
const Ct &x) {
if (x.begin() == x.end()) return os;
for (auto it = x.begin(); it != x.end() - 1; ++it) os << *it << ' ';
os << x.back();
return os;
}
const i64 INF64 = 0x3f3f3f3f3f3f3f3f;
using namespace std;
auto solve([[maybe_unused]] int t_ = 0) -> void {
read_var_(int, n);
read_container_(vc<i64>, a, n);
const i64 lim = 500;
vc<i64> dp(n, INF64);
dp[0] = 0;
for_(i, 0, n - 1) {
rfor_(j, i - 1, max(i64(0), i - lim - 1))
chkmin(dp[i], dp[j] + min(a[i], a[j]) * (i - j) * (i - j));
if (a[i] < lim) {
rfor_(j, i - 1, 0) {
chkmin(dp[i], dp[j] + min(a[i], a[j]) * (i - j) * (i - j));
if (a[j] <= a[i]) break;
}
for_(j, i + 1, n - 1) {
chkmin(dp[j], dp[i] + min(a[i], a[j]) * (i - j) * (i - j));
if (a[j] <= a[i]) break;
}
}
}
cout << dp;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int i_ = 0;
solve(i_);
return 0;
}