Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if a[0] + a[1] > a[-1]:
print(-1)
else:
print(1, 2, n)
1398B - Игра с удалением подстрок
Идея: 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() {
string s;
cin >> s;
vector<int> a;
forn(i, sz(s)) if (s[i] == '1') {
int j = i;
while (j + 1 < sz(s) && s[j + 1] == '1')
++j;
a.push_back(j - i + 1);
i = j;
}
sort(a.rbegin(), a.rend());
int ans = 0;
for (int i = 0; i < sz(a); i += 2)
ans += a[i];
cout << ans << endl;
}
int main() {
int T;
cin >> T;
while (T--) solve();
}
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
for _ in range(int(input())):
n = int(input())
a = input()
d = {0 : 1}
res, s = 0, 0
for i in range(n):
s += int(a[i])
x = s - i - 1
if x not in d:
d[x] = 0
d[x] += 1
res += d[x] - 1
print(res)
1398D - Цветные прямоугольники
Идея: BledDest
Разбор
Tutorial is loading...
Решение (pikmike)
n = [int(x) for x in input().split()]
a = []
for i in range(3):
a.append([int(x) for x in input().split()])
a[i].sort(reverse=True)
dp = [[[0 for i in range(n[2] + 1)] for j in range(n[1] + 1)] for k in range(n[0] + 1)]
ans = 0
for i in range(n[0] + 1):
for j in range(n[1] + 1):
for k in range(n[2] + 1):
if i < n[0] and j < n[1]:
dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k], dp[i][j][k] + a[0][i] * a[1][j])
if i < n[0] and k < n[2]:
dp[i + 1][j][k + 1] = max(dp[i + 1][j][k + 1], dp[i][j][k] + a[0][i] * a[2][k])
if j < n[1] and k < n[2]:
dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k] + a[1][j] * a[2][k])
ans = max(ans, dp[i][j][k])
print(ans)
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 9;
int n;
set <int> sDouble;
long long sum[2];
set <int> s[2];
int cntDouble[2];
// 0: 0 -> 1
// 1: 1 -> 0
void upd(int id) {
assert(s[id].size() > 0);
int x = *s[id].rbegin();
if (id == 1) x = *s[id].begin();
bool d = sDouble.count(x);
sum[id] -= x, sum[!id] += x;
s[id].erase(x), s[!id].insert(x);
cntDouble[id] -= d, cntDouble[!id] += d;
}
int main(){
cin >> n;
for (int i = 0; i < n; ++i) {
int tp, x;
cin >> tp >> x;// tp = 1 if double
if (x > 0) {
sum[0] += x;
s[0].insert(x);
cntDouble[0] += tp;
if (tp) sDouble.insert(x);
} else {
x = -x;
int id = 0;
if (s[1].count(x)) id = 1;
else assert(s[0].count(x));
sum[id] -= x;
s[id].erase(x);
cntDouble[id] -= tp;
if (tp) {
assert(sDouble.count(x));
sDouble.erase(x);
}
}
int sumDouble = cntDouble[0] + cntDouble[1];
while (s[1].size() < sumDouble) upd(0);
while (s[1].size() > sumDouble) upd(1);
while (s[1].size() > 0 && s[0].size() > 0 && *s[0].rbegin() > *s[1].begin()) {
upd(0);
upd(1);
}
assert(s[1].size() == sumDouble);
long long res = sum[0] + sum[1] * 2;
if (cntDouble[1] == sumDouble && sumDouble > 0) {
res -= *s[1].begin();
if (s[0].size() > 0) res += *s[0].rbegin();
}
cout << res << endl;
}
return 0;
}
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e6) + 99;
const int INF = int(1e9) + 99;
int n;
string s;
vector <int> p[2][N];
int nxt[2][N];
int ptr[2];
char buf[N];
int main(){
cin >> n >> s;
for (int i = n - 1; i >= 0; --i) {
if (s[i] != '0') nxt[0][i] = 1 + nxt[0][i + 1];
if (s[i] != '1') nxt[1][i] = 1 + nxt[1][i + 1];
}
for (int b = 0; b <= 1; ++b) {
int l = 0;
while (l < n) {
if (s[l] == char('0' + b)) {
++l;
continue;
}
int r = l + 1;
while (r < n && s[r] != char('0' + b)) ++r;
for (int len = 1; len <= r - l; ++len)
p[b][len].push_back(l);
l = r;
}
}
for (int len = 1; len <= n; ++len) {
int pos = 0, res = 0;
ptr[0] = ptr[1] = 0;
while (pos < n) {
int npos = INF;
for (int b = 0; b <= 1; ++b) {
if (nxt[b][pos] >= len)
npos = min(npos, pos + len);
while (ptr[b] < p[b][len].size() && pos > p[b][len][ ptr[b] ])
++ptr[b];
if (ptr[b] < p[b][len].size())
npos = min(npos, p[b][len][ ptr[b] ] + len);
}
if (npos != INF)
++res;
pos = npos;
}
cout << res << ' ';
}
cout << endl;
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (BledDest)
#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < n; i++)
#define sz(a) ((int)(a).size())
const int LOGN = 20;
const int N = (1 << LOGN);
const int K = 200043;
const int M = 1000043;
typedef double ld;
typedef long long li;
const ld PI = acos(-1.0);
struct comp
{
ld x, y;
comp(ld x = .0, ld y = .0) : x(x), y(y) {}
inline comp conj() { return comp(x, -y); }
};
inline comp operator +(const comp &a, const comp &b)
{
return comp(a.x + b.x, a.y + b.y);
}
inline comp operator -(const comp &a, const comp &b)
{
return comp(a.x - b.x, a.y - b.y);
}
inline comp operator *(const comp &a, const comp &b)
{
return comp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline comp operator /(const comp &a, const ld &b)
{
return comp(a.x / b, a.y / b);
}
vector<comp> w[LOGN];
vector<int> rv[LOGN];
void precalc()
{
for(int st = 0; st < LOGN; st++)
{
w[st].assign(1 << st, comp());
for(int k = 0; k < (1 << st); k++)
{
double ang = PI / (1 << st) * k;
w[st][k] = comp(cos(ang), sin(ang));
}
rv[st].assign(1 << st, 0);
if(st == 0)
{
rv[st][0] = 0;
continue;
}
int h = (1 << (st - 1));
for(int k = 0; k < (1 << st); k++)
rv[st][k] = (rv[st - 1][k & (h - 1)] << 1) | (k >= h);
}
}
inline void fft(comp a[N], int n, int ln, bool inv)
{
for(int i = 0; i < n; i++)
{
int ni = rv[ln][i];
if(i < ni)
swap(a[i], a[ni]);
}
for(int st = 0; (1 << st) < n; st++)
{
int len = (1 << st);
for(int k = 0; k < n; k += (len << 1))
{
for(int pos = k; pos < k + len; pos++)
{
comp l = a[pos];
comp r = a[pos + len] * (inv ? w[st][pos - k].conj() : w[st][pos - k]);
a[pos] = l + r;
a[pos + len] = l - r;
}
}
}
if(inv) for(int i = 0; i < n; i++)
a[i] = a[i] / n;
}
comp aa[N];
comp bb[N];
comp cc[N];
inline void multiply(comp a[N], int sza, comp b[N], int szb, comp c[N], int &szc)
{
int n = 1, ln = 0;
while(n < (sza + szb))
n <<= 1, ln++;
for(int i = 0; i < n; i++)
aa[i] = (i < sza ? a[i] : comp());
for(int i = 0; i < n; i++)
bb[i] = (i < szb ? b[i] : comp());
fft(aa, n, ln, false);
fft(bb, n, ln, false);
for(int i = 0; i < n; i++)
cc[i] = aa[i] * bb[i];
fft(cc, n, ln, true);
szc = n;
for(int i = 0; i < n; i++)
c[i] = cc[i];
}
comp a[N];
comp b[N];
comp c[N];
int used[M];
int dp[M];
int main()
{
precalc();
int n, x, y;
for(int i = 0; i < M; i++)
dp[i] = -1;
scanf("%d %d %d", &n, &x, &y);
vector<int> A(n + 1);
for(int i = 0; i <= n; i++)
scanf("%d", &A[i]);
for(int i = 0; i <= n; i++)
{
a[A[i]] = comp(1.0, 0.0);
b[K - A[i]] = comp(1.0, 0.0);
}
int s = 0;
multiply(a, K + 1, b, K + 1, c, s);
for(int i = K + 1; i < s; i++)
if(c[i].x > 0.5)
used[(i - K + y) * 2] = 1;
for(int i = 1; i < M; i++)
{
if(!used[i]) continue;
for(int j = i; j < M; j += i)
dp[j] = max(dp[j], i);
}
int q;
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
int l;
scanf("%d", &l);
printf("%d ", dp[l]);
}
}