Tutorial is loading...
Код автора
int n, a, b;
string s;
cin >> n >> a >> b >> s;
a--, b--;
cout << ((s[a] - '0') ^ (s[b] - '0'));
Tutorial is loading...
Код автора
int go(ll l, ll r, ll need, int alphSize)
{
ll m = l + (r - l) / 2LL;
if (need < m)
return go(l, m - 1, need, alphSize - 1);
else if (need > m)
return go(m + 1, r, need, alphSize - 1);
else
return alphSize;
}
void solveABig()
{
int n;
ll k;
cin >> n >> k;
ll sz = 1;
for (int i = 1; i < n; i++)
sz = sz * 2LL + 1LL;
cout << go(1, sz, k, n);
}
Tutorial is loading...
Код автора
int n;
cin >> n;
if (n == 1)
cout << -1 << endl;
else
cout << n << ' ' << n + 1 << ' ' << n * (n + 1) << endl;
Tutorial is loading...
Код автора
int a[maxn];
vector<int> g[maxn];
ll sum_subTree[maxn], mx_subTree[maxn];
void dfs1(int v, int p) {
sum_subTree[v] = a[v];
mx_subTree[v] = -llinf;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p)
continue;
dfs1(to, v);
sum_subTree[v] += sum_subTree[to];
mx_subTree[v] = max(mx_subTree[v], mx_subTree[to]);
}
mx_subTree[v] = max(mx_subTree[v], sum_subTree[v]);
}
ll ans = -llinf;
void dfs2(int v, int p, ll out) {
if (out != -llinf)
ans = max(ans, sum_subTree[v] + out);
vector<pair<ll, int> > miniset;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p)
continue;
miniset.pb(mp(mx_subTree[to], to));
sort(miniset.rbegin(), miniset.rend());
if (miniset.size() == 3)
miniset.pop_back();
}
miniset.pb(mp(-llinf, -1));
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to == p)
continue;
ll cur = miniset[0].s == to ? miniset[1].f : miniset[0].f;
dfs2(to, v, max(out, cur));
}
}
int main() {
for (int i = 0; i + 1 < n; i++) {
int u, v;
read(u, v);
u--, v--;
g[u].pb(v);
g[v].pb(u);
}
dfs1(0, -1);
dfs2(0, -1, -llinf);
cout << ((ans == -llinf) ? ("Impossible") : to_string(ans));
return 0;
}
Tutorial is loading...
Код автора
int can(int len) {
for (int i = 0; i < 8; i++)
cur[i] = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j < (1 << 8); j++)
dp[i][j] = -inf;
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << 8); j++) {
if (dp[i][j] == -inf)
continue;
for (int k = 0; k < 8; k++) {
if (j & (1 << k))
continue;
int it = cur[k] + len - 1;
if (it >= in[k].size())
continue;
amax(dp[in[k][it] + 1][j | (1 << k)], dp[i][j]);
it++;
if (it >= in[k].size())
continue;
amax(dp[in[k][it] + 1][j | (1 << k)], dp[i][j] + 1);
}
}
cur[a[i]]++;
}
int ans = -inf;
for (int i = 0; i <= n; i++)
ans = max(ans, dp[i][(1 << 8) - 1]);
if (ans == -inf)
return -1;
return ans * (len + 1) + (8 - ans) * len;
}
int main() {
for (int i = 0; i < n; i++) {
in[a[i]].push_back(i);
}
int l = 1, r = n / 8;
while (r - l > 1) {
int m = (l + r) >> 1;
if (can(m) != -1)
l = m;
else
r = m;
}
int ans = max(can(l), can(r));
if (ans == -1) {
ans = 0;
for (int i = 0; i < 8; i++)
if (!in[i].empty())
ans++;
}
cout << ans;
return 0;
}