Codeforces Round 944 (Div. 4) A~G题解
A
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
int x, y;
cin >> x >> y;
cout << min(x, y) << ' ' << max(x, y) << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
B
现在我们拥有一个字符串,要找到一个与原字符串不同的排序方法,只要交换两个不同的字符即可,也就是说,如果这个字符串中的每个字符相同便不满足条件,应该输出"No", 交换方法有很多种,选择最方便的一种即可
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
string s;
cin >> s;
string a = s;
sort(a.begin(), a.end()); //排序后可以保证如果第一个字符等于最后一个,那么字符串中所有字符相同
if (a == s) swap(a[0], a[a.size() - 1]); //如果与原字符串相同就进行交换
if (a == s) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
cout << a << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
C
我们先调整
在 之间, 不在,那么 一定经过 ; 在 之间, 不在,那么 一定经过 ;
判断后按照题目要求输出
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a > b) swap(a, b);
if (c > d) swap(c, d);
if (a <= c && c <= b && b <= d && d <= a + 24) cout << "YES" << endl; //d在ab外,或者d不在ab内,两种写法都可以
else if (c <= a && a <= d && d <= b && b <= c + 24) cout << "YES" << endl;
else cout << "NO" << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
D
记录
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
string s;
cin >> s;
int cnt = 1; //初始有一块
int t = 0;
for (int i = 1; i < s.size(); i ++ ) {
if (s[i] != s[i - 1]) //进行切片
cnt ++ ;
if (s[i] == '1' && s[i - 1] == '0') //
t = 1;
}
if (t == 1) cnt -- ;
cout << cnt << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
E
二分找到第一个大于等于当前距离的点,这个点的前一个标志距离一定小于当前询问,然后计算当前区间内所花费的时间,加上走到当前标识的时间即可
注:c++23 编译环境下会出现精度问题,建议使用c++17 进行编译
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int a[100005], b[100005];
void solve() {
int n, k, q;
cin >> n >> k >> q;
for (int i = 1; i <= k; i ++ ) {
cin >> a[i];
}
for (int i = 1; i <= k; i ++ ) {
cin >> b[i];
}
while (q -- ) {
int len;
cin >> len;
int x = lower_bound(a + 1, a + k + 1, len) - a - 1; //二分
int s = b[x] + (len - a[x]) * 1.0 * (b[x + 1] - b[x]) / (a[x + 1] - a[x]);
cout << s << ' ';
}
cout << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
// 二分替换代码
int l = 1, r = k;
while (l < r) {
int mid = l + r >> 1;
if (x <= a[mid]) r = mid;
else l = mid + 1;
}
l -- ;
F
暴力枚举即可,因为四个象限相同,所以只需要计算一个象限 + 坐标轴,将答案 * 4,在计算的时候需要优化一下上限,当前的y如果超过了范围,那么在
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
double get(LL x, LL y) {
return sqrtl(x * x + y * y); //sqrtl()精度更高的开根号
}
void solve() {
int n;
cin >> n;
int cnt = 0;
int y = n;
for (int i = 0; i <= n; i ++ ) {
for (int j = y; j > 0; j -- ) {
double t = get(i, j);
if (t >= n + 1) {
y -- ;
continue;
}
if (t >= n) cnt ++ ;
else break;
}
}
cout << cnt * 4 << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
G
只要让每一位上可以存放的数字最小即可,当
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
int n;
cin >> n;
vector<int> a(n);
map <int, priority_queue<int, vector<int>, greater<int>>> mp;
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
mp[a[i] >> 2].push(a[i]);
}
for(int i = 0; i < n; i ++ ) {
cout << mp[a[i] >> 2].top() << ' ';
mp[a[i] >> 2].pop();
}
cout << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话