All problems except the problem F are invented by fcspartakm. The problem F idea belongs to BledDest.
Tutorial
Tutorial is loading...
Solution
n, s = int(input()), list(input())
ans = 0
for i in range(0, n, 2):
if (s[i] == s[i + 1]):
s[i] = chr(1 - ord(s[i]) + 2 * ord('a'))
ans += 1
print(ans)
print(''.join(s))
Tutorial
Tutorial is loading...
Solution
n, a = int(input()), list(map(int, input().split()))
res = []
ans = 0
for i in range(n):
pos = -1
for j in range(n):
if (pos == -1 or a[j] > a[pos]): pos = j
res.append(pos + 1)
ans += i * a[pos] + 1
a[pos] = 0
print(ans)
print(' '.join([str(x) for x in res]))
Tutorial
Tutorial is loading...
Solution 1
#include <iostream>
#include <sstream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <cstring>
#include <cassert>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#define forn(i, n) for(int i=0;i<n;++i)
#define fore(i, l, r) for(int i = int(l); i <= int(r); ++i)
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define x first
#define y1 ________y1
#define y second
#define ft first
#define sc second
#define pt pair<int, int>
template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }
typedef long long li;
typedef long double ld;
using namespace std;
const int INF = 1000 * 1000 * 1000;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);
int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;
inline void read() {
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x5 >> y5 >> x6 >> y6;
x1 *= 2, y1 *= 2;
x2 *= 2, y2 *= 2;
x3 *= 2, y3 *= 2;
x4 *= 2, y4 *= 2;
x5 *= 2, y5 *= 2;
x6 *= 2, y6 *= 2;
}
inline bool ok(int x, int y, int x1, int y1, int x2, int y2) {
return x < x1 || x2 < x || y < y1 || y2 < y;
}
inline void solve() {
for (int x = x1; x <= x2; x++) {
if (ok(x, y1, x3, y3, x4, y4) && ok(x, y1, x5, y5, x6, y6)) {
cout << "YES" << endl;
return;
}
if (ok(x, y2, x3, y3, x4, y4) && ok(x, y2, x5, y5, x6, y6)) {
cout << "YES" << endl;
return;
}
}
for (int y = y1; y <= y2; y++) {
if (ok(x1, y, x3, y3, x4, y4) && ok(x1, y, x5, y5, x6, y6)) {
cout << "YES" << endl;
return;
}
if (ok(x2, y, x3, y3, x4, y4) && ok(x2, y, x5, y5, x6, y6)) {
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main () {
#ifdef fcspartakm
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cerr << setprecision(10) << fixed;
read();
solve();
//cerr << "TIME: " << clock() << endl;
}
Solution 2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>
#include <string>
#include <iomanip>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
typedef long long li;
typedef unsigned long long uli;
typedef pair<int, int> pii;
typedef long double ld;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define fore(i, l, r) for(int i = int(l); i < int(r); ++i)
#define forb(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define vi vector<int>
#define x first
#define y second
const int INF = 2e9;
const li INF64 = 1e18;
const int M = 2 * 1000 * 1000;
const int N = 300 * 1000 + 100;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = 3.14159265359;
pair<pii, pii> intersect(pii a, pii b, pii c, pii d) {
int lf, rg, up, dn;
lf = max(min(a.x, b.x), min(c.x, d.x));
rg = min(max(a.x, b.x), max(c.x, d.x));
up = min(max(a.y, b.y), max(c.y, d.y));
dn = max(min(a.y, b.y), min(c.y, d.y));
if (rg <= lf || up <= dn) return { {0, 0}, {0, 0} };
return { { lf, dn }, { rg, up } };
}
li square(pii a, pii b) {
return 1ll * abs(a.x - b.x) * abs(a.y - b.y);
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
vector<pii> p(6);
forn(i, 6)
cin >> p[i].x >> p[i].y;
pair<pii, pii> wb1 = intersect(p[0], p[1], p[2], p[3]);
pair<pii, pii> wb2 = intersect(p[0], p[1], p[4], p[5]);
pair<pii, pii> inter = intersect(wb1.x, wb1.y, wb2.x, wb2.y);
li swhite = square(p[0], p[1]);
li swb1 = square(wb1.x, wb1.y);
li swb2 = square(wb2.x, wb2.y);
li sinter = square(inter.x, inter.y);
if (swhite > swb1 + swb2 - sinter) cout << "YES\n";
else cout << "NO\n";
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <sstream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <cstring>
#include <cassert>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#define forn(i, n) for(int i=0;i<n;++i)
#define fore(i, l, r) for(int i = int(l); i <= int(r); ++i)
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define x first
#define y1 ________y1
#define y second
#define ft first
#define sc second
#define pt pair<int, int>
template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }
typedef long long li;
typedef long double ld;
using namespace std;
const int INF = 1000 * 1000 * 1000;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);
const int N = 200 * 1000 + 13;
int n;
int a[N];
inline void read() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
}
inline int gcd(int a, int b) {
while (a != 0 && b != 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
return max(a, b);
}
inline void solve() {
int ma = *max_element(a, a + n);
li sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
}
int g = ma - a[0];
for (int i = 1; i < n; i++) {
g = gcd(g, ma - a[i]);
}
li ans = (ma * 1ll * n - sum) / g;
cout << ans << ' ' << g << endl;
}
int main () {
#ifdef fcspartakm
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cerr << setprecision(10) << fixed;
read();
solve();
//cerr << "TIME: " << clock() << endl;
}
1216E1 - Numerical Sequence (easy version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q;
cin >> q;
while (q--) {
long long k;
cin >> k;
--k;
long long lst = 0;
long long nxtpw = 1;
int numlen = 0;
for (long long i = 1; ; ++i) {
if (i == nxtpw) {
++numlen;
nxtpw *= 10;
}
lst += numlen;
if (k >= lst) {
k -= lst;
} else {
long long nxtpw = 1;
int numlen = 0;
for (long long j = 1; j <= i; ++j) {
if (j == nxtpw) {
++numlen;
nxtpw *= 10;
}
if (k >= numlen) {
k -= numlen;
} else {
cout << to_string(j)[k] << endl;
break;
}
}
break;
}
}
}
return 0;
}
1216E2 - Numerical Sequence (hard version)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
long long get(long long r) {
return (r + 1) * r / 2;
}
long long sumto(long long r, int need) {
long long pw = 1;
long long sum = 0;
long long add = 0;
for (int len = 1; ; ++len) {
if (pw * 10 - 1 < r) {
long long cnt = pw * 10 - pw;
if (need) {
sum += add * cnt + get(cnt) * len;
add += cnt * len;
} else {
sum += cnt * len;
}
} else {
long long cnt = r - pw + 1;
if (need) {
sum += add * cnt + get(cnt) * len;
} else {
sum += cnt * len;
}
break;
}
pw *= 10;
}
return sum;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int q;
cin >> q;
while (q--) {
long long k;
cin >> k;
--k;
long long l = 1 , r = 1e9;
long long res = -1;
while (r - l >= 0) {
long long mid = (l + r) >> 1;
if (sumto(mid, 1) > k) {
res = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
k -= sumto(res - 1, 1);
l = 1, r = res;
long long num = -1;
while (r - l >= 0) {
int mid = (l + r) >> 1;
if (sumto(mid, 0) > k) {
num = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << to_string(num)[k - sumto(num - 1, 0)] << endl;
}
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<long long> dp(n + 1);
multiset<long long> mins, vals;
mins.insert(0);
vector<vector<long long>> del(n);
for (int i = n - 1; i >= 0; --i) {
dp[i] = i + 1 + dp[i + 1];
if (i + k + 2 <= n) mins.erase(mins.find(dp[i + k + 2]));
for (auto it : del[i]) vals.erase(vals.find(it));
if (!vals.empty()) dp[i] = min(dp[i], *vals.begin());
if (s[i] == '1') {
long long val = (mins.empty() ? 0 : *mins.begin()) + i + 1;
dp[i] = min(dp[i], val);
vals.insert(val);
if (i - k - 1 >= 0) del[i - k - 1].push_back(val);
}
mins.insert(dp[i]);
}
cout << dp[0] << endl;
return 0;
}