BNUZ 日常训练2021.3.5
今天是个AK的好日子
A - Dense Array
Problem Description
Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1≤i≤n−1), this condition must be satisfied:
max(a[i],a[i+1])min(a[i],a[i+1])≤2
For example, the arrays [1,2,3,4,3], [1,1,1] and [5,10] are dense. And the arrays [5,11], [1,4,2], [6,6,1] are not dense.
You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.
For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,3−−,5−−,10,6−−,4−−,2−−,1] (there are other ways to build such a).
Input
The first line contains one integer t (1≤t≤1000). Then t test cases follow.
The first line of each test case contains one integer n (2≤n≤50) — the length of the array a.
The next line contains n integers a1,a2,…,an (1≤ai≤50).
Output
For each test case, output one integer — the minimum number of numbers that must be added to the array to make it dense.
Example
Input
6
4
4 2 10 1
2
1 3
2
6 1
3
1 4 2
5
1 2 3 4 3
12
4 31 25 50 30 20 34 46 42 16 15 16
Output
5
1
2
1
0
3
思路要点
分组比对,每两个数进行次比对
较小的数每次乘二与较大的数比对并记一次。
AC题解
#include <bits/stdc++.h>
int main(){
int t;
scanf("%d", &t);
while(t --){
int n, a[2000] = {0}, x, y, u, s = 0;
scanf("%d", &n);
for(int i = 0 ;i < n; i ++){
scanf("%d", &a[i]);
}
for(int i = 0; i < n - 1; i ++){
if(a[i] * 2 < a[i + 1] || a[i] > a[i + 1] * 2){
x = a[i]; y = a[i + 1];
if(x > y) {
u = x;
x = y;
y = u;
}
while(1){
x = x * 2;
s ++;
if(x * 2 >= y) break;
}
}
}
printf("%d\n", s);
}
return 0;
}
B - Balanced Remainders
Problem Description
You are given a number n (divisible by 3) and an array a[1…n]. In one move, you can increase any of the array elements by one. Formally, you choose the index i (1≤i≤n) and replace ai with ai+1. You can choose the same index i multiple times for different moves.
Let’s denote by c0, c1 and c2 the number of numbers from the array a that have remainders 0, 1 and 2 when divided by the number 3, respectively. Let’s say that the array a has balanced remainders if c0, c1 and c2 are equal.
For example, if n=6 and a=[0,2,5,5,4,8], then the following sequence of moves is possible:
- initially c0=1, c1=1 and c2=4, these values are not equal to each other. Let’s increase a3, now the array a=[0,2,6,5,4,8];
- c0=2, c1=1 and c2=3, these values are not equal. Let’s increase a6, now the array a=[0,2,6,5,4,9];
- c0=3, c1=1 and c2=2, these values are not equal. Let’s increase a1, now the array a=[1,2,6,5,4,9];
- c0=2, c1=2 and c2=2, these values are equal to each other, which means that the array a has balanced remainders.
Find the minimum number of moves needed to make the array a have balanced remainders.
Input
The first line contains one integer t (1≤t≤104). Then t test cases follow.
The first line of each test case contains one integer n (3≤n≤3⋅104) — the length of the array a. It is guaranteed that the number n is divisible by 3.
The next line contains n integers a1,a2,…,an (0≤ai≤100).
It is guaranteed that the sum of n over all test cases does not exceed 150000.
Output
For each test case, output one integer — the minimum number of moves that must be made for the a array to make it have balanced remainders.
Example
Input
4
6
0 2 5 5 4 8
6
2 0 2 1 0 0
9
7 1 3 4 2 10 3 9 6
6
0 1 2 3 4 5
Output
3
1
3
0
思路要点
计算出所给数组余0,余1,余2的个数x0, x1, x2
x0→x1; x1→x2; x2→x0进行一加一减,直到x0, x1, x2相等
AC题解
#include <bits/stdc++.h>
int main(){
int t;
scanf("%d", &t);
while(t --){
int n, a[50000] = {0}, s = 0, x0 = 0, x1 = 0, x2 = 0;
scanf("%d", &n);
for(int i = 0 ;i < n; i ++){
scanf("%d", &a[i]);
if(a[i] % 3 == 0) x0 ++;
if(a[i] % 3 == 1) x1 ++;
if(a[i] % 3 == 2) x2 ++;
}
while(1){
if(x0 == x1 && x1== x2) break;
if(x0 > x1){
x0 --; x1 ++; s ++;
}
if(x1 > x2){
x1 --; x2 ++; s ++;
}
if(x2 > x0){
x2 --; x0 ++; s ++;
}
}
printf("%d\n", s);
}
return 0;
}
C - Favorite Sequence
Problem Description
Polycarp has a favorite sequence a[1…n] consisting of n integers. He wrote it out on the whiteboard as follows:
- he wrote the number a1 to the left side (at the beginning of the whiteboard);
- he wrote the number a2 to the right side (at the end of the whiteboard);
- then as far to the left as possible (but to the right from a1), he wrote the number a3;
- then as far to the right as possible (but to the left from a2), he wrote the number a4;
- Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard.
The beginning of the result looks like this (of course, if n≥4).
For example, if n=7 and a=[3,1,4,1,5,9,2], then Polycarp will write a sequence on the whiteboard [3,4,5,2,9,1,1].
You saw the sequence written on the whiteboard and now you want to restore Polycarp’s favorite sequence.
Input
The first line contains a single positive integer t (1≤t≤300) — the number of test cases in the test. Then t test cases follow.
The first line of each test case contains an integer n (1≤n≤300) — the length of the sequence written on the whiteboard.
The next line contains n integers b1,b2,…,bn (1≤bi≤109) — the sequence written on the whiteboard.
Output
Output t answers to the test cases. Each answer — is a sequence a that Polycarp wrote out on the whiteboard.
Example
Input
6
7
3 4 5 2 9 1 1
4
9 2 7 1
11
8 4 3 1 2 7 8 7 9 4 2
1
42
2
11 7
8
1 1 1 1 1 1 1 1
Output
3 1 4 1 5 9 2
9 1 2 7
8 2 4 4 3 9 1 7 2 8 7
42
11 7
1 1 1 1 1 1 1 1
思路要点
两边依次输出即可(我数组开小了所以错了一次
AC题解
#include <bits/stdc++.h>
int main(){
int t;
scanf("%d", &t);
while(t --){
int n, a[2000], r = 0, m;
scanf("%d", &n);m = n;
for (int i = 1; i <= n; i ++){
scanf("%d", &a[i]);
}
int i = 1;
while(1){
if(r == m) break;
printf("%d ", a[i]);
i = i + n - 1;
r ++;
if(r == m) break;
printf("%d ", a[i]);
i = i - n + 2;
n -= 2;
r ++;
}
printf("\n");
}
return 0;
}
D - Last Year’s Substring
Problem Description
Polycarp has a string s[1…n] of length n consisting of decimal digits. Polycarp performs the following operation with the string s no more than once (i.e. he can perform operation 0 or 1 time):
- Polycarp selects two numbers i and j (1≤i≤j≤n) and removes characters from the s string at the positions i,i+1,i+2,…,j (i.e. removes substring s[i…j]). More formally, Polycarp turns the string s into the string s1s2…si−1sj+1sj+2…sn.
For example, the string s=“20192020” Polycarp can turn into strings:
- “2020” (in this case (i,j)=(3,6) or (i,j)=(1,4));
- “2019220” (in this case (i,j)=(6,6));
- “020” (in this case (i,j)=(1,5));
- other operations are also possible, only a few of them are listed above.
Polycarp likes the string “2020” very much, so he is wondering if it is possible to turn the string s into a string “2020” in no more than one operation? Note that you can perform zero operations.
Input
The first line contains a positive integer t (1≤t≤1000) — number of test cases in the test. Then t test cases follow.
The first line of each test case contains an integer n (4≤n≤200) — length of the string s. The next line contains a string s of length n consisting of decimal digits. It is allowed that the string s starts with digit 0.
Output
For each test case, output on a separate line:
- “YES” if Polycarp can turn the string s into a string “2020” in no more than one operation (i.e. he can perform 0 or 1 operation);
- “NO” otherwise.
You may print every letter of “YES” and “NO” in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
6
8
20192020
8
22019020
4
2020
5
20002
6
729040
6
200200
Output
YES
YES
YES
NO
NO
NO
思路要点
由题意得,当字符串中间出现“2020”是不算的因为不能一次删除两边的数
所以我们只用判断字符串两头的字符即可
①字符开头或结尾是“2020”
②字符开头是“2”,结尾是“020”
③字符开头是“20”,结尾是“20”
④字符开头是“202”,结尾是“0”
符和上述任意一条件即满足题意输出YES
AC题解
#include <bits/stdc++.h>
int main(){
int t;
scanf("%d", &t);
while(t --){
int l, f = 0;
scanf("%d", &l); getchar();
char a[1100];
gets(a);
l --;
if(a[0] == '2' && a[1] == '0' && a[2] == '2' && a[3] == '0') f = 1;
if(a[l - 3] == '2' && a[l - 2] == '0' && a[l - 1] == '2' && a[l] == '0') f = 1;
if(a[0] == '2' && a[1] == '0' && a[l - 1] == '2' && a[l] == '0') f = 1;
if(a[0] == '2' && a[1] == '0' && a[2] == '2' && a[l] == '0') f = 1;
if(a[0] == '2' && a[l - 2] == '0' && a[l - 1] == '2' && a[l] == '0') f = 1;
if(f) printf("YES\n");
else printf("NO\n");
}
return 0;
}
E - Unique Number
Problem Description
You are given a positive number x. Find the smallest positive integer number that has the sum of digits equal to x and all digits are distinct (unique).
Input
The first line contains a single positive integer t (1≤t≤50) — the number of test cases in the test. Then t test cases follow.
Each test case consists of a single integer number x (1≤x≤50).
Output
Output t answers to the test cases:
- if a positive integer number with the sum of digits equal to x and all digits are different exists, print the smallest such number;
- otherwise print -1.
Example
Input
4
1
5
15
50
Output
1
5
69
-1
思路要点
由题意得,123456789为最大的数,其和为45,所以只有小于等于45时才有答案。
由于项目过少,我们可以找到规律一项一项的弄(手速题,ctrl + c, ctrl + v, yyds
AC题解
#include <bits/stdc++.h>
int main(){
int t;
scanf("%d", &t);
while(t --){
int n;
scanf("%d", &n);
if(n > 45) printf("-1\n");
else{
if(n <= 9) printf("%d\n", n);
else if(n == 10) printf("19\n");
else if(n == 11) printf("29\n");
else if(n == 12) printf("39\n");
else if(n == 13) printf("49\n");
else if(n == 14) printf("59\n");
else if(n == 15) printf("69\n");
else if(n == 16) printf("79\n");
else if(n == 17) printf("89\n");
else if(n == 18) printf("189\n");
else if(n == 19) printf("289\n");
else if(n == 20) printf("389\n");
else if(n == 21) printf("489\n");
else if(n == 22) printf("589\n");
else if(n == 23) printf("689\n");
else if(n == 24) printf("789\n");
else if(n == 25) printf("1789\n");
else if(n == 26) printf("2789\n");
else if(n == 27) printf("3789\n");
else if(n == 28) printf("4789\n");
else if(n == 29) printf("5789\n");
else if(n == 30) printf("6789\n");
else if(n == 31) printf("16789\n");
else if(n == 32) printf("26789\n");
else if(n == 33) printf("36789\n");
else if(n == 34) printf("46789\n");
else if(n == 35) printf("56789\n");
else if(n == 36) printf("156789\n");
else if(n == 37) printf("256789\n");
else if(n == 38) printf("356789\n");
else if(n == 39) printf("456789\n");
else if(n == 40) printf("1456789\n");
else if(n == 41) printf("2456789\n");
else if(n == 42) printf("3456789\n");
else if(n == 43) printf("13456789\n");
else if(n == 44) printf("23456789\n");
else if(n == 45) printf("123456789\n");
}
}
return 0;
}