Codeforces Round 903 (Div. 3) A~F

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();
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值