Add a bunch of other problems.
Some are not finished...
This commit is contained in:
89
Codeforces Round 1016 (Div. 3)/A. Ideal Generator.cpp
Normal file
89
Codeforces Round 1016 (Div. 3)/A. Ideal Generator.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/A */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int k;
|
||||
cin >> k;
|
||||
|
||||
if (k & 1) {
|
||||
cout << "YES\n";
|
||||
} else {
|
||||
cout << "NO\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
107
Codeforces Round 1016 (Div. 3)/B. Expensive Number.cpp
Normal file
107
Codeforces Round 1016 (Div. 3)/B. Expensive Number.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/B */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
vi pref(a.size(), 0);
|
||||
vi suf(a.size(), 0);
|
||||
|
||||
nrep(i, 1, a.size()) {
|
||||
if (a[i - 1] != '0') {
|
||||
pref[i]++;
|
||||
}
|
||||
pref[i] += pref[i - 1];
|
||||
}
|
||||
|
||||
for (int i = a.size() - 2; i >= 0; i--) {
|
||||
suf[i]++;
|
||||
suf[i] += suf[i + 1];
|
||||
}
|
||||
|
||||
int ans = INT32_MAX;
|
||||
rep(i, a.size()) {
|
||||
if (a[i] == '0') {
|
||||
continue;
|
||||
}
|
||||
rmin(ans, pref[i] + suf[i]);
|
||||
}
|
||||
cout << ans << '\n';
|
||||
}
|
||||
}
|
||||
123
Codeforces Round 1016 (Div. 3)/C. Simple Repetition.cpp
Normal file
123
Codeforces Round 1016 (Div. 3)/C. Simple Repetition.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
bool prime(ll n)
|
||||
{
|
||||
if (n == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ll lim = sqrt(n);
|
||||
for (ll i = 2; i <= lim; i++) {
|
||||
if (n % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
set<int> search;
|
||||
search.insert(2);
|
||||
search.insert(19);
|
||||
search.insert(23);
|
||||
while (t--) {
|
||||
string n;
|
||||
int k;
|
||||
cin >> n >> k;
|
||||
|
||||
size_t count = 0;
|
||||
rep(i, n.size()) {
|
||||
if (n[i] != '1') {
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count == n.size() && search.count(count * k)) {
|
||||
cout << "YES\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (k == 1 && prime(stoll(n))) {
|
||||
cout << "YES\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << "NO\n";
|
||||
}
|
||||
}
|
||||
217
Codeforces Round 1016 (Div. 3)/D. Skibidi Table.cpp
Normal file
217
Codeforces Round 1016 (Div. 3)/D. Skibidi Table.cpp
Normal file
@@ -0,0 +1,217 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/D */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
using pl = pair<ll, ll>;
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
ll n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
while (q--) {
|
||||
string op;
|
||||
cin >> op;
|
||||
|
||||
function<ll(int, pl, pl, pl, pl, ll, ll, ll, ll)> find_by_cord = [&](int n, pl p1, pl p2, pl p3, pl p4, ll x, ll y, ll tx, ll ty)
|
||||
{
|
||||
if (n == 1) {
|
||||
if (x == tx && y == ty) {
|
||||
return p1.first;
|
||||
}
|
||||
if (x == tx && y + 1 == ty) {
|
||||
return p2.first;
|
||||
}
|
||||
if (x + 1 == tx && y == ty) {
|
||||
return p3.first;
|
||||
}
|
||||
return p4.first;
|
||||
}
|
||||
|
||||
ll add = 1LL << (n - 1);
|
||||
ll mini;
|
||||
ll add1 = (1LL << (2 * (n - 1) - 2)) - 1;
|
||||
ll xn = 0;
|
||||
ll yn = 0;
|
||||
if (tx < x + add && ty < y + add) {
|
||||
mini = p1.first;
|
||||
xn = x;
|
||||
yn = y;
|
||||
}
|
||||
|
||||
if (tx < x + add && ty >= y + add) {
|
||||
mini = p2.first;
|
||||
xn = x;
|
||||
yn = y + add;
|
||||
}
|
||||
|
||||
if (ty < y + add && tx >= x + add) {
|
||||
mini = p3.first;
|
||||
xn = x + add;
|
||||
yn = y;
|
||||
}
|
||||
|
||||
if (tx >= x + add && ty >= y + add) {
|
||||
mini = p4.first;
|
||||
xn = x + add;
|
||||
yn = y + add;
|
||||
}
|
||||
|
||||
return find_by_cord(n - 1, make_pair(mini, mini + add1),
|
||||
make_pair(mini + add1 * 3 + 3, mini + add1 * 4 + 3),
|
||||
make_pair(mini + add1 * 2 + 2, mini + add1 * 3 + 2),
|
||||
make_pair(mini + add1 + 1, mini + add1 * 2 + 1),
|
||||
xn, yn, tx, ty);
|
||||
};
|
||||
|
||||
function<pl(int, pl, pl, pl, pl, ll, ll, ll)> find_by_value = [&](int n, pl p1, pl p2, pl p3, pl p4, ll x, ll y, ll v){
|
||||
if (n == 1) {
|
||||
if (v == p1.first) {
|
||||
return make_pair(x, y);
|
||||
}
|
||||
if (v == p2.first) {
|
||||
return make_pair(x, y + 1);
|
||||
}
|
||||
if (v == p3.first) {
|
||||
return make_pair(x + 1, y);
|
||||
}
|
||||
return make_pair(x + 1, y + 1);
|
||||
}
|
||||
|
||||
ll add = 1LL << (n - 1);
|
||||
ll mini;
|
||||
ll add1 = (1LL << (2 * (n - 1) - 2)) - 1;
|
||||
ll xn = 0;
|
||||
ll yn = 0;
|
||||
if (v >= p1.first && v <= p1.second) {
|
||||
mini = p1.first;
|
||||
xn = x;
|
||||
yn = y;
|
||||
}
|
||||
|
||||
if (v >= p2.first && v <= p2.second) {
|
||||
mini = p2.first;
|
||||
xn = x;
|
||||
yn = y + add;
|
||||
}
|
||||
|
||||
if (v >= p3.first && v <= p3.second) {
|
||||
mini = p3.first;
|
||||
xn = x + add;
|
||||
yn = y;
|
||||
}
|
||||
|
||||
if (v >= p4.first && v <= p4.second) {
|
||||
mini = p4.first;
|
||||
xn = x + add;
|
||||
yn = y + add;
|
||||
}
|
||||
|
||||
return find_by_value(n - 1, make_pair(mini, mini + add1),
|
||||
make_pair(mini + add1 * 3 + 3, mini + add1 * 4 + 3),
|
||||
make_pair(mini + add1 * 2 + 2, mini + add1 * 3 + 2),
|
||||
make_pair(mini + add1 + 1, mini + add1 * 2 + 1),
|
||||
xn, yn, v);
|
||||
|
||||
};
|
||||
|
||||
if (op == "->") {
|
||||
ll x, y;
|
||||
cin >> x >> y;
|
||||
|
||||
ll add = (1LL << (2 * n - 2));
|
||||
cout << find_by_cord(n, make_pair(1, add),
|
||||
make_pair(add * 3 + 1, add * 4),
|
||||
make_pair(add * 2 + 1, add * 3),
|
||||
make_pair(add + 1, add * 2),
|
||||
1, 1, x, y);
|
||||
cout << '\n';
|
||||
} else {
|
||||
ll v;
|
||||
cin >> v;
|
||||
|
||||
ll add = (1LL << (2 * n - 2));
|
||||
auto [x, y] = find_by_value(n, make_pair(1, add),
|
||||
make_pair(add * 3 + 1, add * 4),
|
||||
make_pair(add * 2 + 1, add * 3),
|
||||
make_pair(add + 1, add * 2),
|
||||
1, 1, v);
|
||||
|
||||
cout << x << ' ' << y << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
137
Codeforces Round 1016 (Div. 3)/E. Min Max MEX.cpp
Normal file
137
Codeforces Round 1016 (Div. 3)/E. Min Max MEX.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vvi act(n);
|
||||
rep(i, n) {
|
||||
ll num;
|
||||
cin >> num;
|
||||
|
||||
if (num >= n) {
|
||||
continue;
|
||||
}
|
||||
|
||||
act[num].push_back(i);
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
int l = 0;
|
||||
int h = n - 1;
|
||||
while (l <= h) {
|
||||
int mid = (l + h) / 2;
|
||||
|
||||
set<int> ms;
|
||||
int count = 0;
|
||||
|
||||
int now = -1;
|
||||
rep(i, k) {
|
||||
int tmp = now;
|
||||
bool stop = false;
|
||||
for (int j = 0; j <= mid; j++) {
|
||||
auto itr = upper_bound(act[j].begin(), act[j].end(), now);
|
||||
if (itr == act[j].end()) {
|
||||
stop = true;
|
||||
break;
|
||||
}
|
||||
rmax(tmp, *itr);
|
||||
}
|
||||
|
||||
if (stop) {
|
||||
break;
|
||||
}
|
||||
|
||||
count++;
|
||||
now = tmp;
|
||||
}
|
||||
|
||||
// cout << mid << ' ' << count << '\n';
|
||||
if (count >= k) {
|
||||
ans = mid + 1;
|
||||
l = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
h = mid - 1;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/F */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<string> a(n);
|
||||
cin >> a;
|
||||
|
||||
V<V<string>> b(m, V<string>(n));
|
||||
cin >> b;
|
||||
|
||||
int matched = 0;
|
||||
V<bool> matches(n, false);
|
||||
bool pos = true;
|
||||
|
||||
int ans = 0;
|
||||
|
||||
priority_queue<pair<int, int>> pq;
|
||||
|
||||
rep(i, m) {
|
||||
int count = 0;
|
||||
rep(j, n) {
|
||||
if (b[i][j] == a[j]) {
|
||||
count++;
|
||||
if (!matches[j]) {
|
||||
matches[j] = true;
|
||||
matched++;
|
||||
}
|
||||
}
|
||||
}
|
||||
pq.emplace(count, i);
|
||||
}
|
||||
|
||||
if (matched < n) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << n + (n - pq.top().first) * 2 << '\n';
|
||||
}
|
||||
}
|
||||
171
Codeforces Round 1016 (Div. 3)/G. Shorten the Array.cpp
Normal file
171
Codeforces Round 1016 (Div. 3)/G. Shorten the Array.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2093/problem/G */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
struct node {
|
||||
int nodes[2], count[2];
|
||||
|
||||
node(): count(0) {
|
||||
fill(nodes, nodes + 2, 0);
|
||||
}
|
||||
|
||||
int& operator[](int i) {
|
||||
return nodes[i];
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vi array(n);
|
||||
cin >> array;
|
||||
|
||||
V<node> bits(1);
|
||||
|
||||
auto add = [&](int i){
|
||||
int now = 0;
|
||||
for (int j = 30; j >= 0; j--) {
|
||||
int bit = (i >> j) & 1;
|
||||
if (bits[now][bit] == 0) {
|
||||
bits[now][bit] = bits.size();
|
||||
bits.emplace_back();
|
||||
}
|
||||
bits[now].count[bit]++;
|
||||
now = bits[now][bit];
|
||||
}
|
||||
};
|
||||
|
||||
auto rem = [&](int i){
|
||||
int now = 0;
|
||||
for (int j = 30; j >= 0; j--) {
|
||||
int bit = (i >> j) & 1;
|
||||
bits[now].count[bit]--;
|
||||
if (bits[now].count[bit] == 0) {
|
||||
bits[now][bit] = 0;
|
||||
return;
|
||||
}
|
||||
now = bits[now][bit];
|
||||
}
|
||||
};
|
||||
|
||||
auto search = [&](int i){
|
||||
int now = 0;
|
||||
for (int j = 30; j >= 0; j--) {
|
||||
int bit = (i >> j) & 1;
|
||||
if (bits[now][!bit] == 0) {
|
||||
if (((k >> j) & 1) == 1) {
|
||||
return false;
|
||||
}
|
||||
now = bits[now][bit];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (((k >> j) & 1) == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
now = bits[now][!bit];
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
int ans = INT32_MAX;
|
||||
|
||||
int l = 0, r = 0;
|
||||
add(array[0]);
|
||||
while (r < array.size() && l <= r) {
|
||||
if (search(array[r])) {
|
||||
rmin(ans, r - l + 1);
|
||||
rem(array[l]);
|
||||
l++;
|
||||
continue;
|
||||
}
|
||||
|
||||
r++;
|
||||
if (r < array.size()) {
|
||||
add(array[r]);
|
||||
}
|
||||
}
|
||||
|
||||
if (r == array.size() && search(array[l])) {
|
||||
rmin(ans, r - l + 1);
|
||||
}
|
||||
|
||||
cout << (ans == INT32_MAX ? -1 : ans) << '\n';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user