Codeforces Round #798 (Div. 2)ABCD

115 篇文章 3 订阅
42 篇文章 2 订阅

A. Lex String

思路:
双指针模拟

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m,k;
void solve()
{
	cin>>n>>m>>k;
	string a,b;
	cin>>a>>b;
	int i=0,j=0;
	int cnt1=0,cnt2=0;
	sort(all(a));
	sort(all(b));
	while(i<n&&j<m)
	{
		if(a[i]<b[j])
		{
			if(cnt1<k)
			{
				cnt1++;
				cnt2=0;
				cout<<a[i++];
			}
			else
			{
				cnt1=0;
				cnt2++;
				cout<<b[j++];
			}
		}
		else 
		{
			if(cnt2<k)
			{
				cnt1=0;
				cout<<b[j++];
				cnt2++;
			}
			else
			{
				cout<<a[i++];
				cnt1++;
				cnt2=0;
			}
		}
	}
	cout<<endl;
}
signed main()
{
	io;
	cin>>_;
	while(_--)
	solve();
	return 0;
}

B. Mystic Permutation

如果一个数字不在他的下标位置,则先把他放回自己本身的位置。如果本身就在下标位置,将这些数字进行swap,能与后边的swap字典序最小

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1010;
int a[N];
int c[N];
void solve()
{
	cin>>n;
	memset(a,0,sizeof a);
	memset(c,0,sizeof c);
	for(int i=1;i<=n;i++)cin>>a[i];
	if(n==1)
	{
		cout<<-1<<endl;
		return; 
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i]!=i)
		{
			c[i]=i;
		}
		else c[i]=0;
	}
	for(int i=1;i<=n;i++)
		if(c[i]==0)
		{
			if(i<n)c[i]=i+1,c[i+1]=i;
			else c[i]=c[i-1],c[i-1]=i;
		}
	for(int i=1;i<=n;i++)cout<<c[i]<<' ';
	cout<<endl;
}
signed main()
{
	io;
	cin>>_;
	while(_--)
	solve();
	return 0;
}

C. Infected Tree

两种方法,第一种dfs
第二种 树形dp

//dfs
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=3e5+10;
int h[N],e[2*N],ne[N*2],idx;
bool st[N];
int l[N],r[N],w[N];
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u,int fa)
{
	if(h[u]==-1)return 0;
	int s=0;
	bool f=false;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa)continue;
		if(!f)s+=dfs(j,u),l[u]=j,f=true;
		else s+=dfs(j,u),r[u]=j;
	}
	w[u]+=s;
	return s+1;
}
int dfs2(int u)
{
	if(h[u]==-1)return 0;
	int res=0;
	res=max(res,w[r[u]]+dfs2(l[u]));
	res=max(res,w[l[u]]+dfs2(r[u]));
	return res;
}
void solve()
{
	cin>>n;
	memset(h,-1,sizeof h);
	idx=0;
	memset(st,false,sizeof st);
	memset(l,0,sizeof l);
	memset(r,0,sizeof r);
	memset(w,0,sizeof w);
	for(int i=1;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		add(a,b),add(b,a);
	}
	dfs(1,-1);
	cout<<dfs2(1)<<endl;
}
signed main()
{
	io;
	cin>>_;
	while(_--)
	solve();
	return 0;
}
//dp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=3e5+10,M=N<<1;
int h[N],e[M],ne[M],idx;
int dp[N],sz[N];
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u,int fa)
{
	sz[u]=1;
	int s1=-1,s2=-1;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa)continue;
		dfs(j,u);
		sz[u]+=sz[j];
		if(s1==-1)s1=j;
		else if(s2==-1)s2=j;
	}
	if(s1==-1)return dp[u]=0;
	else if(s2==-1)return dp[u]=sz[s1]-1;
	return dp[u]=max(sz[s1]-1+dp[s2],sz[s2]-1+dp[s1]);
}
void solve()
{
	memset(h,-1,sizeof h);
	idx=0;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		add(a,b),add(b,a);
	}
	cout<<dfs(1,-1)<<endl;
}
signed main()
{
	io;
	cin>>_;
	while(_--)
	solve();
	return 0;
}

D. Lena and Matrix

离所有黑点的最大距离最小,尽量与左上,左下,右上,右下的中间区域。
在这里插入图片描述
i是我们可以提前预处理的黑点,四个方向取max
然后枚举所有点j,最后再去max就是最大值最小的点

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1010;
char g[N][N];
void solve()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin>>g[i][j];
	int max1=-1e9,max2=-1e9,max3=-1e9,max4=-1e9;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(g[i][j]=='B')
			{
				max1=max(max1,i+j);
				max2=max(max2,i-j);
				max3=max(max3,-i+j);
				max4=max(max4,-i-j);
			}
		}
	}
	int resx=0,resy=0,res=1e9;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int t=max({max1-i-j,max2-i+j,max3+i-j,max4+i+j});
			if(t<res)res=t,resx=i,resy=j;
		}
	}
	cout<<resx+1<<' '<<resy+1<<endl;
}
signed main()
{
	io;
	cin>>_;
	while(_--)
	solve();
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值