【Codeforces】Codeforces Round 847 (Div. 3) (ABCDE) (补赛)

Polycarp and the Day of Pi

解题思路

枚举配对即可

参考代码

#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;
string pi = "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280";
void solve() {
	string s;
	cin >> s;
	int cnt = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == pi[i]) cnt++;
		else break;
	}
	cout << cnt << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Taisia and Dice

解题思路

贪心:s-r就是最大的点数,安置在最后一个,前面的骰子只要一点点累加到r即可

参考代码

#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, s, r;
void solve() {
	cin >> n >> s >> r;
	ll ans[n + 1] = {};
	ans[n] = s - r;
	for (int k = 1; k <= 6; k++) {
		for (int i = 1; i < n; i++) {
			ans[i]++;
			r--;
			if (r == 0) {
				for (int t = 1; t <= n; t++) cout << ans[t] << ' ';
				cout << endl;
				return;
			}
		}
	}

}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Premutation

解题思路

贪心:首先可以确定第一个和最后一个数,接着找不存在最后一个的数的数列,那么直接确定中间的数

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
const int N = 1e3 + 5;
ll T;
ll n;
int a[N][N];
int ans[N];
unordered_map<int, int>mp1, mpn;
void solve() {
	mp1.clear(), mpn.clear();
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j < n; j++) {
			cin >> a[i][j];
		}
		mp1[a[i][1]]++, mpn[a[i][n - 1]]++;
	}
	for (auto i : mp1) if (i.second != 1) ans[1] = i.first;
	for (auto i : mpn) if (i.second != 1) ans[n] = i.first;
	for (int i = 1; i <= n; i++) {
		if (a[i][n - 1] != ans[n]) {
			for (int j = 2; j < n; j++) ans[j] = a[i][j];
			break;
		}
	}
	for (int i = 1; i <= n; 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;
}

Matryoshkas​​​​​​

解题思路

使用哈希表,统计数字出现的次数,每一轮从前往后将连续的自然数计数-1,答案就是轮数

参考代码

#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;
int a[N];
map<int, int>mp;
bool cmp(int a, int b) {
	return a > b;
}
void solve() {
	int ans = 0;
	mp.clear();
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]++;
	for (auto &i : mp) {
		int f = i.first, s = i.second;
		ans += s;
		while (i.second) {
			f = i.first;
			while (mp[f]) {
				mp[f]--;
				f++;
			}
		}
	}
	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Vlad and a Pair of Numbers​​​​​​

解题思路

暴力模拟发现,x为奇数一定为-1,如果有解的话,一定存在一组解a=x*\frac{1}{2} ,b=x*\frac{3}{2}, 所以如果x为偶数,就假设答案为a,b再验证即可

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl;
#define cn cout << "NO" << endl;
//快速幂
ll q_pow(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if (b & 1) ans *= a;
		a *= a;
		b >>= 1;
	}
	return ans;
}
//欧拉筛
const int N = 2e5 + 5;
ll T;
ll x;
ll a, b;
void solve() {
	cin >> x;
	if (x % 2 == 1) {
		cout << -1 << endl;
		return;
	}
	int ok = 0;
	a = x / 2, b = x + x / 2;
	if ((a ^ b) == x) ok = 1;
	if (ok) cout << a << ' ' << b << endl;
	else cout << -1 << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值