2025-11-13~15 hetao1733837的刷题记录

2025-11-13~15 hetao1733837的刷题记录

11-13

[JOISC 2014]Water Bottle

原题链接1:[P14422 [JOISC 2014] 水桶 / Water Bottle]([P14422 JOISC 2014] 水桶 / Water Bottle - 洛谷)

原题链接2:#2876. 「JOISC 2014 Day2」水壶

洛谷题名怎么和LOJ不一样啊/(ㄒoㄒ)/~~

分析

呃,不知从哪冒出了瓶颈最短路这个词,被mhh认为是正确的,因此引发了瓶颈生成树(整张图),因为由定义“无向图$G$的瓶颈生成树是这样的一个生成树,它的最大的边权值在$G$的所有生成树中最小。”而瓶颈生成树又是最小生成树,所以转化为原图求最小生成树,两点之间距离转化为树上路径,可以直接建$Kruskal$重构树(最小生成树多维护一下最大两点距离而已)跑倍增,斜二倍增甚至树剖都可以。但是问题在于$O(n^{2})$建图是无法接受的,因为原图是个矩阵……线段树优化建图,?可行但是吃多了。看一眼$tag$,惊奇发现$bfs$,容我稍考……多源$bfs$即可?好吧,我基础是一坨……

那么,就可以写代码了?我真是吃多了……

正解

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int HW = 2005, P = 400005;
char c[HW][HW];
pair<int, int> vis[HW][HW];
struct node{
	int a, b;
}inp[P];
int h, w, p, q;
int dx[] = {1, 0, -1, 0},
	dy[] = {0, -1, 0, 1};
int fa[P * 2]; 
int getfa(int x){
	return x == fa[x] ? x : fa[x] = getfa(fa[x]);
}
struct node1{
	int u, v, w;
}e[HW * HW];
bool cmp(node1 o1, node1 o2){
	return o1.w < o2.w;
}
vector<int> g[P * 2]; 
int we[P * 2], de[P * 2]; 
pair<int, int> f[P * 2][21]; 
void dfs(int u){
	for (int i = 1; i <= 20; i++){
		f[u][i].first = f[f[u][i - 1].first][i - 1].first;
		f[u][i].second = max(f[u][i - 1].second, f[f[u][i - 1].first][i - 1].second);
	}
	for (auto v : g[u]){
		f[v][0].first = u;
		f[v][0].second = we[v];
		de[v] = de[u] + 1;
		dfs(v);
	}
	return ;
}
int ans;
void lca(int x, int y){
	ans = 0;
	if (de[x] < de[y])
		swap(x, y);
	for (int i = 20; i >= 0; i--){
		if (de[f[x][i].first] >= de[y]){
			ans = max(ans, f[x][i].second);
			x = f[x][i].first;
		}
	} 
	if (x == y){
		return ;
	}
	else{
		for (int i = 20; i >= 0; i--){
			if (f[x][i].first != f[y][i].first){
				ans = max(ans, f[x][i].second);
				ans = max(ans, f[y][i].second);
				x = f[x][i].first;
				y = f[y][i].first;
			}
		}
		ans = max(ans, we[x]);
		ans = max(ans, we[y]);
		ans = max(ans, we[f[x][0].first]);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//input
	cin >> h >> w >> p >> q;
	for (int i = 1; i <= h; i++){
		string s;
		cin >> s;
		for (int j = 0; j < w; j++){
			c[i][j + 1] = s[j];
		}
	}
	for (int i = 1; i <= h; i++){
		for (int j = 1; j <= w; j++){
			vis[i][j] = {0, 0};
		}
	}
	queue<pair<int, int>> qu;
	for (int i = 1; i <= p; i++){
		cin >> inp[i].a >> inp[i].b;
		vis[inp[i].a][inp[i].b] = {0, i};
		qu.push({inp[i].a, inp[i].b});
	}
	//bfs
	int top = 0;
	while (!qu.empty()){
		int x = qu.front().first;
		int y = qu.front().second;
		qu.pop();
		for (int i = 0; i < 4; i++){
			int tx = x + dx[i], ty = y + dy[i];
			if (tx >= 1 && tx <= h && ty >= 1 && ty <= w && c[tx][ty] == '.'){
				if (!vis[tx][ty].second){
					vis[tx][ty] = {vis[x][y].first + 1, vis[x][y].second};
					qu.push({tx, ty});
				}
				else if (vis[x][y].second != vis[tx][ty].second){
					e[++top] = {vis[x][y].second, vis[tx][ty].second, vis[x][y].first + vis[tx][ty].first};
				}
			}
		}
	}
	//Kruskal
	for (int i = 1; i < p; i++)
		e[++top] = {i, i + 1, 0x3f3f3f3f3f3f3f3f};
	sort(e + 1, e + top + 1, cmp);
	int cur = p;
	for (int i = 1; i <= p * 2; i++)
		fa[i] = i;
	for (int i = 1; i <= top; i++){
		int x = e[i].u, y = e[i].v, z = e[i].w;
		int fx = getfa(x), fy = getfa(y);
		if (fx == fy)
			continue;
		cur++;
		g[cur].push_back(fx);
		g[cur].push_back(fy);
		fa[fx] = fa[fy] = cur; 
		we[cur] = z;
	}
	for (int i = 1; i <= cur; i++){
		for (int j = 0; j <= 20; j++){
			f[i][j] = {0, 0};
		}
	}
	de[cur] = 1;
	f[cur][0] = {cur, we[cur]};
	dfs(cur);
	//solve
	while (q--){
		int x, y;
		cin >> x >> y;
		if (x == y){
			cout << 0 << '\n';
			continue;
		}
		ans = 0;
		lca(x, y);
		if (ans == 0x3f3f3f3f3f3f3f3f)
			cout << -1 << '\n';
		else
			cout << ans << '\n';
	}
	return 0;
}

吃饱了……

[JOISC 2014] Making Friends is Fun

原题链接1:[JOISC 2014] 有趣的交朋友 / Making Friends is Fun

原题链接2:「JOISC 2014 Day2」交朋友

分析

利用人类智慧一下就模拟出来了,哈哈哈哈哈哈哈哈哈……计算机,靠你了……靠不住/(ㄒoㄒ)/~~

像这样一个点指向两外两个点,有点像树啊……加上边像基环树?我在胡言乱语……问一下……$tag$里给出了并查集,给我一些恍惚的启发……难道就是这样的“有向三角”?Oh,我有了一些思路,额外记录每个点的入度和出度,然后对于没有入度的点,对其出度进行排列组合,呃,好像还要做一些去重,这些是$tag$里的$bfs$吗?我需要询问。

呃,花花的思路大概是打标记打标记,然后啊吧啊吧啊吧啊,我也并非深刻理解。

但是,aoao的很具启发性,是一种很数学的做法,类似排列组合,虽然并未理解精髓。

正解

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
int n, m, a, b;
vector<int> e[N];
int fa[N], sz[N];
int getfa(int x){
    return x == fa[x] ? fa[x] : fa[x] = getfa(fa[x]);
}
void merge(int x, int y){
    x = getfa(x);
    y = getfa(y);
    if (x != y){
        if (sz[x] > sz[y])
            swap(x, y);
        fa[x] = y;
        sz[y] += sz[x];
    }
}
void dfs(int u){
    for (auto v : e[u]){
        if (getfa(u) == getfa(v))
            continue;
        merge(u, v);
        dfs(v);
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        cin >> a >> b;
        e[a].push_back(b);
    }
    for (int i = 1; i <= n; i++){
        fa[i] = i;
        sz[i] = 1;
    }
    for (int i = 1; i <= n; i++){
        int tmp = 0;
        for (auto v : e[i]){
            if (!tmp){
                tmp = v;
                continue;
            }
            merge(tmp, v);
            tmp = v;
        }
    }
    for (int i = 1; i <= n; i++)
        if (sz[getfa(i)] >= 2)
            dfs(i);
    int ans = 0;
    for (int i = 1; i <= n; i++){
        if (getfa(i) == i)
            ans += sz[i] * (sz[i] - 1);
        for (auto v : e[i]){
            if (getfa(v) == getfa(i)){
                continue;
            }
            ans++;
        }
    }
    cout << ans;
}

花花思路也很巧妙!对于两个点,如果连了双向边,那么就可以直接合并它们的儿子,对于一些还没有合并儿子的,直接合并然后递归儿子即可,那么打一下标记即可。代码我会(will)写吗?

11-14

[CSP-S 2023] 结构体

原题链接:[CSP-S 2023] 结构体

分析

吃饭前写了$op1$和$op2$两个,交了一发全WA了。

${\color{Black}{16:20}}$ 不会后两个,感觉要额外记录,初觉不对。决定找AI把前两个修一下,瞅一眼题解。

${\color{Black}{16:35}}$ 吃不动了,决定he题解。发现有一篇思路十分接近。

wcnm,为啥题解是错的!!!

™不写了。

[CSP-S 2025] 道路修复 / road

原题链接:[CSP-S 2025] 道路修复 / road

分析

考场上在仅剩的40分钟里想到了最小生成树,结果,忘了板子,以为赛季结束了/(ㄒoㄒ)/~~

正解

#include <bits/stdc++.h>
using namespace std;
const int N = 2000005, M = 10000005;
int n, m, k;
int fa[N];
struct node{
    int u, v, w;
}e[M], g[N];
int c[N];
bool vis[N];
bool cmp(node x, node y){
    return x.w < y.w;
}
int getfa(int x){
    return fa[x] == x ? x : fa[x] = getfa(fa[x]);
}
int main(){
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++){
        cin >> e[i].u >> e[i].v >> e[i].w;
    } 
    sort(e + 1, e + m + 1, cmp);
    for (int i = 1; i <= n; i++)
        fa[i] = i;
    int cnt = 0;
    for (int i = 1; i <= m; i++){
        int fu = getfa(e[i].u), fv = getfa(e[i].v);
        if (fu == fv) 
            continue;
        fa[fu] = fv;
        g[++cnt] = e[i];
        if (cnt == n - 1) 
            break;
    }
    cnt = m;
    for (int i = 1; i <= k; i++){
        cin >> c[i];
        for (int j = 1; j <= n; j++){
            cnt++;
            e[cnt].u = j;
            e[cnt].v = n + i;
            cin >> e[cnt].w;
        }
    }
    sort(e + 1, e + cnt + 1, cmp);
    long long ans = 0x3f3f3f3f3f3f3f3f;
    for (int mask = 0; mask < (1 << k); mask++){
        long long cur = 0;
        int num = 0;
        for (int j = 0; j < k; j++){
            if (mask >> j & 1){
                num++;
                cur += c[j + 1];
            }
        }
        for (int i = 1; i <= n + k; i++)
            fa[i] = i;
        int need = n + num - 1;
        for (int i = 1; i <= cnt; i++){
            int u = e[i].u, v = e[i].v;
            if (v > n) {
                int id = v - n;
                if (!(mask >> (id - 1) & 1)) 
                    continue;
            }
            int fu = getfa(u), fv = getfa(v);
            if (fu == fv) 
                continue;
            fa[fu] = fv;
            cur += e[i].w;
            need--;
            if (need == 0) 
                break;
        }
        if (need == 0)
            ans = min(ans, cur);
    }
    cout << ans;
    return 0;
}

两遍$Kruskal$,行吧……我是sugar这启示我们,$\color{Black}{多重限制,时间复杂度允许,就多跑几层,分层的思想还是很常用的。}$

好吧,要去听烫烫选科讲座了,我不明白,竞赛班不是选物化生吗?反正我是这么打算的。毕竟文科从开学以来没咋听过(●'◡'●)

OSU!

原题链接:OSU!

分析

维护$x1$表示$x$的期望,$x2$表示$x^2$的期望。

$x1_i=(x1_{i-1}+1)\times p_i$

$x2_i=(x2_{i-1}+2\times x1_{i - 1} + 1)\times p_i$

$ans_i=ans_{i-1}+(3 \times x2_{i - 1}+ 3 \times x1_{i-1} + 1) \times p_i$

正解

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
double x1[N], x2[N], ans[N], p[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> p[i];
    }
    for (int i = 1; i <= n; i++){
        x1[i] = (x1[i - 1] + 1) * p[i];
        x2[i] = (x2[i - 1] + 2 * x1[i - 1] + 1) * p[i];
        ans[i] = ans[i - 1] + (3 * x2[i - 1] + 3 * x1[i - 1] + 1) * p[i];
    }
    cout << fixed << setprecision(1) << ans[n];
}

[JXOI2018] 游戏

原题链接:[JXOI2018]游戏

分析

设有$k$个关键点,把序列分成$k+1$段。对于每个非关键点,排在关键点后面概率为$P=\frac{1}{k+1}$,期望$E=(n-k)P=\frac{n-k}{k+1}$,最后一个关键点期望$E_n=\frac{k(n+1)}{k+1}$,最终答案$\frac{k}{k+1}(n+1)!$。

正解

#include <bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
const int N = 10000005;
int l, r;
bool vis[N];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> l >> r;
    int k = 0;
    for (int i = l; i <= r; i++){
        if (!vis[i]){
            k++;
            for (int j = i * 2; j <= r; j += i){
                vis[j] = 1;
            }
        }
    }
    int ans = k;
    for (int i = 1; i <= r - l + 2; i++){
        if (i != k + 1)
            ans = ans * i % mod;
    }
    cout << ans;
}

11.15

[JOISC 2014] 邮戳收集 / Collecting Stamps

原题链接1:Collecting Stamps

原题链接2:邮戳拉力赛

分析

$08点33分$ 上下行给我一种二分图的感觉,两站台之间打卡点,也就是说要蛇形走位?$unk$,那么转化为二分图的……(我还没想好)好困……woc,洛谷比赛开了,AeeE5x说T1是$mex$,不想打了/(ㄒoㄒ)/~~

$\color{Black}{11点51分}$ 这比赛是人?不打了,呱呱为啥过了T1,为啥我大样例RE了/ll

还是看《星轨》吧……

ber,我LOJ怎么亖了/ll

cao了,模拟赛10分。

好吧,我要吃了这题。

$\color{Black}{2025.11.16}$ LOJ活了/ll

并非二分图,但是与匹配联系紧密,考虑$DP$。设$f_{i,j}$表示考虑前$i$站,走了$j$次回头路的最小时间花费。

那么转移有:

$f_{i,j}=f_{i-1,j}+u_i+v_i+2jT$。

$f_{i,j}=f_{i-1,j}+e_i+d_i+2jT$,其中$j>0$。

$f_{i,j}=f_{i-1,j+1}+u_i+e_i+2jT$。

$f_{i,j}=f_{i-1,j-1}+d_i+v_i+2jT$,其中$j>0$。

$f_{i,j}=f_{i,j-1}+v_i+d_i$,其中$j>0$。

$f_{i,j}=f_{i,j+1}+u_i+e_i$,其中$n-1\ge j\ge 0$

答案即为$ans=f[n][0]+(n+1)\times T$

$\color{Black}{16:51}$ 连交四发,全部WA掉,开he题解。

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3010;
int n, T, dp[N][N];
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
	cout.tie(0);
    cin >> n >> T;
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        int u, v, e, d;
        cin >> u >> v >> d >> e;
        for (int j = 0; j <= n; j++)
            dp[i - 1][j] += 2 * T * j;
        for (int j = 0; j <= n; j++)
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + u + v);
        for (int j = 1; j <= n; j++)
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + d + e);
        for (int j = 1; j <= n; j++)
            dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + v + d);
        for (int j = 0; j < n; j++)
            dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + u + e);
        for (int j = 1; j <= n; j++)
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + v + d);
        for (int j = n - 1; j >= 0; j--)
            dp[i][j] = min(dp[i][j], dp[i][j + 1] + u + e);
    }
    cout << dp[n][0] + (n + 1) * T;
    return 0;
}

你说他shit吗?从码量上,并非,从思维上应该降绿?我的问题。

【模板】AC 自动机

原题链接:【模板】AC 自动机

正解

#include <bits/stdc++.h>
using namespace std;
const int N = 200005, V = 26, M = 2000005;
int reads(char *s){
	int t = 0;
	char c = getchar();
	while (c < 'a' || c > 'z')
		c = getchar();
	while (c >= 'a' && c <='z'){
		s[++t] = c - 'a';
		c = getchar();
	}
	return t;
}
int n, ncnt, child[N][V], fail[N], m, ans[N], res[N], indeg[N];
char s[M];
int rt;
vector<int> vec[N];
void init(){
	ncnt = rt = 1;
}
void ins(char *s, int n, int id){
	int cur = rt;
	for (int i = 1; i <= n; i++){
		if (!child[cur][s[i]])
			child[cur][s[i]] = ++ncnt;
		cur = child[cur][s[i]];
	}
	vec[cur].emplace_back(id);
}
queue<int> q;
void build(){
	for (int i = 0; i < V; i++){
		if (child[rt][i]){
			fail[child[rt][i]] = rt;
			q.push(child[rt][i]);
		} 
        else{
			child[rt][i] = rt;  
		}
	}
	while (!q.empty()){
		int u = q.front();
		q.pop();
		for (int i = 0; i < V; i++){
			if (child[u][i]){
				fail[child[u][i]] = child[fail[u]][i];
				q.push(child[u][i]);
			} else {
				child[u][i] = child[fail[u]][i];
			}
		}
	}
}
void query(char *s, int n){
	int cur = rt;
	for (int i = 1; i <= n; i++){
		cur = child[cur][s[i]];
		ans[cur]++;
	}
}
void solve(){
	for (int i = 1; i <= ncnt; i++){
		if (fail[i] != i) {  
			indeg[fail[i]]++;
		}
	}
	for (int i = 1; i <= ncnt; i++){
		if (indeg[i] == 0 && i != rt) {
			q.push(i);
		}
	}
	while (!q.empty()){
		int u = q.front();
		q.pop();
		if (fail[u] != u) {
			ans[fail[u]] += ans[u];
			indeg[fail[u]]--;
			if (indeg[fail[u]] == 0 && fail[u] != rt){
				q.push(fail[u]);
			}
		}
	}
	for (int i = 1; i <= ncnt; i++){
		for (int p : vec[i]){
			res[p] = ans[i];
		}
	}
}
int main(){
	init();
	cin >> n;
	for (int i = 1; i <= n; i++){
		m = reads(s);
		ins(s, m, i);
	}
	build();
	m = reads(s);
	query(s, m);
	solve();
	for (int i = 1; i <= n; i++)
		cout << res[i] << '\n';
	return 0;
}

不知道PYD1咋写的,十分扭曲,套了$bfs$序,我没抄全,加了个拓扑排序优化才过,我需要另一个干练一点的AC自动机版本。

AC 自动机(简单版)

原题链接:AC 自动机(简单版)

正解

#include <bits/stdc++.h>
using namespace std;
const int N = 1000005, V = 26, M = 2000005;
int reads(char *s){
	int t = 0;
	char c = getchar();
	while (c < 'a' || c > 'z')
		c = getchar();
	while (c >= 'a' && c <='z'){
		s[++t] = c - 'a';
		c = getchar();
	}
	return t;
}
int n, ncnt, child[N][V], fail[N], m, ans[N], res[N], indeg[N];
char s[M];
int rt;
vector<int> vec[N];
void init(){
	ncnt = rt = 1;
}
void ins(char *s, int n, int id){
	int cur = rt;
	for (int i = 1; i <= n; i++){
		if (!child[cur][s[i]])
			child[cur][s[i]] = ++ncnt;
		cur = child[cur][s[i]];
	}
	vec[cur].emplace_back(id);
}
queue<int> q;
void build(){
	for (int i = 0; i < V; i++){
		if (child[rt][i]){
			fail[child[rt][i]] = rt;
			q.push(child[rt][i]);
		} 
        else{
			child[rt][i] = rt;  
		}
	}
	while (!q.empty()){
		int u = q.front();
		q.pop();
		for (int i = 0; i < V; i++){
			if (child[u][i]){
				fail[child[u][i]] = child[fail[u]][i];
				q.push(child[u][i]);
			} else {
				child[u][i] = child[fail[u]][i];
			}
		}
	}
}
void query(char *s, int n){
	int cur = rt;
	for (int i = 1; i <= n; i++){
		cur = child[cur][s[i]];
		ans[cur]++;
	}
}
void solve(){
	for (int i = 1; i <= ncnt; i++){
		if (fail[i] != i) {  
			indeg[fail[i]]++;
		}
	}
	for (int i = 1; i <= ncnt; i++){
		if (indeg[i] == 0 && i != rt) {
			q.push(i);
		}
	}
	while (!q.empty()){
		int u = q.front();
		q.pop();
		if (fail[u] != u) {
			ans[fail[u]] += ans[u];
			indeg[fail[u]]--;
			if (indeg[fail[u]] == 0 && fail[u] != rt){
				q.push(fail[u]);
			}
		}
	}
	for (int i = 1; i <= ncnt; i++){
		for (int p : vec[i]){
			res[p] = ans[i];
		}
	}
}
int main(){
	init();
	cin >> n;
	for (int i = 1; i <= n; i++){
		m = reads(s);
		ins(s, m, i);
	}
	build();
	m = reads(s);
	query(s, m);
	solve();
	int cnt = 0;
    for (int i = 1; i <= n; i++)
        if (res[i])
            cnt++;
    cout << cnt;
	return 0;
}

经过对PYD1代码稍加的修改,得到神秘代码通过此题。

posted on 2025-11-16 17:17  hetao1733837  阅读(0)  评论(0)    收藏  举报

导航