Your title here... ==================[problem:1195A]
Идея: budalnik
Подготовка: budalnik
Разбор
Tutorial is loading...
Решение
// Created by Nikolay Budin
#ifdef LOCAL
# define _GLIBCXX_DEBUG
#else
# define cerr __get_ce
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
mt19937 tw(9450189);
#else
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }
void solve() {
int n, k;
cin >> n >> k;
map<int, int> have;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
have[num]++;
}
int cnt = 0;
int ans = 0;
for (pii p : have) {
cnt += p.ss % 2;
ans += p.ss / 2 * 2;
}
ans += (cnt + 1) / 2;
cout << ans << "\n";
}
int main() {
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed;
#endif
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count = 1;
// cin >> test_count;
for (int test = 1; test <= test_count; ++test) {
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
Идея: ?
Подготовка: MikeMirzayanov и cdkrot
Разбор
Tutorial is loading...
Решение (бинарный поиск)
#include <iostream>
#include <cmath>
int main() {
long long n, k;
std::cin >> n >> k;
long long l = -1, r = n + 1;
while (r - l > 1) {
long long m = (l + r) / 2;
if ((n - m) * (n - m + 1) / 2 - m > k)
l = m;
else
r = m;
}
std::cout << r;
return 0;
}
Решение (формула)
#include <iostream>
#include <cmath>
int main() {
long long n, k;
std::cin >> n >> k;
std::cout << static_cast<long long>(round(n + 1.5 - sqrt(2 * (n + k) + 2.75)));
return 0;
}
Идея: meshanya
Подготовка: tsarn
Разбор
Tutorial is loading...
Решение
// Created by Nikolay Budin
#ifdef LOCAL
# define _GLIBCXX_DEBUG
#else
# define cerr __get_ce
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
mt19937 tw(9450189);
#else
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }
void solve() {
int n;
cin >> n;
vector<vector<int>> inp;
for (int i = 0; i < 2; ++i) {
inp.push_back(vector<int>());
for (int j = 0; j < n; ++j) {
int num;
cin >> num;
inp[i].push_back(num);
}
}
pll d = {0, 0};
for (int i = 0; i < n; ++i) {
pll next = {max(d.ff, d.ss + inp[0][i]), max(d.ss, d.ff + inp[1][i])};
d = next;
}
cout << max(d.ff, d.ss) << "\n";
}
int main() {
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed;
#endif
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count = 1;
// cin >> test_count;
for (int test = 1; test <= test_count; ++test) {
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
1195D1 - Подводная лодка в Рыбинском море (упрощённая редакция)
Идея: MikeMirzayanov
Подготовка: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
return a;
}
int mul(int a, int b) {
return a * 1ll * b % MOD;
}
int len;
vector<int> pw10;
int f(int a) {
int pos = 0;
int res = 0;
while (a > 0) {
int cur = a % 10;
a /= 10;
res = add(res, mul(cur, pw10[2 * pos]));
res = add(res, mul(cur, pw10[2 * pos + 1]));
++pos;
}
return res;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
pw10 = vector<int>(30);
pw10[0] = 1;
for (int i = 1; i < 30; ++i) {
pw10[i] = mul(pw10[i - 1], 10);
}
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
len = to_string(a).size();
ans = add(ans, mul(n, f(a)));
}
cout << ans << endl;
return 0;
}
1195D2 - Подводная лодка в Рыбинском море (усложнённая редакция)
Идея: meshanya
Подготовка: sava-cska
Разбор
Tutorial is loading...
Решение
// Created by Nikolay Budin
#ifdef LOCAL
# define _GLIBCXX_DEBUG
#else
# define cerr __get_ce
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
mt19937 tw(9450189);
#else
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }
const int MOD = 998244353;
void add(int& a, int b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
int sum(int a, int b) {
add(a, b);
return a;
}
int mult(int a, int b) {
return (ll) a * b % MOD;
}
int f(vector<int> const& a, int l) {
int res = 0;
int p = 1;
for (int i = 0; i < max(szof(a), l); ++i) {
if (i < l) {
p = mult(p, 10);
}
if (i < szof(a)) {
add(res, mult(a[i], p));
p = mult(p, 10);
}
}
return res;
}
int f(int l, vector<int> const& b) {
int res = 0;
int p = 1;
for (int i = 0; i < max(l, szof(b)); ++i) {
if (i < szof(b)) {
add(res, mult(b[i], p));
p = mult(p, 10);
}
if (i < l) {
p = mult(p, 10);
}
}
return res;
}
void solve() {
int n;
cin >> n;
vector<int> arr;
const int MAXL = 11;
vector<int> of_len(MAXL);
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
arr.push_back(num);
int l = 0;
int tmp = num;
while (tmp) {
++l;
tmp /= 10;
}
of_len[l]++;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
vector<int> digits;
int tmp = arr[i];
while (tmp) {
digits.push_back(tmp % 10);
tmp /= 10;
}
for (int l = 1; l < MAXL; ++l) {
int sum = f(digits, l);
add(ans, mult(sum, of_len[l]));
sum = f(l, digits);
add(ans, mult(sum, of_len[l]));
}
}
cout << ans << "\n";
}
int main() {
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed;
#endif
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count = 1;
// cin >> test_count;
for (int test = 1; test <= test_count; ++test) {
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
Идея: budalnik
Подготовка: ima_ima_go
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
long long n, m, a, b, g, x, y, z;
vector<deque<long long> > deq;
vector<vector<long long> > mi;
vector<int> power;
const long long MAX_V = 1000000000000000ll;
void ins(deque<long long>& de, long long val) {
while(!de.empty() && de.back() > val) {
de.pop_back();
}
de.push_back(val);
}
void del(deque<long long>& de, long long val) {
if (!de.empty() && de.front() == val) {
de.pop_front();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
cin >> a >> b;
cin >> g >> x >> y >> z;
mi.resize(n);
deq.resize(m);
for (int i = 0; i < n; i++) {
mi[i].resize(m);
for (int j = 0; j < m; j++) {
mi[i][j] = g;
g = (g * x + y) % z;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < a; j++) {
ins(deq[i], mi[j][i]);
}
}
deque<long long> real_deq;
for (int i = 0; i < b; i++) {
ins(real_deq, deq[i].front());
}
long long ans = 0;
ans += real_deq.front();
for (int i = b; i < m; i++) {
del(real_deq, deq[i - b].front());
ins(real_deq, deq[i].front());
ans += real_deq.front();
}
for (int i = a; i < n; i++) {
for (int j = 0 ; j < m; j++) {
ins(deq[j], mi[i][j]);
del(deq[j], mi[i - a][j]);
}
deque<long long> real_d;
for (int j = 0; j < b; j++) {
ins(real_d, deq[j].front());
}
ans += real_d.front();
for (int j = b; j < m; j++) {
del(real_d, deq[j - b].front());
ins(real_d, deq[j].front());
ans += real_d.front();
}
}
cout << ans << "\n";
}
1195F - Клуб анонимных геометров
Идея: craborac
Подготовка: budalnik
Разбор
Tutorial is loading...
Решение
// Created by Nikolay Budin
#ifdef LOCAL
# define _GLIBCXX_DEBUG
#else
# define cerr __get_ce
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
mt19937 tw(9450189);
#else
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }
struct point {
int x, y;
point(int _x, int _y) : x(_x), y(_y) {}
point operator-(point const& other) const {
return point(x - other.x, y - other.y);
}
point& operator/=(int num) {
x /= num;
y /= num;
return *this;
}
bool operator<(point const& other) const {
return x < other.x || (x == other.x && y < other.y);
}
};
void solve() {
int n;
cin >> n;
vector<vector<point>> polys;
vector<point> vecs;
vector<int> borders;
borders.push_back(0);
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
borders.push_back(borders.back() + k);
polys.push_back({});
for (int j = 0; j < k; ++j) {
int x, y;
cin >> x >> y;
polys.back().push_back(point(x, y));
}
for (int j = 0; j < k; ++j) {
int next = (j + 1) % k;
point v = polys[i][next] - polys[i][j];
int tmp = __gcd(abs(v.x), abs(v.y));
v /= tmp;
vecs.push_back(v);
}
}
vector<int> arr;
map<point, int> inds;
for (auto v : vecs) {
if (!inds.count(v)) {
int tmp = szof(inds);
inds[v] = tmp;
}
arr.push_back(inds[v]);
}
vector<int> next(szof(vecs));
vector<int> last(szof(inds), INF);
for (int i = szof(vecs) - 1; i >= 0; --i) {
next[i] = last[arr[i]];
last[arr[i]] = i;
}
vector<vector<pii>> here(szof(vecs));
int q;
cin >> q;
vector<int> ans(q);
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l;
l = borders[l];
r = borders[r];
here[l].push_back({r, i});
}
int bpv = 1;
while (bpv < szof(vecs)) {
bpv *= 2;
}
vector<int> segtree(bpv * 2);
function<void(int, int)> segtree_set = [&](int pos, int val) {
pos += bpv;
segtree[pos] = val;
pos /= 2;
while (pos) {
segtree[pos] = segtree[pos * 2] + segtree[pos * 2 + 1];
pos /= 2;
}
};
function<int(int, int, int, int, int)> segtree_get = [&](int v, int vl, int vr, int l, int r) {
if (vr <= l || r <= vl) {
return 0;
}
if (l <= vl && vr <= r) {
return segtree[v];
}
int vm = (vl + vr) / 2;
return segtree_get(v * 2, vl, vm, l, r) + segtree_get(v * 2 + 1, vm, vr, l, r);
};
for (int i = 0; i < szof(inds); ++i) {
segtree_set(last[i], 1);
}
for (int i = 0; i < szof(vecs); ++i) {
for (auto p : here[i]) {
ans[p.ss] = segtree_get(1, 0, bpv, i, p.ff);
}
segtree_set(i, 0);
if (next[i] != INF) {
segtree_set(next[i], 1);
}
}
for (int num : ans) {
cout << num << "\n";
}
}
int main() {
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed;
#endif
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count = 1;
// cin >> test_count;
for (int test = 1; test <= test_count; ++test) {
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}