Codeforces Round #847 (Div. 3) BDE题解

Codeforces Round #847 (Div. 3) 题解

B. Taisia and Dice

题目大意:

扔骰子,每次有一只猫会衔走一个点数最大的骰子,给你原来的骰子个数 n n n,原来的骰子点数之和 s s s,和拿走一个骰子的点数之和 r r r

让你求出原来每个骰子可能的点数,输出任意一种即可。

解答:

首先输出最大的点数 s − r s-r sr ,然后输出 r % ( n − 1 ) r \%(n-1) r%(n1) 个点数为 r / ( n − 1 ) + 1 r/(n-1)+1 r/(n1)+1 的数,剩下的骰子点数为 r / ( n − 1 ) r/(n-1) r/(n1)

单词积累:

dice 骰子 sequence 序列 restore 恢复 fulfills 实现 constraints 约束

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
int n, s, r;

void solve() {
	cin >> n >> s >> r;
	int mx = s - r;
	cout << s - r << " ";
	int i;
	for (i = 0; i < r % (n - 1); i++)cout << r / (n - 1) + 1 << ' ';
	while (i < n-1) {
		cout << r / (n - 1) << " \n"[i == n - 2];
		i++;
	}
}

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

	int T;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

D. Matryoshkas

题目大意:

给你一个序列,让你返回这个序列有多少个连续上升子序列。

解答:

贪心。用 m a p map map 离散化,命名为 c n t cnt cnt 。遍历 c n t cnt cnt ,取 r e s = ∑ m a x ( 0 , c n t [ i ] − c n t [ i − 1 ] ) res=\sum max(0,cnt[i]-cnt[i-1]) res=max(0,cnt[i]cnt[i1])。即可

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
int n;


void solve() {
	map<int, int> cnt;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}
	int ans = 0;
	int last = -1;
	int c = 0;
	
	for (auto t : cnt) {
		int x = t.first, y = t.second;
		if (last + 1 != x)c = 0;//两个之间有一些cnt为0的区域
		ans += max(0, y - c);
		last = x, c = y;
	}

	cout << ans << endl;
}

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

	int T;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

E. Vlad and a Pair of Numbers

题目大意:

给你一个数字x,输出两个数字 a a a b b b 使 a + b 2 = a ⨁ b = x \dfrac{a+b}{2}=a\bigoplus b=x 2a+b=ab=x

解法:

首先,由于 a a a 异或 b b b 的结果是 x x x,则 x x x 中是 1 1 1 的位数, a a a b b b 里一定是一个 1 1 1 一个 0 0 0,不妨设置所有此种情况构造出的 1 1 1 都在 a a a 上, 0 0 0 都 在 b b b 上。 x x x 中是 0 0 0 的位数, a a a b b b 的此位数上同是 1 1 1 或者同是 0 0 0。我们可以考虑从高位开始遍历,两个都为 1 1 1 的贡献为 2 i 2^i 2i ,如果两个都为 1 1 1 满足加上这个贡献后 a + b ≤ 2 × x a+b\le2\times x a+b2×x 那么就加上。循环结束时判断是否 a + b = 2 × x a+b=2\times x a+b=2×x ,如果是就返回。不是则返回 − 1 -1 1

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;

void solve() {
	int x;
	cin >> x;
	LL a = x;
	LL b = 0;

	for (int i = 31; i >= 0; i--) {
		if (~a >> i & 1 && a + b + (2ll << i) <= 2 * x) {
			b |= 1ll << i;
			a |= 1ll << i;
		}
	}

	if (a + b == 2 * x)cout << a << ' ' << b << endl;
	else cout << "-1" << endl;
}

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

	int T;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qiutianhan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值