Идея: vovuh
Разбор
Tutorial is loading...
Решение (vovuh)
for i in range(int(input())):
x, y, k = map(int, input().split())
print(((y + 1) * k - 1 + x - 2) // (x - 1) + k)
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Ne0n25)
#include <bits/stdc++.h>
using namespace std;
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < int(n); ++i)
void solve() {
int n;
cin >> n;
vector<int> a(n), b(n), c;
forn(i, n) cin >> a[i];
forn(i, n) cin >> b[i];
forn(i, n) if (!b[i])
c.push_back(a[i]);
sort(c.rbegin(), c.rend());
int j = 0;
forn(i, n) {
if (b[i]) cout << a[i] << ' ';
else cout << c[j++] << ' ';
}
cout << '\n';
}
int main() {
int T;
cin >> T;
while (T--) solve();
}
Идея: vovuh
Разбор
Tutorial is loading...
Решение (vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto &it : a) cin >> it;
int ans = 0;
ans += a[0] == 1;
for (int i = 1; i < n; ++i) {
if (a[i] == 0) {
continue;
}
int j = i;
while (j < n && a[j] == 1) {
++j;
}
ans += (j - i) / 3;
i = j - 1;
}
cout << ans << endl;
}
return 0;
}
Идея: vovuh
Разбор
Tutorial is loading...
Решение (vovuh)
#include <bits/stdc++.h>
using namespace std;
int get(const set<int> &x, const multiset<int> &len) {
if (len.empty()) return 0;
return *x.rbegin() - *x.begin() - *len.rbegin();
}
void add(int p, set<int> &x, multiset<int> &len) {
x.insert(p);
auto it = x.find(p);
int prv = -1, nxt = -1;
if (it != x.begin()) {
--it;
len.insert(p - *it);
prv = *it;
++it;
}
++it;
if (it != x.end()) {
len.insert(*it - p);
nxt = *it;
}
if (prv != -1 && nxt != -1) {
len.erase(len.find(nxt - prv));
}
}
void rem(int p, set<int> &x, multiset<int> &len) {
auto it = x.find(p);
int prv = -1, nxt = -1;
if (it != x.begin()) {
--it;
len.erase(len.find(p - *it));
prv = *it;
++it;
}
++it;
if (it != x.end()) {
len.erase(len.find(*it - p));
nxt = *it;
}
x.erase(p);
if (prv != -1 && nxt != -1) {
len.insert(nxt - prv);
}
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, q;
cin >> n >> q;
set<int> x;
multiset<int> len;
for (int i = 0; i < n; ++i) {
int p;
cin >> p;
add(p, x, len);
}
cout << get(x, len) << endl;
for (int i = 0; i < q; ++i) {
int t, p;
cin >> t >> p;
if (t == 0) {
rem(p, x, len);
} else {
add(p, x, len);
}
cout << get(x, len) << endl;
}
return 0;
}
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 9;
const int MOD = 998244353;
int mul(int a, int b) {
return (a * 1LL * b) % MOD;
}
int bp(int a, int n) {
int res = 1;
for (; n > 0; n /= 2) {
if (n & 1) res = mul(res, a);
a = mul(a, a);
}
return res;
}
int inv(int a) {
int ia = bp(a, MOD - 2);
assert(mul(a, ia) == 1);
return ia;
}
int n, m;
int d[N];
long long sd[N];
long long sum (int l, int r) {
return (sd[r] - sd[l]) % MOD;
}
int main(){
cin >> n >> m;
for (int i = 0; i < n; ++i)
scanf("%d", d + i);
sort(d, d + n);
for (int i = 0; i < n; ++i)
sd[i + 1] = sd[i] + d[i];
for (int i = 0; i < m; ++i) {
int a, b;
scanf("%d%d", &a, &b); // dur, def
int cnt = (d + n) - lower_bound(d, d + n, b);
int res = 0;
if (cnt >= a) {
res = mul( mul(cnt - a, inv(cnt)), sum(n - cnt, n) );
res += mul( mul(cnt - a + 1, inv(cnt + 1)), sum(0, n - cnt) );
res %= MOD;
}
printf("%d\n", res);
}
return 0;
}
Идея: adedalic
Разбор
Tutorial is loading...
Решение (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define x first
#define y second
typedef long long li;
typedef pair<int, int> pt;
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
return out << "(" << p.x << ", " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
out << "[";
fore(i, 0, sz(v)) {
if(i) out << ", ";
out << v[i];
}
return out << "]";
}
const int INF = int(1e9);
const li INF64 = li(1e18);
int n, m;
li l, r;
inline bool read() {
if(!(cin >> n >> m))
return false;
cin >> l >> r;
return true;
}
const int N = int(2e5) + 555;
vector<int> divs[N];
inline void solve() {
fore(d, 1, N) {
for(int pos = d; pos < N; pos += d)
divs[pos].push_back(d);
}
li lf = m + 1, rg = m;
vector<int> cnt(m + 1, 0);
vector<int> id(m + 1, -1);
set<int> curDivs;
vector< vector<int> > ans(n + 1);
fore(x1, 1, n + 1) {
li newlf = (l + x1 - 1) / x1;
li newrg = r / x1;
assert(newrg - newlf + 1 >= 0);
while (lf > newlf) {
lf--;
for (int d : divs[lf]) {
if (cnt[d] == 0)
curDivs.insert(d);
cnt[d]++;
id[d] = (int)lf;
}
}
while (rg > newrg) {
for (int d : divs[rg]) {
cnt[d]--;
if (cnt[d] == 0)
curDivs.erase(d);
}
rg--;
}
for (int a : divs[x1]) {
auto it = curDivs.upper_bound(a);
if (it == curDivs.end())
continue;
int b = *it;
if (x1 / a * 1ll * b <= n) {
int y1 = id[b];
ans[x1] = {x1, y1, x1 / a * b, y1 / b * a};
}
}
}
fore(i, 1, n + 1) {
if (ans[i].empty())
cout << -1 << '\n';
else {
cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << " " << ans[i][3] << '\n';
}
}
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
if(read()) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
Разбор
Tutorial is loading...
Решение 1 (pikmike)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
const int N = 60;
typedef array<int, N> num;
num operator ^(const num &a, const num &b){
num c;
forn(i, N){
c[i] = a[i] + b[i];
if (c[i] >= 3) c[i] -= 3;
}
return c;
}
mt19937 rnd(42);
int main(){
int n;
scanf("%d", &n);
vector<int> a(n);
forn(i, n){
scanf("%d", &a[i]);
--a[i];
}
vector<num> nums(n);
forn(i, n) forn(j, N) nums[i][j] = rnd() % 3;
vector<vector<int>> pos(n);
vector<num> pr(1);
map<num, int> cnt;
cnt[pr[0]] = 1;
int cur = 0;
long long ans = 0;
forn(i, n){
pr.push_back(pr.back() ^ nums[a[i]]);
pos[a[i]].push_back(i);
if (pos[a[i]].size() >= 4){
while (cur <= pos[a[i]][int(pos[a[i]].size()) - 4]){
--cnt[pr[cur]];
++cur;
}
}
ans += cnt[pr.back()];
++cnt[pr.back()];
}
printf("%lld\n", ans);
}
Решение 2 (pikmike)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
typedef unsigned long long uli;
vector<int> a;
vector<pair<int, int>> t;
vector<int> ps;
pair<int, int> merge(const pair<int, int> &a, const pair<int, int> &b){
if (a.first != b.first)
return min(a, b);
return make_pair(a.first, a.second + b.second);
}
void push(int v){
if (v * 2 + 1 < int(ps.size())){
ps[v * 2] += ps[v];
ps[v * 2 + 1] += ps[v];
}
t[v].first += ps[v];
ps[v] = 0;
}
void build(int v, int l, int r){
if (l == r - 1){
t[v] = make_pair(0, 1);
return;
}
int m = (l + r) / 2;
build(v * 2, l, m);
build(v * 2 + 1, m, r);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
void upd(int v, int l, int r, int L, int R, int val){
push(v);
if (L >= R)
return;
if (l == L && r == R){
ps[v] = val;
push(v);
return;
}
int m = (l + r) / 2;
upd(v * 2, l, m, L, min(m, R), val);
upd(v * 2 + 1, m, r, max(m, L), R, val);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
int main(){
int n;
scanf("%d", &n);
a.resize(n);
forn(i, n){
scanf("%d", &a[i]);
--a[i];
}
t.resize(4 * n);
ps.resize(4 * n);
build(1, 0, n);
vector<vector<int>> pos(n, vector<int>(1, -1));
long long ans = 0;
forn(i, n){
int k = pos[a[i]].size();
if (k >= 1) upd(1, 0, n, pos[a[i]][k - 1] + 1, i + 1, 1);
if (k >= 3) upd(1, 0, n, pos[a[i]][k - 3] + 1, pos[a[i]][k - 2] + 1, -1);
if (k >= 4) upd(1, 0, n, pos[a[i]][k - 4] + 1, pos[a[i]][k - 3] + 1, 1);
pos[a[i]].push_back(i);
push(1);
if (t[1].first == 0) ans += t[1].second - (n - i - 1);
}
printf("%lld\n", ans);
}