A. My First Sorting Problem
题面翻译
有 t t t 组数据,每组数据给你两个整数 x x x 和 y y y 。
输出两个整数: x x x 和 y y y 中较小的那个,然后是 x x x 和 y y y 中较大的那个。
题目描述
You are given two integers $ x $ and $ y $ .
Output two integers: the minimum of $ x $ and $ y $ , followed by the maximum of $ x $ and $ y $ .
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 100 $ ) — the number of test cases.
The only line of each test case contains two space-separated integers $ x $ and $ y $ ( $ 0 \leq x, y \leq 9 $ ).
输出格式
For each test case, output two integers: the minimum of $ x $ and $ y $ , followed by the maximum of $ x $ and $ y $ .
样例 #1
样例输入 #1
10
1 9
8 4
1 4
3 4
2 0
2 4
6 9
3 3
0 0
9 9
样例输出 #1
1 9
4 8
1 4
3 4
0 2
2 4
6 9
3 3
0 0
9 9
无脑代码
#include<iostream>
using namespace std;
int a,b;
int T;
int main(){
cin>>T;
while(T--){
cin>>a>>b;
cout<<min(a,b)<<" "<<max(a,b)<<endl;
}
return 0;
}
B. Different String
题面翻译
题目描述
给定一个以小写字母构成的字符串 s s s。
现在你的任务是,重新排列 s s s 的字符以形成一个不等于 s s s 的新字符串 r r r。
输入格式
本题单个测试点内包含多组测试数据。
第一行包含一个整数 t t t( 1 ≤ t ≤ 1000 1\leq t\leq 1000 1≤t≤1000),表示测试数据组数。
每个测试用例的唯一一行包含一个字符串 s s s,长度最多为 10 10 10,由小写英文字母组成。
输出格式
对于每个测试用例,如果不存在语句中描述的字符串 r r r,则输出NO
。
否则,输出YES
。然后,输出一行一个字符串 r r r,由字符串 s s s 的字母组成。
你可以以任何大小写形式输出YES
和NO
。
如果可以有多个答案,则可以输出其中任何一个。
题目描述
You are given a string $ s $ consisting of lowercase English letters.
Rearrange the characters of $ s $ to form a new string $ r $ that is not equal to $ s $ , or report that it’s impossible.
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases.
The only line of each test case contains a string $ s $ of length at most $ 10 $ consisting of lowercase English letters.
输出格式
For each test case, if no such string $ r $ exists as described in the statement, output “NO” (without quotes).
Otherwise, output “YES” (without quotes). Then, output one line — the string $ r $ , consisting of letters of string $ s $ .
You can output “YES” and “NO” in any case (for example, strings “yEs”, “yes”, and “Yes” will be recognized as a positive response).
If multiple answers are possible, you can output any of them.
样例 #1
样例输入 #1
8
codeforces
aaaaa
xxxxy
co
d
nutdealer
mwistht
hhhhhhhhhh
样例输出 #1
YES
forcodesec
NO
YES
xxyxx
YES
oc
NO
YES
undertale
YES
thtsiwm
NO
提示
In the first test case, another possible answer is $ \texttt{forcescode} $ .
In the second test case, all rearrangements of $ \texttt{aaaaa} $ are equal to $ \texttt{aaaaa} $ .
思路
就判断位数是不是为一和是不是全部相同,此时如果成立就输出 NO
。
#include<iostream>
#include<map>
using namespace std;
string s;
int T;
int main(){
cin>>T;
while(T--){
cin>>s;
map<char,int>S;
for(char x:s){
S[x]++;
}
int n=s.size();
if(n==1||S[s[0]]==n){
puts("NO");
}else{
puts("YES");
cout<<s[n-1]<<s.substr(0,n-1)<<endl;
}
}
return 0;
}
C. Clock and Strings
题面翻译
输入 t ( 1 ≤ t ≤ 5940 ) t(1\le t\le 5940) t(1≤t≤5940) 行,每行 4 4 4 个整数 a , b , c , d ( 1 ≤ a , b , c , d ≤ 12 ) a,b,c,d(1\le a,b,c,d\le 12) a,b,c,d(1≤a,b,c,d≤12),表示钟面上 a , b a, b a,b 和 c , d c, d c,d 两条线段的四个端点所对应的刻度,问这两条线段会不会在钟面上相交。如果会,则输出 YES
,不会则输出 NO
。
题目描述
There is a clock labeled with the numbers $ 1 $ through $ 12 $ in clockwise order, as shown below.
In this example, $ (a,b,c,d)=(2,9,10,6) $ , and the strings intersect.
Alice and Bob have four distinct integers $ a $ , $ b $ , $ c $ , $ d $ not more than $ 12 $ . Alice ties a red string connecting $ a $ and $ b $ , and Bob ties a blue string connecting $ c $ and $ d $ . Do the strings intersect? (The strings are straight line segments.)
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 5940 $ ) — the number of test cases.
The only line of each test case contains four distinct integers $ a $ , $ b $ , $ c $ , $ d $ ( $ 1 \leq a, b, c, d \leq 12 $ ).
输出格式
For each test case, output “YES” (without quotes) if the strings intersect, and “NO” (without quotes) otherwise.
You can output “YES” and “NO” in any case (for example, strings “yEs”, “yes”, and “Yes” will be recognized as a positive response).
样例 #1
样例输入 #1
15
2 9 10 6
3 8 9 1
1 2 3 4
5 3 4 12
1 8 2 10
3 12 11 8
9 10 12 1
12 1 10 2
3 12 6 9
1 9 8 4
6 7 9 12
7 12 9 6
10 12 11 1
3 9 6 12
1 4 3 5
样例输出 #1
YES
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
提示
The first test case is pictured in the statement.
In the second test case, the strings do not intersect, as shown below.
思路
我们为了研究方便就把 a , c a,c a,c 看成比较大的数,我们可以固定红色的线,去看蓝色的线怎么样才能没交点,那么剩下的都是有交点的。
#include<iostream>
using namespace std;
int T;
int a,b,c,d;
int main(){
cin>>T;
while(T--){
cin>>a>>b>>c>>d;
if(a>b)swap(a,b);
if(c>d)swap(c,d);
if(b>=d&&c<=a&&d>=a||a<=c&&b>=c&&b<=d){
puts("YES");
}else{
puts("NO");
}
}
return 0;
}
D. Binary Cut
题面翻译
题目描述
给定一个二进制字符串 $ ^{\dagger} $ 。请找到您需要将其切割成的最小片段数,将生成的片段重新排列成有序的二进制字符串。
请注意:
- 每个字符必须恰好位于其中一个片段中;
- 这些片段必须是原始字符串的连续子字符串;
- 你必须在重排中使用所有的片段。
† ^{\dagger} †二进制字符串是由字符 $ \texttt{0}$ 和 1 \texttt{1} 1 组成的字符串。排序后的二进制字符串是一个二进制字符串,使得所有字符 0 \texttt{0} 0 位于所有字符 1 \texttt{1} 1 之前。
输入格式
第一行包含一个整数 t t t( 1 ≤ t ≤ 500 1\leq t\leq 500 1≤t≤500)——测试用例的数量。
每个测试用例的唯一一行包含一个由 0 \texttt{0} 0 和 1 \texttt1 1 组成的字符串 s s s ( 1 ≤ ∣ s ∣ ≤ 500 1 \leq |s| \leq 500 1≤∣s∣≤500),其中 ∣ s ∣ |s| ∣s∣ 表示字符串 s s s 的长度。
输出格式
对于每个测试用例,输出将字符串重新排列为有序二进制字符串所需的最小分割数量。
题目描述
You are given a binary string $ ^{\dagger} $ . Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.
Note that:
- each character must lie in exactly one of the pieces;
- the pieces must be contiguous substrings of the original string;
- you must use all the pieces in the rearrangement.
$ ^{\dagger} $ A binary string is a string consisting of characters $ \texttt{0} $ and $ \texttt{1} $ . A sorted binary string is a binary string such that all characters $ \texttt{0} $ come before all characters $ \texttt{1} $ .
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 500 $ ) — the number of test cases.
The only line of each test case contains a single string $ s $ ( $ 1 \leq |s| \leq 500 $ ) consisting of characters $ \texttt{0} $ and $ \texttt{1} $ , where $ |s| $ denotes the length of the string $ s $ .
输出格式
For each test case, output a single integer — the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.
样例 #1
样例输入 #1
6
11010
00000000
1
10
0001111
0110
样例输出 #1
3
1
1
2
1
2
提示
The first test case is pictured in the statement. It can be proven that you can’t use fewer than $ 3 $ pieces.
In the second and third test cases, the binary string is already sorted, so only $ 1 $ piece is needed.
In the fourth test case, you need to make a single cut between the two characters and rearrange them to make the string $ \texttt{01} $ .
思路
贪心题,我们这边要特别注意下,本道题是问块数,不是问切割数;我们通过多列几个样例可以看出如下性质:
- 当我们遇到一段全是 0 或全是 1 的字符串,就不需要分开;
- 当我们遇到 01 这个子串时,我们只能保留一个 01 子串;
- 当我们遇到 10 就一定得分段。
- 最后的答案要+1,因为本身就是一段。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 510;
int T;
int main(){
cin>>T;
while(T--){
string a,b;
cin>>a;
b=a;
sort(b.begin(),b.end());
if(a==b){
puts("1");
continue;
}
int n=b.size();
int res=0,pos=0;
for(int i=1;i<n;i++){
if(a[i]!=a[i-1])res++;
if(a[i]=='1'&&a[i-1]=='0')pos=1;
}
if(pos)res--;
cout<<res+1<<endl;
}
return 0;
}
E. Find the Car
题面翻译
一条笔直的公路上有 k k k 个标志,已知这条公路的长度 n n n,第 i i i 个标志位于 a i a_{i} ai 点,一辆大巴车从 0 0 0 点出发,从一个站点到另一个站点匀速行驶,并在 b i b_{i} bi 分钟经过第 i i i 个标志。现给出 q q q 个询问,求大巴车经过 d d d 点的时间(向下取整)。
输入有 t t t 组,对于每组数据的 q q q 个询问,输出一行,用空格间隔。
对于 100 % 100\% 100% 的数据, 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1≤t≤104, k ≤ n ≤ 1 0 9 k\le n\le 10^9 k≤n≤109, 1 ≤ k , q ≤ 1 0 5 1\le k,q\le 10^5 1≤k,q≤105, 1 ≤ a 1 < a 2 < … < a k = n 1\le a_1<a_2<\ldots<a_k=n