Weekly test 2023.9.24

D-Xiaodu Fishing_Weekly Competition (nowcoder.com)

If you use two points, it will time out.

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n, m, ans;
char a[N][N];
bool check(int x, int aa, int bb)
{
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= m; j ++)
		{
			if(a[i][j] == '#')
			{
				if(abs(aa - i) + abs(bb - j) >= x)return false;
			}
		}
	}
	return true;
}
int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= m; j ++)
		{
			cin >> a[i][j];
		}
	}
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= m; j ++)
		{
			int l = 0, r = 1e9 + 10;
			while(l + 1 != r)
			{
				int mid = (l + r) >> 1;
				if(check(mid, i, j))r = mid;
				else l = mid;
			}
			ans = max(ans, l);
		}
	}
	cout << ans;
	return 0;
}

Therefore, we can make certain optimizations

For each fish we know that the four vertices are the farthest distances that can be reached

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

const int N = 1e3 + 5, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

#define x first;
#define y second;

typedef long long LL;
typedef pair<int, int> PII;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int n, m;
char g[N][N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;

	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			cin >> g[i][j];

	int ans = -1;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
			if (g[i][j] == '#')
			{

				int d1 = i - 1 + j - 1; 			
				int d = d1;
				int d2 = i - 1 + abs(j - m);		
				d = max(d, d2);
				int d3 = abs(i - n) + j - 1;		
				d = max(d, d3);
				int d4 = abs(i - n) + abs(j - m);	
				d = max(d, d4);
				ans = max(ans, d);
				// cout << d1 << ' ' << d2 << ' ' << d3 << ' ' << d4 << endl;
			}

	cout << ans << endl;
	return 0;
}

F-Xiaohong’s palindrome_weekly competition (nowcoder.com)

The palindrome string can be traversed in half. If the traversed is '?', check whether the corresponding one is '?'. If so, it means that there can be 26 changes. If not, it means that there is only one change, which is the corresponding letter.

Note: If the string is an odd number, pay special attention to the letter in the middle. If the letter in the middle is '?', there are 26 more variations.

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
string s;
int sum;
int main()
{
	cin >> s;
	int n = s.size();
	for(int i = 0; i < n / 2; i ++)
	{
		if(s[i] == '?' && s[n - i - 1] == '?')sum ++;
		if(s[i] != s[n - i - 1])
		{
			if(s[i] != '?' && s[n - i - 1] != '?')
			{
				cout << 0;
				return 0;
			}
		}
	}
	long long ans = 1;
	if(n & 1 && s[n / 2] == '?')sum ++;
	for(int i = 0; i < sum; i ++)ans = (ans * 26) % mod;
	cout << ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_75087931/article/details/133250820