比赛记录 - Hello 2023

比赛链接

进度: 5 / 8

A - Hall of Fame

Thalia is a Legendary Grandmaster in chess. She has \(n\) trophies in a line numbered from \(1\) to \(n\) (from left to right) and a lamp standing next to each of them (the lamps are numbered as the trophies)

A lamp can be directed either to the left or to the right, and it illuminates all trophies in that direction (but not the one it is next to). More formally, Thalia has a string \(s\) consisting only of characters 'L' and 'R' which represents the lamps' current directions. The lamp \(i\) illuminates:

  • trophies \(1,2,\ldots, i-1\) if \(s_i\) is 'L';
  • trophies \(i+1,i+2,\ldots, n\) if \(s_i\) is 'R'

She can perform the following operation at most once:

  • Choose an index \(i\) (\(1 \leq i < n\));
  • Swap the lamps \(i\) and \(i+1\) (without changing their directions). That is, swap \(s_i\) with \(s_{i+1}\)

Thalia asked you to illuminate all her trophies (make each trophy illuminated by at least one lamp), or to tell her that it is impossible to do so. If it is possible, you can choose to perform an operation or to do nothing. Notice that lamps cannot change direction, it is only allowed to swap adjacent ones

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of the test cases follows

The first line of each test case contains a positive integer \(n\) (\(2 \leq n \leq 100\,000\)) — the number of trophies

The second line of each test case contains a string \(s\) of length \(n\) consisting only of characters 'L' and 'R' — the \(i\)-th character describes the direction of the \(i\)-th lamp

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(100\,000\)

Output

For each test case print \(-1\) if it is impossible to illuminate all trophies by performing one operation (or doing nothing). Otherwise, print \(0\) if you choose not to perform the operation (i.e., the trophies are illuminated by the initial positioning of the lamps), or an index \(i\) (\(1 \leq i < n\)) if you choose to swap lamps \(i\) and \(i+1\)

If there are multiple answers, print any

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
6
2
LL
2
LR
2
RL
2
RR
7
LLRLLLR
7
RRLRRRL

output

1
2
3
4
5
6
-1
1
0
-1
3
6

Note

In the first example, it is possible to swap lamps \(1\) and \(2\), or do nothing. In any case, the string "LL" is obtained. Not all trophies are illuminated since trophy \(2\) is not illuminated by any lamp — lamp \(1\) illuminates nothing and lamp \(2\) illuminates only the trophy \(1\)

In the second example, it is necessary to swap lamps \(1\) and \(2\). The string becomes "RL". Trophy \(1\) is illuminated by lamp \(2\) and trophy \(2\) is illuminated by lamp \(1\), hence it is possible to illuminate all trophies

In the third example, all trophies are initially illuminated — hence, not performing any operation is a valid solution

In the last two examples performing swaps is not necessary as all trophies are illuminated initially. But, the presented solutions are also valid

代码参考

Show code

CodeForces_1779Aview 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
/*
* @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;
}
};
#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 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_(int, n);
read_var_(string, s);
if (count(all_(s), 'L') == n || count(all_(s), 'R') == n)
run_return_void_(cout << "-1\n");
if (s.find("RL") != string::npos) run_return_void_(cout << "0\n");
cout << s.find("LR") + 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 - MKnez's ConstructiveForces Task

MKnez wants to construct an array \(s_1,s_2, \ldots , s_n\) satisfying the following conditions:

  • Each element is an integer number different from \(0\);
  • For each pair of adjacent elements their sum is equal to the sum of the whole array

More formally, \(s_i \neq 0\) must hold for each \(1 \leq i \leq n\). Moreover, it must hold that \(s_1 + s_2 + \cdots + s_n = s_i + s_{i+1}\) for each \(1 \leq i < n\)

Help MKnez to construct an array with these properties or determine that it does not exist

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 100\)). The description of the test cases follows

The only line of each test case contains a single integer \(n\) (\(2 \leq n \leq 1000\)) — the length of the array

Output

For each test case, print "YES" if an array of length \(n\) satisfying the conditions exists. Otherwise, print "NO". If the answer is "YES", on the next line print a sequence \(s_1,s_2, \ldots, s_n\) satisfying the conditions. Each element should be a non-zero integer in the range \([-5000,5000]\), i. e. \(-5000 \leq s_i \leq 5000\) and \(s_i \neq 0\) should hold for each \(1 \leq i \leq n\)

It can be proved that if a solution exists then there also exists one which satisfies the additional constraints on the range

If there are several correct answers, print any of them

Example

input

1
2
3
2
2
3

output

1
2
3
YES
9 5
NO

Note

In the first test case, \([9,5]\) is a valid answer since \(9+5\) (the sum of the two adjacent elements \(s_1+s_2\)) is equal to \(9+5\) (the sum of all elements). Other solutions include \([6,-9], [-1,-2], [-5000,5000], \ldots\)

For the second test case, let us show why some arrays do not satisfy the constraints:

  • \([1,1,1]\)\(s_1+s_2 = 1+1 = 2\) and \(s_1+s_2+s_3=1+1+1 = 3\) differ;
  • \([1,-1,1]\)\(s_1+s_2=1+(-1)=0\) and \(s_1+s_2+s_3=1+(-1)+1 = 1\) differ;
  • \([0,0,0]\) — The array \(s\) cannot contain a \(0\)

This is not a proof, but it can be shown that the answer is "NO"

代码参考

Show code

CodeForces_1779Bview 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
/*
* @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;
}
};
#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 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
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;
auto solve([[maybe_unused]] int t_ = 0) -> void {
read_var_(int, n);
if (n == 3) run_return_void_(cout << RES_YN[0] << '\n');
cout << RES_YN[1] << '\n';
if (n % 2 == 0)
for_(i, 1, n) cout << (i & 1 ? 1 : -1) << " \n"[i == n];
else
for_(i, 1, n) cout << (i & 1 ? n / 2 - 1 : -n / 2) << " \n"[i == 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 - Least Prefix Sum

Baltic, a famous chess player who is also a mathematician, has an array \(a_1,a_2, \ldots, a_n\), and he can perform the following operation several (possibly \(0\)) times:

  • Choose some index \(i\) (\(1 \leq i \leq n\));
  • multiply \(a_i\) with \(-1\), that is, set \(a_i := -a_i\)

Baltic's favorite number is \(m\), and he wants \(a_1 + a_2 + \cdots + a_m\) to be the smallest of all non-empty prefix sums. More formally, for each \(k = 1,2,\ldots, n\) it should hold that

\[ a_1 + a_2 + \cdots + a_k \geq a_1 + a_2 + \cdots + a_m. \]

Please note that multiple smallest prefix sums may exist and that it is only required that \(a_1 + a_2 + \cdots + a_m\) is one of them

Help Baltic find the minimum number of operations required to make \(a_1 + a_2 + \cdots + a_m\) the least of all prefix sums. It can be shown that a valid sequence of operations always exists

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of the test cases follows

The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq m \leq n \leq 2\cdot 10^5\)) — the size of Baltic's array and his favorite number

The second line contains \(n\) integers \(a_1,a_2, \ldots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)) — the array

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

Output

For each test case, print a single integer — the minimum number of required operations

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
6
4 3
-1 -2 -3 -4
4 3
1 2 3 4
1 1
1
5 5
-2 3 -5 1 -20
5 2
-2 3 -5 -5 -20
10 4
345875723 -48 384678321 -375635768 -35867853 -35863586 -358683842 -81725678 38576 -357865873

output

1
2
3
4
5
6
1
1
0
0
3
4

Note

In the first example, we perform the operation \(a_4 := -a_4\). The array becomes \([-1,-2,-3,4]\) and the prefix sums, \([a_1, \ a_1+a_2, \ a_1+a_2+a_3, \ a_1+a_2+a_3+a_4]\), are equal to \([-1,-3,-6,-2]\). Thus \(a_1 + a_2 + a_3=-6\) is the smallest of all prefix sums

In the second example, we perform the operation \(a_3 := -a_3\). The array becomes \([1,2,-3,4]\) with prefix sums equal to \([1,3,0,4]\)

In the third and fourth examples, \(a_1 + a_2 + \cdots + a_m\) is already the smallest of the prefix sums — no operation needs to be performed

In the fifth example, a valid sequence of operations is:

  • \(a_3 := -a_3\),
  • \(a_2 := -a_2\),
  • \(a_5 := -a_5\)

The array becomes \([-2,-3,5,-5,20]\) and its prefix sums are \([-2,-5,0,-5,15]\). Note that \(a_1+a_2=-5\) and \(a_1+a_2+a_3+a_4=-5\) are both the smallest of the prefix sums (and this is a valid solution)

代码参考

Show code

CodeForces_1779Cview 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
/*
* @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 pq = std::priority_queue<Tp>;
template <class Tp>
using pqg = std::priority_queue<Tp, std::vector<Tp>, std::greater<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 run_exec_(expressions, post_process) \
{ \
expressions; \
post_process; \
}
#define run_return_void_(expressions) run_exec_(expressions, return)
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 {
i64 n, m;
cin >> n >> m;
vc<i64> a(n + 1);
for_(i, 1, n) cin >> a[i];
if (n == 1) run_return_void_(cout << "0\n");
i64 ans = 0;
i64 now = 0;
pq<i64> pq1;
rfor_(i, m, 2) {
now += a[i];
if (now > 0) {
if (!pq1.empty() && pq1.top() > a[i]) {
now -= 2 * pq1.top();
pq1.pop();
} else {
now -= 2 * a[i];
a[i] = -a[i];
}
++ans;
}
if (a[i] > 0) pq1.push(a[i]);
}
now = 0;
pqg<i64> pq2;
for_(i, m + 1, n) {
now += a[i];
if (now < 0) {
if (!pq2.empty() && pq2.top() < a[i]) {
now -= 2 * pq2.top();
pq2.pop();
} else {
now -= 2 * a[i];
a[i] = -a[i];
}
ans++;
}
if (a[i] < 0) pq2.push(a[i]);
}
cout << ans << '\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 - Boris and His Amazing Haircut

Boris thinks that chess is a tedious game. So he left his tournament early and went to a barber shop as his hair was a bit messy

His current hair can be described by an array \(a_1,a_2,\ldots, a_n\), where \(a_i\) is the height of the hair standing at position \(i\). His desired haircut can be described by an array \(b_1,b_2,\ldots, b_n\) in a similar fashion

The barber has \(m\) razors. Each has its own size and can be used at most once. In one operation, he chooses a razor and cuts a segment of Boris's hair. More formally, an operation is:

  • Choose any razor which hasn't been used before, let its size be \(x\);
  • Choose a segment \([l,r]\) (\(1\leq l \leq r \leq n\));
  • Set \(a_i := \min (a_i,x)\) for each \(l\leq i \leq r\);

Notice that some razors might have equal sizes — the barber can choose some size \(x\) only as many times as the number of razors with size \(x\)

He may perform as many operations as he wants, as long as any razor is used at most once and \(a_i = b_i\) for each \(1 \leq i \leq n\) at the end. He does not have to use all razors

Can you determine whether the barber can give Boris his desired haircut?

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 20\,000\)). The description of the test cases follows

The first line of each test case contains a positive integer \(n\) (\(3\leq n\leq 2\cdot 10^5\)) — the length of arrays \(a\) and \(b\)

The second line of each test case contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) — Boris's current hair

The third line of each test case contains \(n\) positive integers \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq 10^9\)) — Boris's desired hair

The fourth line of each test case contains a positive integer \(m\) (\(1 \leq m \leq 2\cdot 10^5\)) — the number of razors

The fifth line of each test case contains \(m\) positive integers \(x_1,x_2, \ldots, x_m\) (\(1 \leq x_i \leq 10^9\)) — the sizes of the razors

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

Output

For each test case, print "YES" if the barber can cut Boris's hair as desired. Otherwise, print "NO"

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses

Example

input

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
7
3
3 3 3
2 1 2
2
1 2
6
3 4 4 6 3 4
3 1 2 3 2 3
3
3 2 3
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
3
1 1 1
1 1 2
12
4 2 4 3 1 5 6 3 5 6 2 1
13
7 9 4 5 3 3 3 6 8 10 3 2 5
5 3 1 5 3 2 2 5 8 5 1 1 5
8
1 5 3 5 4 2 3 1
13
7 9 4 5 3 3 3 6 8 10 3 2 5
5 3 1 5 3 2 2 5 8 5 1 1 5
7
1 5 3 4 2 3 1
3
19747843 2736467 938578397
2039844 2039844 2039844
1
2039844

output

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

Note

In the first test case, Boris's hair is initially \([3,3,3]\). Let us describe a sequence of \(2\) operations the barber can perform:

  • The barber uses the razor with size \(1\) on the segment \([2,2]\); hence Boris's hair becomes \([3,1,3]\)
  • The barber uses the razor with size \(2\) on the segment \([1,3]\); hence Boris's hair becomes \([2,1,2]\) which is the desired haircut

In the third test case, no operation has to be done since Boris's hair is already as desired

In the fourth test case, no cuts will be able to increase the third element in \([1,1,1]\) in a way that the array becomes \([1,1,2]\)

解题思路

显然 \(b_i\leq a_i,~\forall i\in 1..n\)\(b_i\in c,~\forall a_i\ne b_i\) 时才可能可行

我们将需要修剪的 \(i\)\(b_i\) 升序排列,每次尽可能修建尽可能大的范围,不难发现所有的 \(i\) 至多被修剪一次,维护修建范围的边界即可

代码参考

Show code

CodeForces_1779Dview 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
/*
* @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 pi = std::pair<Tp, Tp>;
template <class Tp>
using vc = std::vector<Tp>;
template <class Tp>
using pqg = std::priority_queue<Tp, std::vector<Tp>, std::greater<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;
using pii64 = pi<i64>;
#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 run_exec_(expressions, post_process) \
{ \
expressions; \
post_process; \
}
#define run_return_void_(expressions) run_exec_(expressions, return)
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;
auto solve([[maybe_unused]] int t_ = 0) -> void {
int n, m;
cin >> n;
vc<i64> a(n + 1), b(n + 1);
for_(i, 1, n) cin >> a[i];
for_(i, 1, n) cin >> b[i];
cin >> m;
multiset<i64> s;
for_(i, 1, m, x) {
cin >> x;
s.insert(x);
}
pqg<pii64> bb;
for_(i, 1, n)
if (a[i] < b[i])
run_return_void_(cout << RES_YN[0] << '\n') else if (a[i] > b[i])
bb.emplace(b[i], i);
vc<i64> L(n + 1), R(n + 1);
while (!bb.empty()) {
auto [v, idx] = bb.top();
bb.pop();
auto it = s.find(v);
if (it == s.end()) run_return_void_(cout << RES_YN[0] << '\n');
s.erase(it);
i64 l = idx - 1, r = idx + 1;
for (; l >= 1; --l) {
if (L[l]) l = L[l];
else {
if (b[l] > v) break;
else if (b[l] == v && a[l] != v) bb.pop();
}
}
for (; r <= n; ++r) {
if (R[r]) r = R[r];
else {
if (b[r] > v) break;
else if (b[r] == v && a[r] != v) bb.pop();
}
}
R[l + 1] = r - 1;
L[r - 1] = l + 1;
}
cout << RES_YN[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 - Anya's Simultaneous Exhibition

This is an interactive problem

Anya has gathered \(n\) chess experts numbered from \(1\) to \(n\) for which the following properties hold:

  • For any pair of players one of the players wins every game against the other (and no draws ever occur);
  • Transitivity does not necessarily hold — it might happen that \(A\) always beats \(B\), \(B\) always beats \(C\) and \(C\) always beats \(A\)

Anya does not know, for each pair, who is the player who beats the other

To organize a tournament, Anya hosts \(n-1\) games. In each game, she chooses two players. One of them wins and stays, while the other one is disqualified. After all the games are hosted only one player will remain. A player is said to be a candidate master if they can win a tournament (notice that the winner of a tournament may depend on the players selected by Anya in the \(n-1\) games)

Since Anya is a curious girl, she is interested in finding the candidate masters. Unfortunately, she does not have much time. To speed up the process, she will organize up to \(2n\) simuls (short for "simultaneous exhibition", in which one player plays against many)

In one simul, Anya chooses exactly one player who will play against some (at least one) of the other players. The chosen player wins all games they would win in a regular game, and the same holds for losses. After the simul finishes, Anya is only told the total number of games won by the chosen player (but not which ones). Nobody is disqualified during a simul

Can you help Anya host simuls and determine the candidate masters?

The winning players in each pair could be changed between the simuls, but only in a way that preserves the results of all previous simuls. These changes may depend on your queries

Interaction

Firstly, the jury sends one integer \(n\) (\(3 \leq n \leq 250\)) which should be read — the number of players. After that, your program may ask queries or report an answer

To ask a query, print "? \(i \; s_1 s_2 \ldots s_n\)" (without quotes), where \(i\) is the index of the player who will play against some of the other players in the simul. \(s\) is a binary string that denotes the players they play against. \(i\) plays against every player \(j\) for which \(s_j = 1\) holds (and \(s_j = 1\) should hold for at least one \(1 \leq j \leq n\)). Please note that \(s_i = 0\) must hold since a player cannot play against themselves, otherwise, the query is considered to be incorrect

After this, you should read an integer — the number of games player \(i\) has won

When you have identified the answer, you must print "! \(c_1 c_2 \ldots c_n\)" (without quotes) and terminate your program. \(c\) is a binary string which represents the candidate masters. Player \(i\) is a candidate master if \(c_i=1\) holds, otherwise, they are not

If you ask more than \(2n\) queries or if one of the queries is malformed, the interaction terminates immediately and your program receives verdict Wrong Answer

After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages

Hacks are disabled in this problem

Examples

input

1
2
3
4
5
6
7
3

1

1

1

output

1
2
3
4
5
6
7
8

? 1 010

? 2 001

? 3 100

! 111

input

1
2
3
4
5
6
7
5

0

3

4

output

1
2
3
4
5
6
7
8

? 5 10110

? 2 10111

? 1 01111

! 10000

Note

In the first example, the first query describes a simul in which player \(1\) plays against player \(2\) (and no one else). The answer to the query is \(1\), meaning that player \(1\) won the only game they played. We can conclude that \(1\) beats \(2\). Similarly, the second query tells us that \(2\) beats \(3\) and the third query tells us that \(3\) beats \(1\). All players are candidate masters in this case as

  • Player \(1\) can win the tournament if \(2\) and \(3\) play first. \(3\) loses and leaves, while \(2\) stays. \(1\) then plays against \(2\) and wins;
  • Other players can win in the same fashion

In the second example, the third query describes a simul in which player \(1\) plays against every other player. The answer to the query is \(4\), meaning that they won every game they played. It can be concluded that player \(1\) also beats every other player. They can never lose, hence they are the only player who can remain at the end of every possible tournament, and the only possible candidate master

解题思路

不难看出题目等价于:

有一个含 \(n\) 个点的竞赛图,你可以进行至多 \(2n\) 次询问,每次询问可以问某点在含该点的某个子图中的出度,问该竞赛图中所有满足如下条件的点 \(v\):

\[ \operatorname{dis}(v,v')\in\mathbb{N},~\forall v'\in V\setminus\{v\} \]

我们有引理

引理 - E-1 竞赛图 \(T=\lang V,E\rang\) 中出度最大的点到任意点的距离不超过 \(2\)

证明

假设出度最大的点为 \(v\), 与之相邻的点集为 \(V_1\), \(V_2=V\setminus(V_1\cup\{v\})\)

任取 \(V_2\) 中的点 \(u\), 若 \(\operatorname{dis}(v,u)>2\), 则说明 \(V_1\cup\{v\}\) 中所有的点均不与 \(u\) 相邻,由竞赛图的性质,\(d_{out}(u)\geq|V_1|+1>d_{out}(v)\), 矛盾

进一步,竞赛图中出度最大的点满足要求

另外,若该竞赛图是强连通的,则所有点均满足条件,否则,我们考虑所有的强连通分量 \(T_1,T_2,\dots,T_k\) (按拓扑序排列), 则

\[ d_{out}(u)>d_{out}(v),~\forall u\in T_i, v\in T_j, i<j \]

不难发现 \(T_1\) 中的点即为所求

我们只需 \(n\) 次查询获得所有点的出度,之后按出度排序后计算 \(T_1\) 即可

官方题解说可以将查询次数控制在 \(n-1\), 也许可以进一步利用竞赛图的性质来优化算法

代码参考

Show code

CodeForces_1779Eview 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
/*
* @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 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 {
auto query = [](int p, string_view s) -> int {
cout << "? " << p << ' ' << s << '\n';
int res;
cin >> res;
return res;
};
auto answer = [](string_view s) { cout << "! " << s << '\n'; };
read_var_(int, n);
vc<int> deg(n);
for_(i, 0, n - 1) {
string s(n, '1');
s[i] = '0';
deg[i] = query(i + 1, s);
}
vc<int> ord(n);
iota(all_(ord), 0);
sort(all_(ord), [&](int i, int j) { return deg[i] > deg[j]; });
string ans(n, '0');
for_(i, 0, n - 1, sum) {
ans[ord[i]] = '1';
sum = -i * (i + 1) / 2;
for_(j, 0, i) sum += deg[ord[j]];
if (sum == (i + 1) * (n - i - 1)) run_return_void_(answer(ans));
}
}
int main() {
int i_ = 0;
solve(i_);
return 0;
}

F - Xorcerer's Stones

Misha had been banned from playing chess for good since he was accused of cheating with an engine. Therefore, he retired and decided to become a xorcerer

One day, while taking a walk in a park, Misha came across a rooted tree with nodes numbered from \(1\) to \(n\). The root of the tree is node \(1\)

For each \(1\le i\le n\), node \(i\) contains \(a_i\) stones in it. Misha has recently learned a new spell in his xorcery class and wants to test it out. A spell consists of:

  • Choose some node \(i\) (\(1 \leq i \leq n\))
  • Calculate the bitwise XOR \(x\) of all \(a_j\) such that node \(j\) is in the subtree of \(i\) (\(i\) belongs to its own subtree)
  • Set \(a_j\) equal to \(x\) for all nodes \(j\) in the subtree of \(i\)

Misha can perform at most \(2n\) spells and he wants to remove all stones from the tree. More formally, he wants \(a_i=0\) to hold for each \(1\leq i \leq n\). Can you help him perform the spells?

A tree with \(n\) nodes is a connected acyclic graph which contains \(n-1\) edges. The subtree of node \(i\) is the set of all nodes \(j\) such that \(i\) lies on the simple path from \(1\) (the root) to \(j\). We consider \(i\) to be contained in its own subtree

Input

The first line contains a single integer \(n\) (\(2 \leq n \leq 2\cdot 10^5\)) — the size of the tree

The second line contains an array of integers \(a_1,a_2,\ldots, a_n\) (\(0 \leq a_i \leq 31\)), describing the number of stones in each node initially

The third line contains an array of integers \(p_2,p_3,\ldots, p_n\) (\(1 \leq p_i \leq i-1\)), where \(p_i\) means that there is an edge connecting \(p_i\) and \(i\)

Output

If there is not a valid sequence of spells, output \(-1\)

Otherwise, output a single integer \(q\) (\(0 \leq q \leq 2n\)) in the first line — the number of performed spells

In the second line output a sequence of integers \(v_1,v_2,\ldots,v_q\) (\(1 \leq v_i \leq n\)) — the \(i\)-th spell will be performed on the subtree of node \(v_i\). Please note that order matters

If multiple solutions exist, output any. You don't have to minimize the number of operations

Examples

input

1
2
3
2
13 13
1

output

1
2
1
1

input

1
2
3
7
5 2 8 3 4 1 31
1 1 2 2 3 3

output

1
-1

input

1
2
3
9
3 31 1 2 7 30 7 3 1
1 1 1 2 5 5 3 4

output

1
2
6
3 2 3 1 2 2

Note

Please refer to the following pictures for an explanation of the third test. Only the first \(4\) spells are shown since the last \(2\) do nothing. The first picture represents the tree initially with the number of stones for each node written above it in green. Changes applied by the current spell are highlighted in red

代码参考

Show code

G - The Game of the Century

The time has finally come, MKnez and Baltic are to host The Game of the Century. For that purpose, they built a village to lodge its participants

The village has the shape of an equilateral triangle delimited by three roads of length \(n\). It is cut into \(n^2\) smaller equilateral triangles, of side length \(1\), by \(3n-3\) additional roads which run parallel to the sides. See the figure for \(n=3\). Each of the \(3n\) roads is made of multiple (possibly \(1\)) road segments of length \(1\) which connect adjacent intersections

The direction has already been chosen for each of the \(3n\) roads (so, for each road, the same direction is assigned to all its road segments). Traffic can only go in the specified directions (i. e. the roads are monodirectional)

You are tasked with making adjustments to the traffic plan so that from each intersection it is possible to reach every other intersection. Specifically, you can invert the traffic direction of any number of road segments of length \(1\). What is the minimal number of road segments for which you need to invert the traffic direction?

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of the test cases follows

The first line of each test case contains a positive integer \(n\) (\(1\leq n\leq 10^5\)) — the size of the triangular village's sides

Three lines follow, each containing a binary string of length \(n\) which describes the traffic directions of the roads

The \(i\)-th of the following three lines contains a binary string \(s_i\) of length \(n\) representing the direction of each road parallel to the road segment denoted by \(i\) in the picture above. In particular, the \(j\)-th character of \(s_i\) is "1" if the \(j\)-th shortest road (parallel to the road segment denoted by \(i\) in the picture) has the same direction of the road segment denoted by \(i\) in the picture, while it is "0" if it has the opposite direction. So the first character of \(s_i\) describes the direction of the road containing only \(1\) road segment, while the last character describes the direction of the road containing \(n\) road segments

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

Output

For each test case, print the minimum number of road segments for which you need to invert the traffic direction

Example

input

1
2
3
4
5
6
7
8
9
10
11
12
13
3
3
001
001
010
1
0
0
0
3
111
011
100

output

1
2
3
2
0
3

Note

The first example corresponds to the picture in the statement. There exist multiple solutions that invert the traffic direction of exactly \(2\) road segments, but inverting only \(1\) road segment never makes it possible to reach every intersection from any other. One of the possible solutions is shown in the picture below in which the inverted road segments are highlighted in blue

In the second example, the answer is \(0\) since it is already possible to reach every intersection from any other

代码参考

Show code

H - Olympic Team Building

Iron and Werewolf are participating in a chess Olympiad, so they want to practice team building. They gathered \(n\) players, where \(n\) is a power of \(2\), and they will play sports. Iron and Werewolf are among those \(n\) people

One of the sports is tug of war. For each \(1\leq i \leq n\), the \(i\)-th player has strength \(s_i\). Elimination rounds will be held until only one player remains — we call that player the absolute winner

In each round:

  • Assume that \(m>1\) players are still in the game, where \(m\) is a power of \(2\)
  • The \(m\) players are split into two teams of equal sizes (i. e., with \(m/2\) players in each team). The strength of a team is the sum of the strengths of its players
  • If the teams have equal strengths, Iron chooses who wins; otherwise, the stronger team wins
  • Every player in the losing team is eliminated, so \(m/2\) players remain

Iron already knows each player's strength and is wondering who can become the absolute winner and who can't if he may choose how the teams will be formed in each round, as well as the winning team in case of equal strengths

Input

The first line contains a single integer \(n\) (\(4 \leq n \leq 32\)) — the number of players participating in tug of war. It is guaranteed that \(n\) is a power of \(2\)

The second line consists of a sequence \(s_1,s_2, \ldots, s_n\) of integers (\(1 \leq s_i \leq 10^{15}\)) — the strengths of the players

Output

In a single line output a binary string \(s\) of length \(n\) — the \(i\)-th character of \(s\) should be \(1\) if the \(i\)-th player can become the absolute winner and it should be \(0\) otherwise

Examples

input

1
2
4
60 32 59 87

output

1
1001

input

1
2
4
100 100 100 100

output

1
1111

input

1
2
8
8 8 8 8 4 4 4 4

output

1
11110000

input

1
2
32
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

output

1
00000000000000001111111111111111

input

1
2
16
1 92875987325987 1 1 92875987325986 92875987325985 1 92875987325988 92875987325990 92875987325989 1 1 92875987325984 92875987325983 1 1

output

1
0100110111001000

Note

In the first example, players \(1\) and \(4\) with their respective strengths of \(60\) and \(87\) can become the absolute winners

Let's describe the process for player \(1\). Firstly, we divide the players into teams \([1,3]\) and \([2,4]\). Strengths of those two teams are \(60+59=119\) and \(32+87=119\). They they are equal, Iron can choose to disqualify any of the two teams. Let his choice be the second team

We are left with players \(1\) and \(3\). Since \(1\) has greater strength (\(60>59\)) they win and are declared the absolute winner as they are the last remaining player

In the third example, the strengths of the remaining players may look like \([8,8,8,8,4,4,4,4] \rightarrow [8,8,4,4] \rightarrow [8,4] \rightarrow [8]\). Each person with strength \(8\) can become the absolute winner and it can be proved that others can't

代码参考

Show code