[JOISC 2018] 做题记录
_MengShang_ · · 个人记录
P14365 [JOISC 2018] 高速公路建设 / Construction of Highway - 洛谷
查询相当于查根到
最终树的形态是固定的。考虑离线操作,得到整棵树后树剖。
发现每次查询之后都有到根的推平操作,因此可以对每条重链开
复杂度
代码有点长。
const int maxn = 1e5 + 5;
int n, m, c[maxn], A[maxn], B[maxn]; vector<int> e[maxn];
int siz[maxn], son[maxn], dep[maxn], fa[maxn], dfn[maxn], pos[maxn], tim, top[maxn];
inline void dfs1(int u, int fat) { fa[u] = fat; dep[u] = dep[fat] + 1; siz[u] = 1; for (int v : e[u]) if (v != fat) { dfs1(v, u); siz[u]+= siz[v]; if (siz[v] > siz[son[u]]) son[u] = v; } }
inline void dfs2(int u, int fff) { top[u] = fff; dfn[u] = ++tim; pos[tim] = u; if (son[u]) dfs2(son[u], fff); for (int v : e[u]) if (v != fa[u] && v != son[u]) dfs2(v, v); }
struct Node{ int l, r, v; Node(int il, int ir, int iv) : l(il), r(ir), v(iv) {} Node(int il) : l(il), r(0),v(0) {} };
inline bool operator < (const Node& x, const Node& y) { return x.l < y.l; }
set<Node> st;
inline auto split(int x)
{
auto it = st.lower_bound(Node(x)); if (it != st.end() && it->l == x) return it; if ((--it)->r < x) return st.end();
int l = it->l, r = it->r, v = it->v; st.erase(it); st.insert(Node(l, x - 1, v)); return st.insert(Node(x, r, v)).fst;
}
inline void assign(int l, int r, int v)
{
auto itr = split(r + 1), itl = split(l);
st.erase(itl, itr); st.insert(Node(l, r, v));
}
int sum[maxn];
inline void add(int x, int d) { for (; x <= m; x+= x & -x) sum[x]+= d; }
inline int qry(int x) { int res = 0; for (; x; x-= x & -x) res+= sum[x]; return res; }
inline ll solve(int x, int y)
{
ll res = 0; vector<pii> a, b;
while (x) { a.pbk({dfn[top[x]], dfn[x]}); x = fa[top[x]]; } reverse(a.begin(), a.end());
for (auto [l, r] : a)
{
auto itr = split(r + 1), itl = split(l);
for (auto it = itl; it != itr; ++it) { int len = (it->r - it->l + 1); res+= (ll)qry(it->v - 1) * len; add(it->v, len); b.pbk({it->v, len}); }
}
for (auto [v, len] : b) add(v, -len);
for (auto [l, r] : a) assign(l, r, c[y]);
return res;
}
int main()
{
read(n); vector<int> num(n); rep(i, 1, n) read(c[i]), num[i - 1] = c[i];
sort(num.begin(), num.end()); num.erase(unique(num.begin(), num.end()), num.end()); m = num.size();
rep(i, 1, n) c[i] = m - (lower_bound(num.begin(), num.end(), c[i]) - num.begin());
rep(i, 2, n) { int u, v; read(u, v); e[u].pbk(v); e[v].pbk(u); A[i] = u; B[i] = v; }
dfs1(1, 0); dfs2(1, 1);
rep(i, 1, n) st.insert(Node(dfn[i], dfn[i], c[i]));
rep(i, 2, n) { write(solve(A[i], B[i])); pc(0xa); }
return 0;
}
P14366 [JOISC 2018] 栅栏 / Fences - 洛谷
计算几何。咕咕咕~
P14367 [JOISC 2018] 帐篷 / Tents - 洛谷
显然一行如果放了两个那么会导致有两列不能放,列相同。
考虑枚举有
记
总答案为:
记:
显然
注意如果一个帐篷所在的行和列有且只有这一个帐篷,那么它有四种方法。
考虑最后一行的方法,转移
一种理解方式是,在最后一行,可以选择不放,即
预处理
const int maxn = 3e3 + 5, mod = 1e9 + 7;
int n, m, N, f[maxn][maxn], h[maxn], fact[maxn], invf[maxn], ans; // h[x] = \binom{2x}{2,2,...,2}
inline void madd(int& x, const int& y) { x+= y; (x >= mod) && (x-= mod); }
inline ll qpow(ll x, ll y) { ll z = 1; for(; y; y>>= 1, x = x * x % mod) if (y & 1) z = z * x % mod; return z; }
inline ll comb(int x, int y) { if (y < 0 || y > x) return 0; if (y == 0 || y == x) return 1; return (ll)fact[x] * invf[x - y] % mod * invf[y] % mod; }
void init()
{
fact[0] = 1; rep(i, 1, N) fact[i] = (ll)fact[i - 1] * i % mod;
invf[N] = qpow(fact[N], mod - 2); fep(i, N, 1) invf[i - 1] = (ll)invf[i] * i % mod;
h[0] = 1; rep(i, 1, N / 2) h[i] = (ll)fact[2 * i] * qpow(qpow(2, i), mod - 2) % mod;
}
int main()
{
read(n, m); N = max(n, m); ans = mod - 1; init();
rep(i, 0, n) f[i][0] = 1; rep(j, 0, m) f[0][j] = 1;
rep(i, 1, n) rep(j, 1, m) madd(f[i][j], (f[i - 1][j] + 4ll * j * f[i - 1][j - 1] % mod) % mod);
for (int x = 0; x <= n && 2 * x <= m; ++x)
{
int ter1 = comb(n, x) * comb(m, 2 * x) % mod * h[x] % mod;
for (int y = 0; 2 * x + y <= m && x + 2 * y <= n; ++y)
{
int ter2 = comb(m - 2 * x, y) * comb(n - x, 2 * y) % mod * h[y] % mod;
madd(ans, (ll)ter1 * ter2 % mod * f[n - x - 2 * y][m - y - 2 * x] % mod);
}
}
write(ans); pc(0xa);
return 0;
}
P14368 [JOISC 2018] 修行 / Asceticism - 洛谷
观察到如果
设
考虑如何求
因此断言第
考虑有用的不同的
时间复杂度
const int maxn = 5e5 + 5, LG = 25;
int n, q; ll d[maxn], f[maxn];
ll R[maxn], F[LG], cnt[LG], tot;
int main()
{
read(n, q); rep(i, 1, n) read(d[i]);
f[0] = 1;
rep(i, 1, n) f[i] = f[i - 1] > d[i] ? f[i - 1] : ceil((db)d[i] / f[i - 1]) * f[i - 1];
rep(i, 0, n) (i == 0 || f[i] != f[i - 1]) ? F[++tot] = f[i], R[tot] = -i, cnt[tot] = 1 : ++cnt[tot];
while (q--)
{
ll t, ql, qr, res = 0; read(t, ql, qr);
rep(i, 1, tot)
{
ll r = R[i] + t / F[i] * F[i], l = r - cnt[i] + 1;
chkmn(r, qr); chkmx(l, ql); res+= max(r - l + 1, 0ll);
}
write(res); pc(0xa);
}
return 0;
}
P14371 [JOISC 2018] 航空路线图 / Airline Route Map - 洛谷
通信题。咕咕咕~
P14372 [JOISC 2018] 比太郎的聚会 / Bitaro's Party - 洛谷
单次询问
考虑
复杂度
const int maxn = 1e5 + 5, B = 200; // B 取小点防止爆空间
int n, m, q; vector<int> ver[maxn];
int a[maxn][B + 5], f[maxn][B + 5]; pii tmp[B + 5];
int t, c[maxn], vis[maxn], tot;
inline void merge(int u, int v)
{
int p = 0, p1 = 1, p2 = 1, ter1, ter2;
while (a[u][p1] && a[v][p2] && p < B)
{
ter1 = f[u][p1], ter2 = f[v][p2] + 1;
if (ter1 >= ter2) tmp[++p] = {a[u][p1++], ter1}, vis[tmp[p].fst] = 1;
else tmp[++p] = {a[v][p2++], ter2}, vis[tmp[p].fst] = 1;
while (a[u][p1] && vis[a[u][p1]]) ++p1;
while (a[v][p2] && vis[a[v][p2]]) ++p2;
}
while (a[u][p1] && p < B) { if (!vis[a[u][p1]]) tmp[++p] = {a[u][p1], f[u][p1]}, vis[tmp[p].fst] = 1; ++p1; }
while (a[v][p2] && p < B) { if (!vis[a[v][p2]]) tmp[++p] = {a[v][p2], f[v][p2] + 1}, vis[tmp[p].fst] = 1; ++p2; }
rep(i, 1, p) a[u][i] = tmp[i].fst, f[u][i] = tmp[i].sec, vis[tmp[i].fst] = 0;
}
namespace Sub1 // Y < B
{
inline void work()
{
rep(i, 1, B) if (a[t][i] && !vis[a[t][i]]) return write(f[t][i]), pc(0xa), void(); puts("-1");
}
}
namespace Sub2 // Y >= B
{
int g[maxn];
inline void work()
{
rep(i, 1, n) g[i] = vis[i] ? -1 : 0;
rep(i, 1, n) for (int j : ver[i]) if (~g[j]) chkmx(g[i], g[j] + 1);
if (~g[t]) write(g[t]), pc(0xa); else puts("-1");
}
}
int main()
{
read(n, m, q); rep(i, 1, m) { int u, v; read(u, v); ver[v].pbk(u); }
rep(u, 1, n) a[u][1] = u;
rep(u, 1, n) for (int v : ver[u]) merge(u, v);
while (q--)
{
read(t, tot);
rep(i, 1, tot) read(c[i]), vis[c[i]] = 1;
(tot < B) ? Sub1::work() : Sub2::work();
rep(i, 1, tot) vis[c[i]] = 0;
}
return 0;
}
P14373 [JOISC 2018] 安全门 / Security Gate - 洛谷
思路有点长,感觉以本人的语文水平写出来会是一坨。直接去看 官方题解 吧,写的很清晰了。
大概就是对每个有贡献的
const int maxn = 305, mod = 1e9 + 7;
inline void madd(int& x, const int& y) { x+= y; (x >= mod) && (x-= mod); }
inline void mdec(int& x, const int& y) { x-= y; (x < 0) && (x-= mod); }
int n, c[maxn], ans; char s[maxn];
namespace Sub1
{
int dp[maxn][maxn];
inline int calc()
{
mems(dp, 0); dp[0][0] = 1;
rep(i, 0, n - 1)
{
if (c[i] >= 0) rep(j, 0, n - 1) madd(dp[i + 1][j + 1], dp[i][j]);
if (c[i] <= 0) rep(j, 1, n) madd(dp[i + 1][j - 1], dp[i][j]);
}
return dp[n][0];
}
}
namespace Sub2
{
int g1[maxn / 2][maxn][2][maxn];
int g2[maxn][maxn][2][maxn];
int f[maxn][maxn];
inline int calc1(int a, int x)
{
int res = 0;
for (int len = 0; len + 1 <= n; len+= 2) if (c[len] <= 0) madd(res, (ll)g1[x][len][1][0] * f[len + 1][2 * a - 1] % mod);
return res;
}
inline int calc2(int a, int x)
{
int res = 0;
for (int len = 0; len + 1 <= n; len+= 2) if (c[len] <= 0) madd(res, (ll)g1[x][len][1][0] * g2[a + x][len + 1][1][2 * a - 1] % mod);
return res;
}
inline int calc(int a, int x) { return (a <= x) ? calc1(a, x) : calc2(a, x); }
void init1()
{
mems(g1, 0);
rep(x, 0, n / 2 - 1)
{
g1[x][0][x == 0][0] = 1;
rep(i, 0, n - 1)
{
if (c[i] >= 0)
{
rep(j, 0, x - 1) madd(g1[x][i + 1][j + 1 == x][j + 1], g1[x][i][0][j]);
rep(j, 0, x - 1) madd(g1[x][i + 1][1][j + 1], g1[x][i][1][j]);
}
if (c[i] <= 0)
{
rep(j, 1, x) madd(g1[x][i + 1][0][j - 1], g1[x][i][0][j]);
rep(j, 1, x) madd(g1[x][i + 1][1][j - 1], g1[x][i][1][j]);
}
}
}
}
void init2()
{
mems(g2, 0);
rep(x, 0, n - 1)
{
g2[x][n][x == 0][0] = 1;
fep(i, n - 1, 0)
{
if (c[i] <= 0)
{
rep(j, 0, n - 1) madd(g2[x][i][0][j + 1], g2[x][i + 1][0][j]);
if (x > 0) madd(g2[x][i][1][x], g2[x][i + 1][0][x - 1]);
rep(j, x, min(x * 2, n) - 1) madd(g2[x][i][1][j + 1], g2[x][i + 1][1][j]);
}
if (c[i] >= 0)
{
rep(j, 1, n - 1) madd(g2[x][i][0][j - 1], g2[x][i + 1][0][j]);
madd(g2[x][i][1][x], g2[x][i + 1][0][x + 1]);
rep(j, x + 2, min(x * 2, n - 1)) madd(g2[x][i][1][j - 1], g2[x][i + 1][1][j]);
}
}
}
}
void init3()
{
mems(f, 0); f[n][0] = 1;
fep(i, n - 1, 0)
{
if (c[i] <= 0) rep(j, 0, n - 1) madd(f[i][j + 1], f[i + 1][j]);
if (c[i] >= 0) rep(j, 1, n - 1) madd(f[i][j - 1], f[i + 1][j]);
}
}
inline int solve()
{
init1(); init2(); init3();
int res = 0; rep(a, 1, n / 2) rep(x, 0, n / 2 - a) madd(res, calc(a, x)); return res;
}
}
namespace Sub3
{
int g1[maxn / 2][maxn][2][maxn];
int g2[maxn / 2][maxn][3][maxn * 2];
inline int calc(int a, int x)
{
int res = 0;
for (int len = 0; len + 1 <= n; len+= 2) if (c[len] <= 0) madd(res, (ll)g1[x][len][1][0] * g2[a + x][len + 1][2][a * 2 - 1 + n] % mod);
return res;
}
void init1()
{
mems(g1, 0);
rep(x, 0, n / 2 - 1)
{
g1[x][0][x == 0][0] = 1;
rep(i, 0, n - 1)
{
if (c[i] >= 0)
{
rep(j, 0, x - 1) madd(g1[x][i + 1][j + 1 == x][j + 1], g1[x][i][0][j]);
rep(j, 0, x - 1) madd(g1[x][i + 1][1][j + 1], g1[x][i][1][j]);
}
if (c[i] <= 0)
{
rep(j, 1, x - 1) madd(g1[x][i + 1][0][j - 1], g1[x][i][0][j]);
rep(j, 1, x) madd(g1[x][i + 1][1][j - 1], g1[x][i][1][j]);
}
}
}
}
void init2(int flg)
{
mems(g2, 0);
rep(x, 0, n / 2 - 1)
{
g2[x][n][(x + flg) == 0][n] = 1;
fep(i, n - 1, 0)
{
if (c[i] <= 0)
{
rep(j, 0, n - 1) madd(g2[x][i][(j + 1) == (x + flg)][j + 1 + n], g2[x][i + 1][0][j + n]);
rep(j, 0, n - 1) madd(g2[x][i][1][j + 1 + n], g2[x][i + 1][1][j + n]);
rep(j, -n, min(x * 2, n) - 1) madd(g2[x][i][2][j + n + 1], g2[x][i + 1][2][j + n]);
}
if (c[i] >= 0)
{
rep(j, 1, n - 1) madd(g2[x][i][0][j + n - 1], g2[x][i + 1][0][j + n]);
rep(j, 0, n - 1) madd(g2[x][i][(j - 1) == -1 ? 2 : 1][j - 1 + n], g2[x][i + 1][1][j + n]);
rep(j, -n + 1, min(x * 2, n - 1)) madd(g2[x][i][2][j + n - 1], g2[x][i + 1][2][j + n]);
}
}
}
}
inline int solve()
{
int res = 0;
rep(flg, 0, 1)
{
init1(); init2(flg);
rep(a, -n / 2 + 1, n / 2 - 1) rep(x, max(0, -a), n / 2 - abs(a) - 1) madd(res, calc(a, x));
reverse(c, c + n); rep(i, 0, n - 1) c[i]*= -1;
}
return res;
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s; if (n & 1) return puts("0"), 0;
rep(i, 0, n - 1) c[i] = s[i] == '(' ? 1 : s[i] == ')' ? -1 : 0;
madd(ans, Sub1::calc());
rep(o, 0, 1) { madd(ans, Sub2::solve()); reverse(c, c + n); rep(i, 0, n - 1) c[i]*= -1; }
madd(ans, Sub3::solve());
write(ans); pc(0xa);
return 0;
}
P14374 [JOISC 2018] 糖果 / Candies - 洛谷
(反悔贪心裸题?)
直接链表+堆维护,每次取最大的,取
关于边界情况可以直接给
不知道为什么评紫。
const int maxn = 2e5 + 5; const ll inf = 1e18;
int n, l[maxn], r[maxn], vis[maxn], cnt; ll a[maxn], ans;
priority_queue<pli> pq;
int main()
{
read(n); rep(i, 1, n) read(a[i]);
rep(i, 1, n) l[i] = i - 1, r[i] = i + 1;
rep(i, 1, n) pq.push({a[i], i});
a[0] = a[n + 1] = -inf;
while (cnt < (n + 1) / 2)
{
int p = pq.top().sec; pq.pop(); if (vis[p]) continue;
ans+= a[p]; a[p] = a[l[p]] + a[r[p]] - a[p]; vis[l[p]] = vis[r[p]] = 1;
r[l[l[p]]] = p; l[r[r[p]]] = p; l[p] = l[l[p]]; r[p] = r[r[p]];
pq.push({a[p], p});
write(ans); pc(0xa); ++cnt;
}
return 0;
}