2022杭电多校联赛第六场 题解

比赛传送门
作者: fn


基本题

1006题 Maex / 最大MEX

题目大意
给定一棵 n n n 个结点的树,在每个结点标上 0 , 1 , . . . , n − 1 0,1,...,n-1 0,1,...,n1 的权重。
求每个子树mex之和的最大值。

考察内容
树形dp

分析

状态:
f [ i ] f[i] f[i] : 结点 i i i 的答案。
s z [ i ] sz[i] sz[i] : 结点 i i i 的子树大小。

边界:
叶子 f [ i ] = 1 f[i]=1 f[i]=1

转移:
从根节点开始跑dfs。

f[x]=max(f[x],dfs3(a1,x)+sz[x]);

完整代码:

#include<bits/stdc++.h>
#define ll long long
#define cer(x) cerr<<(#x)<<" = "<<(x)<<'\n'
#define endl '\n'
#define int long long
using namespace std;
ll read(ll &n){
	char ch=' '; ll q=0,w=1;
	for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
	if(ch=='-')w=-1,ch=getchar();
	for(;ch>='0'&&ch<='9';ch=getchar())q=q*10+ch-48;
	n=q*w; return n;
}
const int N=5e5+10;
ll n,m;

vector<ll> g[N];
ll sz[N]; // 子树大小 
ll f[N]; // 当前结点的答案 

int dfs2(int x,int fa){ // 统计子树大小
	sz[x]=1; // 至少为1 
	for(auto a1:g[x]){
		if(a1==fa)continue;
		
		sz[x]+=dfs2(a1,x); 
	}
	return sz[x];
}

int dfs3(int x,int fa){ // 转移 
	f[x]=1; 
	for(auto a1:g[x]){
		if(a1==fa)continue;
		
		f[x]=max(f[x],dfs3(a1,x)+sz[x]); // 转移
	}
	return f[x];
}

signed main(){ // AC
	int t; 
	read(t);
	while(t--){
		read(n);
		memset(sz,0,sizeof(sz[0])*(n+1));
		memset(f,0,sizeof(f[0])*(n+1));	
		for(int i=1;i<=n;i++){
			g[i].clear();
		}
		int u,v;
		for(int i=1;i<=n-1;i++){
			read(u); read(v);
			g[u].push_back(v);
			g[v].push_back(u);
		}

		dfs2(1,0); // 统计子树大小 
		dfs3(1,0); // 转移 
		
		cout<<f[1]<<endl;
	}
	return 0;
}
/*
1
5
1 2
2 3
2 4
2 5

*/ 

1012题 Loop / 环

题目大意
给定一个数组,操作 k k k 次,每次把某一个数字往后挪若干格。
求字典序最大的结果,行末无空格。

考察内容
贪心,排序

分析
取出前 k k k 个不是最大的元素,从大到小排序,再和剩下的序列归并即可。
最后控制一下输出。

#include<bits/stdc++.h>
#define ll long long
#define cer(x) cerr<<(#x)<<" = "<<(x)<<'\n'
#define endl '\n'
using namespace std;
const int N=3e5+10;
ll n,k,a[N];
ll b[N];

bool cmp(ll x,ll y){
	return x>y;
}
vector<ll> v;

int main(){ 
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t; cin>>t;
	while(t--){
		v.clear();
		
		cin>>n>>k;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		ll max1=a[1]; 
		for(int i=2;i<=n;i++){
			max1=max(max1,a[i]);
		}
		
		int cnt=0;
		int pos=n;
		for(int i=1;i<=n;i++){
			if(a[i]<max1){
				cnt++; 
				v.push_back(a[i]);
				
				if(cnt>=k){
					pos=i;
					break;
				}
			}
		}
		
		sort(v.begin(),v.end(),cmp);
		
		int F=1; // 是第一个 
		for(int i=1;i<=pos;i++){
			if(a[i]==max1){
				if(F){
					F=0; cout<<a[i];
				}
				else cout<<' '<<a[i];
			}
		}
		int p1=0;
		int len=v.size();
		for(int i=pos+1;i<=n;i++){
			while(p1<=len-1 && v[p1]>a[i]){
				if(F){
					F=0; cout<<v[p1];
				}
				else cout<<' '<<v[p1];
				
				p1++;
			}
			
			if(F){
				F=0; cout<<a[i];
			}
			else cout<<' '<<a[i];
		}
		while(p1<=len-1){
			if(F){
				F=0; cout<<v[p1];
			}
			else cout<<' '<<v[p1];
				
			p1++;
		}
		cout<<endl;
	}
	return 0;
}

进阶题

1010题 Planar graph / 平面图

题目大意
从一个平面图中删去若干条边,使其无环。
求字典序最小的删边方案。

考察内容
最小生成树,并查集

分析
对平面图中的每个连通分量求最大生成树,剩下的边即答案。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int M = 2e5 + 5;

int n, m;

int p[N];
bool vis[M];
int u[M], v[M];

int dsu(int x) {
	return p[x] == x ? x : p[x] = dsu(p[x]);
}

int main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		memset(vis, 0, sizeof vis);
		memset(p, 0, sizeof p);
		memset(u, 0, sizeof u);
		memset(v, 0, sizeof v);
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			p[i] = i;
		}
		for (int i = 1; i <= m; i++) {
			cin >> u[i] >> v[i];
		}
		
		int tot = m;
		for (int i = m; i >= 1; i--) { // 按序号从大到小的顺序枚举每条边 
			int x = dsu(u[i]), y = dsu(v[i]); // 取根节点 
			if (x != y) { 
				tot--;
				vis[i] = 1;
				p[x] = y; // 合并并查集 
			}
		}
		
		cout << tot << endl;
		for (int i = 1; i <= m; i++) {
			if (!vis[i]) cout << i << ' ';
		}
		cout << endl;
	}
	return 0;
}

1009题 Map / 地图

题目大意
给定一个大地图和一个小地图,它们都是矩形。小地图是从大地图压缩而来的。

将小地图放在大地图上,使小地图完全位于大地图(包括边界)内。无论如何放置小地图,总有一个点 P P P 代表小地图和大地图中的相同位置(例如,在下图中,两张地图上的图钉位置代表日本东京)。
求点 P P P 的坐标。
地图

考察内容
数学知识

分析
本题的题目背景是巴拿赫不动点定理。由于大地图到小地图的映射关系是一个压缩映射, 因此存在唯一不动点。
分析

#include<bits/stdc++.h>
using namespace std;
#define int long long

int X[4], Y[4];
int x[4], y[4];

int cross(int a1, int a2, int a3, int a4) {
	return a1 * a4 - a2 * a3;
}

signed main() {
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		for (int i = 0; i < 4; i++) cin >> X[i] >> Y[i];
		for (int i = 0; i < 4; i++) cin >> x[i] >> y[i];
		int Ux = X[3] - X[0], Uy = Y[3] - Y[0];
		int Vx = X[1] - X[0], Vy = Y[1] - Y[0];
		int ux = x[3] - x[0], uy = y[3] - y[0];
		int vx = x[1] - x[0], vy = y[1] - y[0];
		double p = cross(X[0] - x[0], vx - Vx, Y[0] - y[0], vy - Vy) / (double)cross(ux - Ux, vx - Vx, uy - Uy, vy - Vy);
		double q = cross(ux - Ux, X[0] - x[0], uy - Uy, Y[0] - y[0]) / (double)cross(ux - Ux, vx - Vx, uy - Uy, vy - Vy);
		cout << fixed << setprecision(6) << x[0] + p * ux + q * vx << ' ' << y[0] + p * uy + q * vy << endl; 
	} 
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值