Codeforces Round 903 (Div. 3) A~F

本文介绍了五道编程题目,涉及字符串比较、线性搜索、数学逻辑(如质因数分解和约数)、动态规划以及树结构的使用。通过暴力枚举、分情况讨论和优化算法实现AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Don't Try to Count

思路:暴力,字符串arr1的长度不超过25,暴力枚举即可。

AC代码

#include <bits/stdc++.h>
#define int long long
void solve()
{
	int a, b;
	cin >> a >> b;
	string arr1, arr2;
	cin >> arr1 >> arr2;
	if (arr1 == arr2)
	{
		cout << 0 << endl;
		return;
	}

	int anscnt = 0;

	for (int i = 0; i < arr1.size(); i++)
	{
		int l = i;
		int r = 0;
		while (arr2[r] == arr1[l] && l < arr1.size())
		{
			l++;
			r++;
		}
		if (r == arr2.size())
		{
			cout << anscnt << endl;
			return;
		}
	}

	int cnt = 25;
	while (arr1.size() <= 25)
	{
		arr1 += arr1;
		anscnt++;
		for (int i = 0; i < arr1.size(); i++)
		{
			int l = i;
			int r = 0;
			while (arr2[r] == arr1[l] && l < arr1.size())
			{
				l++;
				r++;
			}
			if (r == arr2.size())
			{
				cout << anscnt << endl;
				return;
			}
		}
	}


	cout << -1 << endl;

}

signed main()
{
	int t;
	t = 1;
	t = read(t);
	while (t--)
	{
		solve();
	}

}

B. Three Threadlets

思路:分情况讨论即可

AC代码

#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
using namespace std;
constexpr int inf=1e16;
int a[8];
void ss()
{

    for(int i=1;i<=3;i++)cin>>a[i];
    if(a[1]==a[2]&&a[1]==a[3])
    {
    	cout<<"YES";
    	return;
	}
	sort(a+1,a+1+3);
	if(a[1]==a[2]&&a[1]!=a[3])
	{
		if(a[3]%a[1]==0)
		{
			if(a[3]/a[1]<=4)cout<<"YES";
			else cout<<"No";
			return;
		}
		else
		{
			cout<<"No";return;
		}
	}
	if(a[3]==a[2]&&a[1]!=a[3])
	{
		if(a[3]%a[1]==0)
		{
			if(a[3]/a[1]<=2)cout<<"YES";
			else cout<<"No";
			return;
		}
		else
		{
			cout<<"No";return;
		}
	}
	if(a[3]!=a[2]&&a[1]!=a[3]&&a[1]!=a[2])
	{
		if(a[1]+a[2]==a[3]&&a[2]==a[1]*2)cout<<"YES";
			else cout<<"No";
			return;
	}
	
}
signed main()
{
	int t;
	cin>>t;
	while(t--)
	{
		ss();cout<<endl;
	 } 
}

C. Perfect Square

思路:旋转90度不变的话,将方阵四等分,每个方阵对应元素相等即可。坐标(x, y)旋转90度后坐标为(y, n + 1 - x)。枚举任一四等分方阵,将四个坐标同时变为四个中最大的元素即可。

AC代码

#include <bits/stdc++.h>
#define int long long
char arr[1010][1010];
void solve()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			cin >> arr[i][j];
		}

	int ans = 0;
	for(int i = 1; i <= n / 2; i++)
		for (int j = 1; j <= n / 2; j++)
		{
			int x = i, y = j;
			int x1 = y, y1 = (n + 1) - x;
			int x2 = y1, y2 = (n + 1) - x1;
			int x3 = y2, y3 = (n + 1) - x2;
			char minchar = max(arr[x][y], max(arr[x1][y1], max(arr[x2][y2], arr[x3][y3])));
			ans += minchar - arr[x][y] + minchar - arr[x1][y1] + minchar - arr[x2][y2] + minchar - arr[x3][y3];
		}

	cout << ans << endl;
}

signed main()
{
	int t;
	t = 1;
	t = read(t);
	while (t--)
	{
		solve();
	}

}

D. Divide and Equalize

思路:题目中的操作实际上就是在转移约数,最终整个数组的累乘不变,所以统计每个数的质因数的个数,如果均能整除n即为成立。

AC代码

#include <bits/stdc++.h>
#define int long long
int numyue[1000010];
int a[100010];
set<int> st;
void check(int x)
{
	for (int i = 2; i <= x / i; i++)
	{
		int s = 0;
		if (x % i == 0)
		{
			st.insert(i);
			while (x % i == 0)
			{
				x /= i;
				s++;
			}
			numyue[i] += s;
		}
	}
	if (x > 1)
	{
		st.insert(x);
		numyue[x]++;
	}
}
void solve()
{
	zero(numyue);
	st.clear();
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		check(a[i]);
	}
	int res = 1;
	for (auto x : st)
	{
		if (numyue[x] % n)
		{
			cout << "NO" << endl;
			return;
		}
	}
	cout << "YES" << endl;
}

signed main()
{
	int t;
	t = 1;
	t = read(t);
	while (t--)
	{
		solve();
	}

}

E. Block Sequence

思路:dp问题,设dp[i]为第i个位置上的最小代价。从后往前枚举。

当a[i] > n - i时,dp[i] = dp[i + 1] + 1,因为这个位置肯定不能选,删掉这个位置代价的最小值即为dp[i + 1] + 1;

当a[i] == n - i时,dp[i] = 0;

当a[i] < n - i时,有三种可能,一是删掉后面的点,使a[i] == n - i,代价为n - i - a[i], 二是i + a[i] + 1位置上的最小代价,三是删掉这个点,代价为dp[i + 1] + 1,三者取最小值;

AC代码

#include <bits/stdc++.h>
#define int long long

int dp[200010];
int a[200010];
const int inf = 1e16;
void solve()
{
	zero(dp);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n; i++)dp[i] = 0;
	for (int i = n; i >= 1; i--)
	{
		if (a[i] > n - i)dp[i] = dp[i + 1] + 1;
		if (a[i] == n - i)dp[i] = 0;
		if (a[i] < n - i)
		{
			dp[i] = min(n - i - a[i], dp[i + a[i] + 1]);
			dp[i] = min(dp[i], dp[i + 1] + 1);
		}
	}
	
	cout << dp[1] << endl;
}

signed main()
{
	int t;
	t = 1;
	t = read(t);
	while (t--)
	{
		solve();
	}

}

F. Minimum Maximum Distance

思路:树上任一点到其他点最远的距离即为到树的直径的两个端点中的一个的距离,跑两遍bfs求一下两个端点,取两个距离中的最大值,最后再取所有最大值中的最小值即可。

AC代码

#include <bits/stdc++.h>
#define int long long

const int N = 400010;
vector<int> v[N];
int dist[N], f[N];
int st[N];
int n, k;

int bfs(int u)
{
	for (int i = 1; i <= n; i++)dist[i] = -1;
	queue<int> q;
	q.push(u);
	dist[u] = 0;

	while (q.size())
	{
		int t = q.front();
		q.pop();
		for (auto i : v[t])
		{
			int j = i;
			if (dist[j] == -1)
			{
				dist[j] = dist[t] + 1;
				q.push(j);
			}
		}
	}

	int ans = -1;
	for (int i = 1; i <= n; i++)
	{
		if (st[i] && (ans == -1 || dist[i] > dist[ans]))
		{
			ans = i;
		}
	}
	return ans;
}
void solve()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		v[i].clear();
	}
	for (int i = 1; i <= n; i++)st[i] = 0;
	for (int i = 1; i <= k; i++)
	{
		int x;
		cin >> x;
		st[x] = 1;
	}
	int m = n - 1;
	while (m--)
	{
		int a, b;
		cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}

	int a = bfs(1);
	int b = bfs(a);
	for (int i = 1; i <= n; i++)f[i] = dist[i];
	bfs(b);

	for (int i = 1; i <= n; i++)
	{
		f[i] = max(f[i], dist[i]);
	}
	int minans = 1e18;
	for (int i = 1; i <= n; i++)
	{
		minans = min(minans, f[i]);
	}
	cout << minans << endl;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	t = 1;
	t = read(t);
	while (t--)
	{
		solve();
	}

}

### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参与并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值