跳到正文

个人记录

2024.10.22上午 补题

dreamnotfounddd约 15 分钟
原文
文章信息与版本

文章信息

发布
2024年10月22日
收录
2026年10月6日
更新
2026年10月6日
篇幅
约 15 分钟

版本

  1. v1当前

注* : 本题解包含桌面国王,乌鸦喝水,次数,区间,树的世界一共五道题tm的累死我了

桌面国王

贪心:

如果两个格子在同一行,那么路径一定是“长城”形状的,但是贪心细节很多

我们也可以考虑合并:

因为:这个区间具有可加性。

给定一个矩形,我们维护0,0,0,1,1,0,1,1这四个状态,表示:

  1. 左上角到右上角的最短路;

  2. 左上角到右下角的最短路;

  3. 左下角到右上角的最短路;

  4. 左下角到右下角的最短路。

然后就可以将两个区间O(1)O(1)合并求解了。

因此,我们需要一种可以维护区间合并的数据结构:显然可以用线段树和stst表。

这道题考查了线段树和stst表等的根本思想,是一道不错的题。

code

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
const int s=1e9;
int n,q,a[2][maxn],x,y;
char ccccc;
struct Node{
	int l,r,uu,ud,du,dd;
	friend Node operator +(Node x,Node y){
		Node n={x.l,y.r,s,s,s,s};
		if(x.uu!=s && y.uu!=s)n.uu=x.uu+y.uu+1;
		if(x.ud!=s && y.du!=s)n.uu=min(n.uu,x.ud+y.du+1);
		if(x.dd!=s && y.dd!=s)n.dd=x.dd+y.dd+1;
		if(x.du!=s && y.ud!=s)n.dd=min(n.dd,x.du+y.ud+1);
		if(x.uu!=s && y.ud!=s)n.ud=x.uu+y.ud+1;
		if(x.ud!=s && y.dd!=s)n.ud=min(n.ud,x.ud+y.dd+1);
		if(x.dd!=s && y.du!=s)n.du=x.dd+y.du+1;
		if(x.du!=s && y.uu!=s)n.du=min(n.du,x.du+y.uu+1);
		return n;
	}
	friend bool operator ==(Node x,Node y){
		return (x.l==y.l && x.r==y.r && x.uu==y.uu && x.ud==y.ud && x.du==y.du && x.dd==y.dd);
	}
}t[maxn*4];
void build(int rt,int l,int r){
	if(l==r){
		if(a[0][l] && a[1][l])t[rt]={l,r,s,s,s,s};
		else if(a[0][l] && !a[1][l])t[rt]={l,r,s,s,s,0};
		else if(a[1][l] && !a[0][l])t[rt]={l,r,0,s,s,s};
		else t[rt]={l,r,0,1,1,0};
	}else{
		int mid=(l+r)/2;
		build(rt*2,l,mid);
		build(rt*2+1,mid+1,r);
		t[rt]=t[rt*2]+t[rt*2+1];
	}
//	cout << rt << "(" << t[rt].l << "," << t[rt].r << "):" << t[rt].uu << " " << t[rt].ud << " " << t[rt].du << " " << t[rt].dd << "\n";
}
Node query(int rt,int l,int r){
	int ll=t[rt].l,rr=t[rt].r;
	if(l<=ll && rr<=r)return t[rt];
	int mid=ll+rr>>1;
	if(r<=mid)return query(rt*2,l,r);
	if(mid<l)return query(rt*2+1,l,r);
	return query(rt*2,l,r)+query(rt*2+1,l,r);
}
int main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);
//	cout.tie(0);
	cin >> n >> q;
	for(int i = 1;i<=n;i++){
		cin >> ccccc;
		if(ccccc=='X')a[0][i]=1;
	}
	for(int i = 1;i<=n;i++){
		cin >> ccccc;
		if(ccccc=='X')a[1][i]=1;
	}
	build(1,1,n);
	while(q--){
		cin >> x >> y;
		int xx=0,yy=0;
		if(x>n)x-=n,xx=1;
		if(y>n)y-=n,yy=1;
		if(x>y){
			swap(x,y);
			swap(xx,yy);
		}
		if(xx && yy){
			Node ans=query(1,x,y);
			if(ans.dd==s)cout << "-1\n";
			else cout << ans.dd << "\n";
		}else if(!xx && yy){
			Node ans=query(1,x,y);
			if(ans.ud==s)cout << "-1\n";
			else cout << ans.ud << "\n";
		}else if(xx && !yy){
			Node ans=query(1,x,y);
			if(ans.du==s)cout << "-1\n";
			else cout << ans.du << "\n";
		}else{
			if(x>y)swap(x,y);
			Node ans=query(1,x,y);
			if(ans.uu==s)cout << "-1\n";
			else cout << ans.uu << "\n";
		}
	}
	return 0;
}

乌鸦喝水

注* : 这种题尽量先看部分分和暴力,再想正解

正解:

设a,b,ca,b,c为长宽高

发现c=hnm−1nm−abc = \frac{hnm - 1}{nm-ab}

于是,我只需要知道枚举好的矩形是什么样子,就可以知道a,b,ha,b,h了。

考虑压缩枚举

枚举矩阵上下界,然后压缩为一维。

因为我们想要最小深度最大化,面积最大化,就可以把面积转换成长度了。

于是可以用单调栈维护。

此时这道题就相当于枚举的a,ha,h,计算出了最大的bb

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=503;
int n,m,a,b,c;
int f[maxn],d[maxn][maxn];
int u[maxn],v[maxn],w[maxn];
int t,x,y,z,l;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> a >> b >> m >> n;
	for(int i = 1;i<=m;i++)
		for(int j = 1;j<=n;j++)
			cin >> d[i][j];
	if(a<b)swap(a,b);
	for(int i = 1;i<=m;i++){
		for(int j = 1;j<=n;j++)f[j]=d[i][j];
		for(int j = i;j<=m;j++){
			for(int k = 1;k<=n;k++)f[k]=min(f[k],d[j][k]);
			memset(u,0,sizeof u);
			memset(v,0,sizeof v);
			memset(w,0,sizeof w);
			t = 0;
			for(int k = n;k>=1;k--){
				while(t>0 && f[u[t]]>=f[k])t--;
				if(t==0)x = n;
				else x = u[t]-1;
				u[++t] = k;
				w[k] = x;
			}
			t = 0;
			for(int k = 1;k<=n;k++){
				while(t>0 && f[v[t]]>=f[k])t--;
				if(t==0)x=0;
				else x=v[t];
				v[++t]=k,y=(w[k]-x),l=j-i+1;
				if(l<=b)y=min(y,a),z=(f[k]*n*m-1)/(n*m-l*y),c=max(c,z*y*l);
				else if(l<=a)y=min(y,b),z=(f[k]*n*m-1)/(n*m-l*y),c=max(c,z*y*l);
			}
		}
	}
	cout << c << "\n";
	return 0;
}

次数(CKW day2 T2)

开一个vector数组当作桶,在上面二分即可

code

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+7;
int n,q,a[maxn];
int o,l,r,k;
vector<int> v[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> q;
	for(int i = 1;i<=n;i++){
		cin >> a[i];
		v[a[i]].push_back(i);
	}
//	for(int i = 1;i<=3;i++){
//		for(int j : v[i])cout << j << " ";
//		cout << "\n";
//	}
	while(q--){
		cin >> o;
		if(o==1){
			cin >> l >> r >> k;
			auto x = lower_bound(v[k].begin(),v[k].end(),l);
			auto y = upper_bound(v[k].begin(),v[k].end(),r);
			cout << y-x << "\n";
		}else{
			cin >> k;
			if(a[k]==a[k+1])continue;
			auto x = lower_bound(v[a[k]].begin(),v[a[k]].end(),k);
			auto y = lower_bound(v[a[k+1]].begin(),v[a[k+1]].end(),k+1);
			swap(*x,*y);
			swap(a[k],a[k+1]);
		}
	}
	return 0;
}

区间(CKW day1 T3)

问题转化为求尽可能大的∑l,rai−k>=0\sum_{l,r} {a_i-k} >= 0

于是可以O(n2)O(n^2)暴力

然后发现可能使用的前缀和一定具有单调性,于是可以二分O(nm∗log2n)O(nm * log_2 n),这种做法常数好可以100pts,不好也可以80pts

又发现两个做差的前缀和都有单调性,于是单调栈+双指针即可O(nm)O(nm)拿下AC

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=1e6+7;
int n,m,a[maxn],k,ans,s[maxn],l[maxn],r[maxn];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i<=n;i++)cin >> a[i];
	while(m--){
		int L = 1,lt = 0,rt = 0,p = 0,ans=0;
		ans=0;
		cin >> k;
		for(int i = 1;i<=n;i++)s[i]=s[i-1]+a[i]-k;
		for(int i = 1;i<=n;i++){
			while(rt && s[i]>=s[r[rt]])rt--;
			r[++rt]=i;
		}
		for(int R,i = 1;i<=rt;i++){
			for(R = r[i];L<=R;L++)if(s[L]<s[l[lt]])l[++lt]=L;
			while(p<=lt && s[R]<s[l[p]])p++;
			if(s[R]>=s[l[p]])ans=max(ans,R-l[p]);
		}
		cout << ans << '\n';
	}
	return 0;
}

树的世界

显然可以DP

设fu,0/1f_{u,0/1}表示根为uu的子树中选或不选uu的最大边数。

fu,0=∑max(fv,0,fv,1)f_{u,0} = \sum max(f_{v,0},f_{v,1})

fu,1=Max(fu,0−max(fv,0,fv,1)+fv,0+1)f_{u,1} = Max(f_{u,0}-max(f_{v,0},f_{v,1})+f_{v,0}+1)

设gg为方案数

gu,0=∏vhvg_{u,0} = \prod\limits_v h_v

当fu,1f_{u,1}是由fv,0f_{v,0}和fv,1f_{v,1}转一过来

也即fu,1+max(fv,0+fv,1)==fu,0+fv,0+1f_{u,1}+max(f_{v,0}+f_{v,1})==f_{u,0}+f_{v,0}+1时

就有gu,1=∑gu,0÷hv×gv,0g_{u,1} = \sum {g_{u,0} \div h_v \times g_{v,0}}

注* : hv=(fv,0>=fv,1)×gv,0+(fv,0<=fv,1)×gv,1h_v = (f_{v,0}>=f_{v,1}) \times g_{v,0}+(f_{v,0}<=f_{v,1}) \times g_{v,1}

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=1e3+7;
const int mod = 1e9+7;
int n,g[maxn][2],f[maxn][2],a[maxn],s;
vector<int> G[maxn];
int Pow(int x,int y){
	int ans = 1;
	while(y){
		if(y&1)ans=ans*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ans;
}//zhanghx0219 is lesbian
void dfs1(int u,int fa){
	for(int v : G[u]){
		dfs1(v,u);
		f[u][0]+=max(f[v][0],f[v][1]);
	}
	for(int v : G[u])
		f[u][1]=max(f[u][1],f[u][0]-max(f[v][0],f[v][1])+f[v][0]+1);
}
int h(int v){
	int x = 0;
	if(f[v][0]>=f[v][1])x=g[v][0];
	if(f[v][0]<=f[v][1])x=(x+g[v][1])%mod;
	return x;
}
void dfs2(int u,int fa){
	g[u][0]=1;
	for(int v : G[u]){
		dfs2(v,u);
		g[u][0]=(g[u][0]*h(v))%mod;
	}
	for(int v : G[u])
		if(f[u][0]+f[v][0]+1==f[u][1]+max(f[v][0],f[v][1]))
			g[u][1]+=g[u][0]%mod*Pow(h(v),mod-2)%mod*g[v][0]%mod,g[u][1]%=mod;
//	for(int v : G[u])
//		g[u][1]=(0ll+g[u][1]+1ll*g[v][0]*h(v)%mod*1ll*g[v][0]%mod)%mod;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int x,m,u,i = 1;i<=n;i++){
		cin >> u >> m;
		while(m--){
			cin >> x;
			G[u].push_back(x);
			a[x]=u;
		}
	}
	for(int i = 1;i<=n;i++)if(a[i]==0)s=i;
	dfs1(s,0);
	dfs2(s,0);
	cout << max(f[s][0],f[s][1]) << '\n' << h(s) << '\n';
	return 0;
}

吐槽:这TM一上午干了NM四道绿蓝题我要疯了艹

原文