feat(codeforces): 1062 div 4
This commit is contained in:
parent
6fc5131466
commit
7b9271093d
6 changed files with 296 additions and 0 deletions
27
codeforces/1062/2167a.cc
Normal file
27
codeforces/1062/2167a.cc
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
vector<int> a;
|
||||
void solve() {
|
||||
a.resize(4);
|
||||
for (int i = 0; i < a.size(); ++i) {
|
||||
cin >> a[i];
|
||||
}
|
||||
|
||||
sort(begin(a), end(a));
|
||||
|
||||
if (a.front() == a.back()) {
|
||||
cout << "YES";
|
||||
} else
|
||||
cout << "NO";
|
||||
cout << '\n';
|
||||
}
|
||||
int main() {
|
||||
int t;
|
||||
cin >> t;
|
||||
for (int i = 0; i < t; ++i) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
26
codeforces/1062/2167b.cc
Normal file
26
codeforces/1062/2167b.cc
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
string s, t;
|
||||
int n;
|
||||
void solve() {
|
||||
cin >> n;
|
||||
cin >> s >> t;
|
||||
sort(begin(s), end(s));
|
||||
sort(begin(t), end(t));
|
||||
if (s == t) {
|
||||
cout << "YES";
|
||||
} else {
|
||||
cout << "NO";
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
int main() {
|
||||
int t;
|
||||
cin >> t;
|
||||
for (int i = 0; i < t; ++i) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
33
codeforces/1062/2167c.cc
Normal file
33
codeforces/1062/2167c.cc
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
vector<int> a;
|
||||
int n;
|
||||
void solve() {
|
||||
cin >> n;
|
||||
a.resize(n);
|
||||
bool odd = false, even = false;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> a[i];
|
||||
odd |= a[i] & 1;
|
||||
even |= a[i] % 2 == 0;
|
||||
}
|
||||
|
||||
if (odd && even) {
|
||||
sort(begin(a), end(a));
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << a[i] << " \n"[i == n - 1];
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int t;
|
||||
cin >> t;
|
||||
for (int i = 0; i < t; ++i) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
30
codeforces/1062/2167d.cc
Normal file
30
codeforces/1062/2167d.cc
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
using u64 = unsigned long long;
|
||||
vector<u64> a;
|
||||
int n;
|
||||
void solve() {
|
||||
cin >> n;
|
||||
a.resize(n);
|
||||
for (auto& e : a) cin >> e;
|
||||
|
||||
for (u64 i = 2;; ++i) {
|
||||
for (auto& e : a) {
|
||||
if (gcd(e, i) == 1) {
|
||||
cout << i << '\n';
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int t;
|
||||
cin >> t;
|
||||
for (int i = 0; i < t; ++i) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
94
codeforces/1062/2167e.cc
Normal file
94
codeforces/1062/2167e.cc
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <climits>
|
||||
|
||||
using namespace std;
|
||||
using i64 = long long;
|
||||
|
||||
struct Seg {
|
||||
i64 L, R, PL, PR;
|
||||
int type; // 0=interior, 1=left-edge, 2=right-edge
|
||||
i64 pos, key;
|
||||
};
|
||||
|
||||
static void eval(Seg& s) {
|
||||
if (s.type == 1) {
|
||||
s.pos = s.L;
|
||||
s.key = s.PL - s.pos;
|
||||
} else if (s.type == 2) {
|
||||
s.pos = s.R;
|
||||
s.key = s.pos - s.PR;
|
||||
} else {
|
||||
i64 m = (s.PL + s.PR) / 2;
|
||||
if (m < s.L) m = s.L;
|
||||
if (m > s.R) m = s.R;
|
||||
s.pos = m;
|
||||
s.key = min(s.pos - s.PL, s.PR - s.pos);
|
||||
}
|
||||
if (s.key < 0) s.key = 0;
|
||||
}
|
||||
|
||||
struct Cmp {
|
||||
bool operator()(const Seg& a, const Seg& b) const { return a.key < b.key; }
|
||||
};
|
||||
|
||||
void solve() {
|
||||
int n; i64 k, X;
|
||||
cin >> n >> k >> X;
|
||||
vector<i64> A(n);
|
||||
for (auto& e : A) cin >> e;
|
||||
sort(A.begin(), A.end());
|
||||
|
||||
priority_queue<Seg, vector<Seg>, Cmp> pq;
|
||||
|
||||
if (A[0] > 0) {
|
||||
Seg s{0, A[0] - 1, A[0], 0, 1, 0, 0};
|
||||
eval(s); pq.push(s);
|
||||
}
|
||||
for (int i = 0; i + 1 < n; ++i) {
|
||||
if (A[i] + 1 <= A[i + 1] - 1) {
|
||||
Seg s{A[i] + 1, A[i + 1] - 1, A[i], A[i + 1], 0, 0, 0};
|
||||
eval(s); pq.push(s);
|
||||
}
|
||||
}
|
||||
if (A.back() < X) {
|
||||
Seg s{A.back() + 1, X, 0, A.back(), 2, 0, 0};
|
||||
eval(s); pq.push(s);
|
||||
}
|
||||
|
||||
i64 printed = 0;
|
||||
while (printed < k && !pq.empty()) {
|
||||
Seg s = pq.top(); pq.pop();
|
||||
cout << s.pos << (printed + 1 == k ? '\n' : ' ');
|
||||
++printed;
|
||||
if (s.L <= s.pos - 1) {
|
||||
Seg Ls{s.L, s.pos - 1, s.PL, s.PR, s.type, 0, 0};
|
||||
eval(Ls); pq.push(Ls);
|
||||
}
|
||||
if (s.pos + 1 <= s.R) {
|
||||
Seg Rs{s.pos + 1, s.R, s.PL, s.PR, s.type, 0, 0};
|
||||
eval(Rs); pq.push(Rs);
|
||||
}
|
||||
}
|
||||
|
||||
i64 last = LLONG_MIN;
|
||||
for (int i = 0; printed < k && i < n; ++i) {
|
||||
if (A[i] == last) continue;
|
||||
last = A[i];
|
||||
cout << A[i] << (printed + 1 == k ? '\n' : ' ');
|
||||
++printed;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) solve();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
86
codeforces/1062/2167f.cc
Normal file
86
codeforces/1062/2167f.cc
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using u32 = uint32_t;
|
||||
using i32 = int32_t;
|
||||
using u64 = uint64_t;
|
||||
using i64 = int64_t;
|
||||
|
||||
void solve() {
|
||||
u32 n;
|
||||
i64 k;
|
||||
cin >> n >> k;
|
||||
vector<vector<u32>> tree(n + 1);
|
||||
for (u32 i = 0; i < n - 1; ++i) {
|
||||
u32 u, v;
|
||||
cin >> u >> v;
|
||||
tree[u].emplace_back(v);
|
||||
tree[v].emplace_back(u);
|
||||
}
|
||||
|
||||
u64 ans = 0;
|
||||
|
||||
vector<i64> size(n + 1, 0);
|
||||
auto dfs = [&](auto &&self, u32 u, u32 par) -> i64 {
|
||||
i64 ss = 1;
|
||||
for (auto &v : tree[u]) {
|
||||
if (v == par) {
|
||||
continue;
|
||||
}
|
||||
ss += self(self, v, u);
|
||||
}
|
||||
|
||||
return size[u] = ss;
|
||||
};
|
||||
|
||||
dfs(dfs, 1, 0);
|
||||
i64 count =
|
||||
count_if(begin(size), end(size), [&k](auto const &e) { return e >= k; });
|
||||
|
||||
auto dfs2 = [&](auto &&self, u32 u, u32 par, i64 count) -> void {
|
||||
ans += count;
|
||||
|
||||
for (auto v : tree[u]) {
|
||||
if (v == par) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto su = size[u], sv = size[v];
|
||||
size[u] = su - sv;
|
||||
size[v] = n;
|
||||
|
||||
i64 child_count = count + (size[u] >= k) - (sv >= k);
|
||||
self(self, v, u, child_count);
|
||||
|
||||
size[u] = su;
|
||||
size[v] = sv;
|
||||
}
|
||||
};
|
||||
|
||||
dfs2(dfs2, 1, 0, count);
|
||||
|
||||
println("{}", ans);
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
std::cin.exceptions(std::cin.failbit);
|
||||
#ifdef LOCAL
|
||||
std::cerr.rdbuf(std::cout.rdbuf());
|
||||
std::cout.setf(std::ios::unitbuf);
|
||||
std::cerr.setf(std::ios::unitbuf);
|
||||
#else
|
||||
std::cin.tie(nullptr)->sync_with_stdio(false);
|
||||
#endif
|
||||
u32 tc = 1;
|
||||
std::cin >> tc;
|
||||
for (u32 t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue