首页 > 其他分享 >Codeforces Round 944 (Div. 4) A~G题解

Codeforces Round 944 (Div. 4) A~G题解

时间:2024-09-27 21:12:32浏览次数:1  
标签:typedef int 题解 944 long -- solve Div tt

A

\(min\) 函数和 \(max\) 函数的使用,按照格式输出即可。

#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

我们先调整\(ab\),\(cd\)的大小,使数字小的在前,两条线想要相交,一共只有两种情况

  1. \(c\) 在 \(ab\) 之间,\(d\) 不在,那么\(cd\) 一定经过 \(ab\);
  2. \(a\) 在 \(cd\) 之间,\(b\) 不在,那么\(ab\) 一定经过 \(cd\);

判断后按照题目要求输出

#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

记录 \(0\) 和 \(1\) 字符串的次数即片段数,如果出现 \(01\) 的字符串,说明可以减少一次片段(仅限 \(1\) 次)。

#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如果超过了范围,那么在\(i + 1\) 的情况下一定不够,所以可以减少上限来优化时间复杂度

#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

只要让每一位上可以存放的数字最小即可,当 \(x >> 2\) = \(y >> 2\) 时,才满足 $x $ ^ $ y < 4$的交换条件,所以需要使用map + 优先队列来进行辅助存储。

#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;
}

标签:typedef,int,题解,944,long,--,solve,Div,tt
From: https://www.cnblogs.com/sccccc/p/18436574

相关文章

  • Codeforces Round 973题解(E)
    E.PrefixGCD假设我们从一个空集合\(b\)开始,不断从\(a\)数组中选择一个元素添加到\(b\)集合的尾部,当把\(a\)数组的元素全部添加到\(b\)中后,得到的\(b\)即为所求的rearrange后的\(a\)。结论1:每次选择使得其和当前\(b\)中所有元素的最大公约数最小的那个\(a_i\)加入到\(b\)的末......
  • ZZJC新生训练赛第一场题解
    先给出比赛链接:https://ac.nowcoder.com/acm/contest/91452下面说一下难度分层:(同一难度下按字典序排序)Easy(简单):B,FMedium(中等):A,E,HHard(困难):C,GAnti-AK(防AK):D,Icin.tie(nullptr)->sync_with_stdio(false);//加速输入输出的A游游的整数翻转将所......
  • 【洛谷】P4819 [中山市选] 杀人游戏 的题解
    【洛谷】P4819[中山市选]杀人游戏的题解题目传送门题解Tarjan我可爱的Tarjan嘻嘻qaq枚举每一个点,然后枚举每条出边,如果相连的两个点在同一个强连通分量中,或者xx......
  • 【洛谷】AT_abc178_d [ABC178D] Redistribution 的题解
    【洛谷】AT_abc178_d[ABC178D]Redistribution的题解洛谷传送门AT传送门题解一个水水的动态规划,阿巴巴巴。题目大概是这样:给定一个正整数SSS,问有多少个数满足以......
  • 【2024秋#113】锦城ACM周测题解
    2024秋#112】锦城ACM周测题解A.awa1思路这里是对答案进行二分,我们预测一个答案的范围,取这个范围的中点,试探是否可行。如果可行,将这个范围的右边的范围缩小到mid(注意我们所求是最短时间,所以当mid可行的时候我们是将预测的最大的值变小),如果不可行,说明我们预测的这个范围左边......
  • Pbootcms源码上传安装后前端显示错乱乱码问题解决方案
    PbootCMS前端显示错乱或乱码问题可能是由多种原因造成的,下面是一些可能的解决方案:检查字符集设置:确认前端页面的字符集设置是否正确。通常在HTML头部会有一个<meta>标签定义字符集,例如<metacharset="UTF-8">。同时检查PbootCMS后台的字符集设置是否与前端一致,确保数据库和......
  • 【题解】【归并排序】—— [NOIP2011 普及组] 瑞士轮
    【题解】【归并排序】——[NOIP2011普及组]瑞士轮[NOIP2011普及组]瑞士轮题目背景题目描述输入格式输出格式输入输出样例输入#1输出#1提示1.思路解析2.AC代码[NOIP2011普及组]瑞士轮通往洛谷的传送门题目背景在双人对决的竞技性比赛,如乒乓球、羽毛球、......
  • P10603 BZOJ4372 烁烁的游戏 题解
    题目传送门前置知识动态树分治|动态开点线段树|标记永久化解法考虑动态点分治。两种操作本质上是将luoguP6329【模板】点分树|震波的操作互换了下,将原需支持单点修改、区间查询的数据结构换成需支持区间修改、单点查询的数据结构即可。区间修改、单点查询的动态开......
  • Light Bulbs (Hard Version) 题解
    提供一个非常另类的解法,没有异或哈希,没有建图,没有缩点和强连通分量,而是使用了并查集和线段树的算法。由于每个颜色恰好有两种,我们考虑把两个颜色的位置\(i,j\)变成一段区间\([i,j]\)(\(i<j\)),然后每个颜色就能用一段区间\([l,r]\)表示。根据题意,如果我们激活了一个区间\([l,......
  • 【MX-J3-T3+】Tuple+ 题解
    一个比较自然的思路就是对于每个三元组\((u_i,v_i,w_i)\),把\((v_i,w_i)\)这个二元组放在属于\(u_i\)的vector里面。然后对于每一个\(i\in[1,n-3]\),把\(i\)的vector里面的所有二元组\((x,y)\)当作一条连接\(x,y\)的无向边,则我们的目的是在图中找出所有的三元环\(......