Codeforces Round #786 (Div. 3)ABCDE题解


题目链接

A - Number Transformation

签到题
code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve() {
	ll x, y;
	cin >> x >> y;
	if (x == y) {
		cout << 1 << ' ' << 1 << endl;
		return;
	}
	else if (x > y || y % x != 0) {
		cout << 0 << ' ' << 0 << endl;
		return;
	}
	else {
		ll o = y / x;
		cout << 1 << ' ' << o << endl;
	}

}
int main() {
	std::cin.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}

}

B - Dictionary

题意:输入字符串,输出顺序。
题解:模拟即可。
code:

#include<bits/stdc++.h>
uusing namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve() {
	string s;
	cin >> s;
	int x = s[0] - 'a' + 1;
	int y = s[1] - 'a' + 1;
	int res = (x - 1) * 25;
	if (x < y) res += y - 1;
	else res += y;
	cout << res << endl;
}
int main() {
	std::cin.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}

}

C - Infinite Replacement

题意:给出两个字符串s,t,s中只含a,t可以代替s中任何一个a,问共有几种代替方法?
题解:如果t中有a且长度大于1,结果为无穷,如果t中有a且长度等于1,结果为1,如果t中无a,那就数学排列组合计算。
code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
set<string>a;
void solve() {
	string s;
	string tt;
	cin >> s >> tt;
	int flag = 0;
	for (int i = 0; i < tt.length(); i++) {
		if (tt[i] == 'a') flag = 1;
	}

	if (tt.length() > 1 && flag == 1) {
		cout << -1 << endl;
		return;
	}
	if (tt.length() == 1 && flag == 1) {
		cout << 1 << endl;
		return;
	}
	else {
		ll len = s.length();
		ll ans = pow(2, len);
		cout << ans << endl;
	}

}
int main() {
	std::cin.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}

}

D - A-B-C Sort

题意:有三个数组,分别进行下列操作,a从末尾开始放入b的中间,b从中间开始放入c的末尾,看看c最终是否是不递减顺序。
题解:转换思路,其实就是看a中相邻元素是否交换后不递减,n为奇数的情况,第一个数一定是最小的,这里的相邻指的是(1-2,3-4)。
code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int a[200040];
void solve() {
	int n;
	cin >> n;
	memset(a, 0, sizeof(a));
	int minn = 1000006;
	int flag = 0;

	for (int i = 0; i < n; i++) {
		cin >> a[i];
		minn = min(a[i], minn);
	}
	if (n == 1) {
		cout << "YES" << endl;
		return;
	}
	ll oo = 0;
	if (n % 2 == 1) {
		if (a[0] != minn) flag = 1;
		if (flag == 0) {
			for (int i = 1; i < n - 1; i = i + 2) {
				if (a[i] > a[i + 1]) {
					oo = a[i];
					a[i] = a[i + 1];
					a[i + 1] = oo;
				}

			}
			for (int i = 0; i < n - 1; i++) {
				if (a[i] > a[i + 1]) {
					flag = 1;
					break;
				}
			}
		}
	}
	else if (n % 2 == 0) {
		for (int i = 0; i < n - 1; i = i + 2) {
			if (a[i] > a[i + 1]) {
				oo = a[i];
				a[i] = a[i + 1];
				a[i + 1] = oo;
			}

		}
		for (int i = 0; i < n - 1; i++) {
			if (a[i] > a[i + 1]) {
				flag = 1;
				break;
			}
		}
	}
	if (flag == 1) cout << "NO" << endl;
	else cout << "YES" << endl;

}
int main() {
	std::cin.tie(nullptr);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}

}

E - Breaking the Wall

题意:有一个武器,打中相应位置减2,相邻位置减1,问打破两个位置最小打多少?
题解:此题用暴力解决,共有三种情况。1.打两个相邻的位置。2.打两个位置,其中间隔一个位置。3.打两个最小的位置。
code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve() {
	int n;
	cin >> n;
	double a[200006];
	double ans = 1000 * 1000 * 1000;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n - 1; i++) {
		double x = a[i];
		double y = a[i + 1];
		if (x < y) swap(x, y);
		if (2 * y <= x) ans = min(ceil(x / 2), ans);
		else ans = min(ans, ceil((x + y) / 3));
	}
	for (int i = 0; i < n - 2; i++) {
		ans = min(ans, ceil((a[i] + a[i + 2]) / 2));
	}
	sort(a, a + n);
	ans = min(ans, ceil(a[0] / 2) + ceil(a[1] / 2));
	cout << ans << endl;
}
int main() {
	std::cin.tie(nullptr);
	int t = 1;
	//cin>>t;
	while (t--) {
		solve();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值