【Codeforces】Codeforces Round 839 (Div. 3) (ABCD) (补赛)

文章提供了四道编程题目的解题思路和参考代码,包括简单的加法运算,矩阵的90度旋转,构造差分数组以最大化不同数值,以及绝对值排序问题的解决方案。每道题都涉及算法设计和模拟实现。
摘要由CSDN通过智能技术生成

A  A+B?

解题思路

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
const int N = 2e5 + 5;
ll T;
ll a, b;
void solve() {
	scanf("%lld+%lld", &a, &b);
	cout << a + b << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Matrix Rotation

解题思路

模拟即可

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
int a[3][3];
void sp() {
	int b[3][3];
	for (int i = 1; i <= 2; i++) {
		for (int j = 1; j <= 2; j++) {
			b[i][j] = a[i][j];
		}
	}
	a[1][1] = b[2][1];
	a[1][2] = b[1][1];
	a[2][1] = b[2][2];
	a[2][2] = b[1][2];
}
bool check() {
	for (int i = 1; i <= 2; i++) {
		if (a[1][i] >= a[2][i]) return 0;
		if (a[i][1] >= a[i][2]) return 0;
	}
	return 1;
}
void solve() {
	for (int i = 1; i <= 2; i++) {
		for (int j = 1; j <= 2; j++) {
			cin >> a[i][j];
		}
	}
	int ok = 0;
	for (int i = 0; i < 4; i++) {
		sp();
		if (check()) ok = 1;
	}
	if (ok) cy;
	else cn;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

C Different Differences

解题思路

求差分数组,使数字种类最多,我们先尽可能使差分数字最多,那么直接构造差分1,2,3,4,...

再判断是否在范围内

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll k, n;
ll ans[N];
void solve() {
	cin >> k >> n;
	int cnt = 1;
	ans[1] = 1;
	for (int i = 2; i <= k; i++) {
		ans[i] = ans[i - 1] + cnt;
		cnt++;
	}
	cnt = n;
	for (int i = k; i >= 1; i--) {
		if (ans[i] > n || ans[i] >= ans[i + 1]) {
			ans[i] = cnt;
			cnt--;
		} else break;
	}
	for (int i = 1; i <= k; i++) cout << ans[i] << ' ';
	cout << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Absolute Sorting

解题思路

分类讨论求x的范围区间

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n, x;
ll a[N];
ll inf = 9999999999999;
void solve() {
	ll l = 0, r = inf;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i < n; i++) {
		if (a[i] < a[i + 1]) r = min(r, (a[i] + a[i + 1]) / 2);
		if (a[i] > a[i + 1]) l = max(l, (a[i] + a[i + 1] + 1) / 2);
	}
	if (l > r) l = -1;
	cout << l << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值