CF710

本文详细解析了Codeforces编程竞赛中的四道题目:A. KingMoves、B. OptimalPointonaLine、C. MagicOddSquare和E. GenerateaString。对于每道题目,分别给出了思路和简洁高效的代码实现,包括判断棋盘国王移动步数、找到数列中位数点、构造奇数平方阵以及生成特定字符串的方法。通过这些解析,帮助读者深入理解算法和优化技巧。
摘要由CSDN通过智能技术生成

A. King Moves

思路

一共只有三种情况

  1. 在角上, 答案为3
  2. 在边上, 答案为5
  3. 在中间, 答案为8

代码

#include<bits/stdc++.h>

using namespace std;

char s[2];
int main()
{
	cin >> s[0] >> s[1];
	if(s[0] - 'a' == 0 || s[0] - 'a' == 7)
	{
		if(s[1] - '0' == 1 || s[1] - '0' == 8)
		{
			cout << 3 << endl;
		}
		else
		{
			cout << 5 << endl;
		}
	}
	else if(s[1] - '0' == 1 || s[1] - '0' == 8)
	{
		cout << 5 << endl;
	}
	else
	{
		cout << 8 << endl;
	}
	return 0;
}

大佬

采用了更简便的赋值方法

int a=0, b=0;
if(c=='a' || c=='h') a=1;
if(d=='1' || d=='8') b=1;
if(a+b==2) cout<<3;
else if(a+b==1) cout<<5;
else cout<<8;

B. Optimal Point on a Line

思路

显而易见, 中位数到所有点的距离最小。
这是肯定的, 举个例子, 设有三个数 a 1 , a 1 + b , a 1 + c a_1, a_1+b, a_1+c a1,a1+b,a1+c
那么中位数距离为b + ( c - b) = c.
然而在两边的数距离却为b+c;

代码

#include<bits/stdc++.h>

using namespace std;

int n;
int a[300010];
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	sort(a+1, a+n+1);
	if(n % 2 == 1)
	{
		n = n / 2 + 1;
	}
	else
	{
		n = n / 2;
	}
	cout << a[n] << endl;
	return 0;
}

大佬

其实不用排序, 直接n_thelement 它不香吗?QAQ

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

#define mp make_pair
#define pb push_back


typedef long long ll;
typedef long double ld;

using namespace std;

int n;
int a[500000];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	int x = (n - 1) / 2;
	nth_element(a, a + x, a + n);
	cout << a[x] << "\n";
	return 0;
}

C. Magic Odd Square

思路

很容易就能发现, 要求行,列, 对角线都为奇数个奇数,那么, 显然, 一个这样的图形是合法的。
菱形里面填奇数, 剩下地方填偶数即可

代码

#include<bits/stdc++.h>

using namespace std;

int n;
int ans[50][50];
int main()
{
	int odd = 1;
	int even = 2;
	cin >> n;
	int l = (n+1) / 2;
	int r = (n+1) / 2;
	for(int i = 1; i <= n / 2; i++)
	{
		for(int j = l; j <= r; j++)
		{
			ans[i][j] = odd;
			odd += 2;
		}
		l--; r++;
	}
	if(n % 2 == 1)
	{
		for(int i = l; i <= r; i++)
		{
			ans[n / 2 + 1][i] = odd;
			odd += 2;
		}
	}
	l = (n+1) / 2;
	r = (n+1) / 2;
	for(int i = n; i > n - (n / 2); i--)
	{
		for(int j = l; j <= r; j++)
		{
			ans[i][j] = odd;
			odd += 2;
		}
		l--, r++;

	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			if(ans[i][j] == 0)
			{
				ans[i][j] = even;
				even += 2;
			}
		}
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

dalao

先预处理出来哪些位置填奇数, 最后在重新遍历一遍填数值;

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

#define mp make_pair
#define pb push_back


typedef long long ll;
typedef long double ld;

using namespace std;
int n0 = 2;
int n1 = 1;
int en[100][100];

int g0() {
	n0 += 2;
	return n0 - 2;
}

int g1() {
	n1 += 2;
	return n1 - 2;
}
int n;

int main() {
	cin >> n;
	int now = 1;
	for (int i = 0; i < n; ++i) {
		int x = now / 2;
		int st = n / 2;
		for (int j = st - x; j <= st + x; ++j)
			en[i][j] = 1;
		if (i * 2 + 1 < n)
			now += 2;
		else
			now -= 2;
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (en[i][j])
				printf("%d ", g1());
			else
				printf("%d ", g0());
		}
		printf("\n");
	}
	return 0;
}

E. Generate a String

题解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值