Codeforces Round #820 (Div. 3) A ~ D

A. Two Elevators

题目链接:

Problem - A - Two Elevators

题面:

在这里插入图片描述

题目大意:

Vlad要乘坐电梯从一楼到达要去的楼层。一共有两部电梯,第一部在a层,可以直接到1层接她,第二部在b层,需要先到c层,再到1层接他。求应该选择哪一部电梯。

思路:

判断|a - 1| 与 |b - c| + |c - 1|的大小

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
void solve() {
	int a, b, c;
	cin >> a >> b >> c;
	if (a - 1 > abs(b - c) + c - 1) cout << 2 << "\n";//a - 1和c - 1一定大于0所以不用取绝对值也行
	else if (a - 1 < abs(b - c) + c - 1) cout << 1 << "\n";
	else cout << 3 << "\n"; 
}
int main() {
	IOS;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

B.Decode String

题目链接:

Problem B - Decode String

题面:

在这里插入图片描述

题目大意:

将一个由0~10的数字构成的字符串按照题目给定的编码规则转换成一个由小写字母组成的字符串。

思路:

模拟。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int ans, n;
string s1;
void solve() {
	cin >> n;
	cin >> s1;
	for (int i = 0; i < n; i++) {
		if (s1[i + 2] != '0' || (s1[i + 2] == '0' && s1[i + 3] == '0') || i > n - 3) {
			cout << char(s1[i] - '0' - 1 + 'a');		
		}
		else {
			ans = 0;
			ans = (s1[i] - '0') * 10 + s1[i + 1] - '0';
			cout << char(ans - 1 + 'a');
			i += 2;
		}
	}
	cout << "\n";
}
int main() {
	IOS;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

C.Jumping on Tiles

题目链接:

C - Jumping on Tiles

题面:

在这里插入图片描述

题目大意:

Polycarp有一个字符串,他想通过几次跳跃从字符串的第一个位置跳跃到字符串的最后一个位置。其中从s[i]跳跃到s[j]所花的代价为|index(s[i]) - index(s[j])|。(index(‘a’)代表字符‘a’在字母表中的位置)求花费的最小代价和花费最小代价能够跳跃的最多次数以及跳跃过程。

思路:

假设字符串长度为n,所花费的最小代价必然是|s[n - 1] - s[0]|。不难发现,当我们跳跃到一个位置上,而这个位置上的字符在字母表上的位置正好在s[0]和s[n - 1]之间时,对于跳跃所需的花费不影响。那么很轻易地就能想到,当s[0] < s[n - 1]时,跳跃过程应该是一个字母表顺序过程,反之,则为逆序。所以我们只需对字符串中的字符进行排序,并维护原来的位置即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 10;
const ll mod = 1e11 + 3;
#define IOS std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
struct ty {
	char c;
	int pos;
} a[MAX];
string s;
bool comp(ty a, ty b) {
	if (a.c != b.c) return a.c < b.c;
	else return a.pos < b.pos;
}
void solve() {
	cin >> s;
	int n = s.length();
	for (int i = 0; i < s.length(); i++) {
		a[i + 1].c = s[i];
		a[i + 1].pos = i + 1;
	}
	sort(a + 1, a + 1 + n, comp);
	int pos1 = 0, pos2 = 0, pos3, pos4;
	for (int i = 1; i <= n; i++) {
		if (s[0] == a[i].c) {
			if (pos1 == 0) pos1 = i;
			pos4 = i;
		}
		if (s[n - 1] == a[i].c) {
			if (pos2 == 0) pos2 = i;
			pos3 = i;
		}
	}
	cout << abs(s[n - 1] - s[0]) << " ";
	if (s[0] <= s[n - 1]) {
		cout << pos3 - pos1 + 1 << "\n";
		for (int i = pos1; i <= pos3; i++) {
			cout << a[i].pos << " ";
		}
	} else {
		cout << pos4 - pos2 + 1 << "\n";
//		cout << a[pos1].pos << " ";
//		for (int i = pos1 - 2; i >= pos2; i--) cout << a[i].pos << " ";
//		cout << a[pos1 - 1].pos;
		for (int i = pos1; i <= pos4; i++) cout << a[i].pos << " ";
		for (int i = pos1 - 1; i >= pos3 + 1; i--) { 
			cout << a[i].pos << " ";
		}
		for (int i = pos3 - 1; i >= pos2; i--) cout << a[i].pos << " ";
		cout << a[pos3].pos;
	}
	cout << "\n";
}
int main() {
	IOS;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

D.Friends and the Restaurant

题目链接:

D - Friends and the Restaurant

题面:

在这里插入图片描述

题目大意:

有n个人去餐厅吃饭,对于每个人,都有在餐厅的消费金额x和消费预算y。将n个人分组分天,去餐厅吃饭,每个组内的消费总预算必须不小于消费总金额。组内每个人去这家餐厅吃饭的天数不超过一天。求这n个人能去餐厅吃饭的最大天数。

思路:

对于第i个人,如果有第j个人满足yi - xi + yj - xj >= 0,则他们可以组团去餐厅吃饭,天数加一。而两个人组团无疑是最优选项。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 10;
const ll mod = 1e11 + 3;
#define IOS std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
ll a[MAX], b[MAX], c[MAX];
void solve() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> b[i];
		c[i] = b[i] - a[i];
	}
	sort(c + 1, c + 1 + n);
	int j = n;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (j > i && c[i] + c[j] >= 0) {
			j--;
			ans++;
		}
		if (j <= i) break;
	}
	cout << ans << "\n";
}
int main() {
	IOS;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值