Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
print('Alice' if max(a) >= max(b) else 'Bob')
print('Alice' if max(a) > max(b) else 'Bob')
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = int(input())
print(a[sum(map(int, input().split())) % n])
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tmp = [i for i in range(n)]
tmp.sort(key=lambda i: [a[i], b[i]])
for i in range(n - 1):
if a[tmp[i]] > a[tmp[i + 1]] or b[tmp[i]] > b[tmp[i + 1]]:
print("-1")
break
else:
ans = []
for i in range(n - 1):
for j in range(n - 1):
if a[j] > a[j + 1] or b[j] > b[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
b[j], b[j + 1] = b[j + 1], b[j]
ans.append([j + 1, j + 2])
print(len(ans))
for it in ans:
print(*it)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
long long v;
cin >> v;
queue<long long> q;
map<long long, int> dist;
dist[v] = 0;
q.push(v);
while(!q.empty())
{
long long k = q.front();
q.pop();
string s = to_string(k);
if(s.size() == n)
{
cout << dist[k] << endl;
return 0;
}
for(auto x : s)
{
if(x == '0') continue;
long long w = k * int(x - '0');
if(!dist.count(w))
{
dist[w] = dist[k] + 1;
q.push(w);
}
}
}
cout << -1 << endl;
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
const long long INF64 = 1e18;
typedef pair<int, int> pt;
#define x first
#define y second
int main() {
int n;
scanf("%d", &n);
vector<vector<pt>> d(n - 1, vector<pt>(2));
forn(i, n - 1) forn(j, 2){
scanf("%d%d", &d[i][j].x, &d[i][j].y);
--d[i][j].x, --d[i][j].y;
}
int lg = 1;
while ((1 << lg) < n - 1) ++lg;
vector<vector<vector<vector<long long>>>> dp(n - 2, vector<vector<vector<long long>>>(lg, vector<vector<long long>>(2, vector<long long>(2, INF64))));
forn(i, n - 2) forn(k, 2){
dp[i][0][0][k] = abs(d[i][0].x + 1 - d[i + 1][k].x) + abs(d[i][0].y - d[i + 1][k].y) + 1;
dp[i][0][1][k] = abs(d[i][1].x - d[i + 1][k].x) + abs(d[i][1].y + 1 - d[i + 1][k].y) + 1;
}
for (int l = 1; l < lg; ++l) forn(i, n - 2) forn(j, 2) forn(k, 2) forn(t, 2) if (i + (1 << (l - 1)) < n - 2){
dp[i][l][j][k] = min(dp[i][l][j][k], dp[i][l - 1][j][t] + dp[i + (1 << (l - 1))][l - 1][t][k]);
}
int m;
scanf("%d", &m);
forn(_, m){
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
--x1, --y1, --x2, --y2;
int l1 = max(x1, y1), l2 = max(x2, y2);
if (l1 > l2){
swap(l1, l2);
swap(x1, x2);
swap(y1, y2);
}
if (l1 == l2){
printf("%d\n", abs(x1 - x2) + abs(y1 - y2));
continue;
}
vector<long long> ndp(2);
ndp[0] = abs(x1 - d[l1][0].x) + abs(y1 - d[l1][0].y);
ndp[1] = abs(x1 - d[l1][1].x) + abs(y1 - d[l1][1].y);
for (int i = lg - 1; i >= 0; --i) if (l1 + (1 << i) < l2){
vector<long long> tmp(2, INF64);
forn(j, 2) forn(k, 2)
tmp[k] = min(tmp[k], ndp[j] + dp[l1][i][j][k]);
ndp = tmp;
l1 += (1 << i);
}
long long ans = INF64;
ans = min(ans, ndp[0] + abs(d[l1][0].x + 1 - x2) + abs(d[l1][0].y - y2) + 1);
ans = min(ans, ndp[1] + abs(d[l1][1].x - x2) + abs(d[l1][1].y + 1 - y2) + 1);
printf("%lld\n", ans);
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution 1 (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
struct edge{
int u, w;
};
int n;
vector<vector<edge>> g;
vector<vector<int>> ng;
vector<int> tin, tout, siz, ord;
int T;
vector<int> pw;
void init(int v, int p = -1){
tin[v] = T++;
ord.push_back(v);
siz[v] = 1;
for (const auto &it : g[v]){
int u = it.u, w = it.w;
if (u == p) continue;
pw[u] = w;
init(u, v);
siz[v] += siz[u];
}
tout[v] = T;
}
int isp(int v, int u){
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
vector<int> nsiz;
vector<vector<int>> dp;
long long dfs(int v, int x){
long long res = 0;
for (int u : ng[v])
res += dfs(u, x);
dp[v][0] = siz[v];
dp[v][1] = 0;
for (int u : ng[v]) dp[v][0] -= siz[u];
for (int u : ng[v]){
if (pw[u] == x){
res += dp[u][0] * 1ll * dp[v][0];
dp[v][1] += dp[u][0];
}
else{
res += dp[u][0] * 1ll * dp[v][1];
res += dp[u][1] * 1ll * dp[v][0];
dp[v][0] += dp[u][0];
dp[v][1] += dp[u][1];
}
}
return res;
}
int main() {
scanf("%d", &n);
g.resize(n);
forn(i, n - 1){
int v, u, w;
scanf("%d%d%d", &v, &u, &w);
--v, --u, --w;
g[v].push_back({u, w});
g[u].push_back({v, w});
}
T = 0;
tin.resize(n);
tout.resize(n);
siz.resize(n);
pw.resize(n, -1);
init(0);
vector<vector<int>> sv(n, vector<int>(1, 0));
for (int v : ord) for (auto it : g[v])
sv[it.w].push_back(v);
ng.resize(n);
nsiz.resize(n);
dp.resize(n, vector<int>(2));
long long ans = 0;
forn(i, n) if (!sv[i].empty()){
sv[i].resize(unique(sv[i].begin(), sv[i].end()) - sv[i].begin());
vector<int> st;
for (int v : sv[i]){
while (!st.empty() && !isp(st.back(), v))
st.pop_back();
if (!st.empty())
ng[st.back()].push_back(v);
st.push_back(v);
}
ans += dfs(0, i);
for (int v : sv[i]) ng[v].clear();
}
printf("%lld\n", ans);
return 0;
}
Solution 2 (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
struct edge{
int v, u, w;
};
vector<vector<edge>> t;
void add(int v, int l, int r, int L, int R, const edge &e){
if (L >= R)
return;
if (l == L && r == R){
t[v].push_back(e);
return;
}
int m = (l + r) / 2;
add(v * 2, l, m, L, min(m, R), e);
add(v * 2 + 1, m, r, max(m, L), R, e);
}
vector<int> rk, p;
vector<int*> where;
vector<int> val;
int getp(int a){
return a == p[a] ? a : getp(p[a]);
}
void unite(int a, int b){
a = getp(a), b = getp(b);
if (a == b) return;
if (rk[a] < rk[b]) swap(a, b);
where.push_back(&rk[a]);
val.push_back(rk[a]);
rk[a] += rk[b];
where.push_back(&p[b]);
val.push_back(p[b]);
p[b] = a;
}
void rollback(){
*where.back() = val.back();
where.pop_back();
val.pop_back();
}
long long trav(int v, int l, int r){
int sv = where.size();
for (auto it : t[v]) if (it.w == 0)
unite(it.v, it.u);
long long res = 0;
if (l == r - 1){
for (auto it : t[v]) if (it.w == 1)
res += rk[getp(it.v)] * 1ll * rk[getp(it.u)];
}
else{
int m = (l + r) / 2;
res += trav(v * 2, l, m);
res += trav(v * 2 + 1, m, r);
}
while (int(where.size()) > sv) rollback();
return res;
}
int main() {
int n;
scanf("%d", &n);
vector<edge> e(n - 1);
forn(i, n - 1){
scanf("%d%d%d", &e[i].v, &e[i].u, &e[i].w);
--e[i].v, --e[i].u, --e[i].w;
}
sort(e.begin(), e.end(), [](const edge &a, const edge &b){
return a.w < b.w;
});
t.resize(4 * n);
forn(i, n - 1){
int pos = lower_bound(e.begin(), e.end(), e[i], [](const edge &a, const edge &b){
return a.w < b.w;
}) - e.begin();
add(1, 0, n, 0, pos, {e[i].v, e[i].u, 0});
add(1, 0, n, pos, pos + 1, {e[i].v, e[i].u, 1});
add(1, 0, n, pos + 1, n, {e[i].v, e[i].u, 0});
}
rk.resize(n, 1);
p.resize(n);
iota(p.begin(), p.end(), 0);
printf("%lld\n", trav(1, 0, n));
return 0;
}