CodeForces - 1277A
Hooray! Polycarp turned n years old! The Technocup Team sincerely congratulates Polycarp!
Polycarp celebrated all of his n birthdays: from the 1-th to the n-th. At the moment, he is wondering: how many times he turned beautiful number of years?
According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: 1, 77, 777, 44 and 999999. The following numbers are not beautiful: 12, 11110, 6969 and 987654321.
Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).
Help Polycarpus to find the number of numbers from 1 to n (inclusive) that are beautiful.
Input
The first line contains an integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.
Each test case consists of one line, which contains a positive integer n (1≤n≤109) — how many years Polycarp has turned.
Output
Print t integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between 1 and n, inclusive.
Example
Input
6
18
1
9
100500
33
1000000000
Output
10
1
9
45
12
81
Note
In the first test case of the example beautiful years are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 11.
题意:求给出数字之前有多少个“美丽数字”(由单一数字组成的数),想了一下大概就是每一位对应有9个,需要考虑的是最高位。
AC代码:
#include
using namespace std;
int main(){
int t,n,a;
cin>>t;
while (t--) {
a=0;
cin>>n;
for (int i=1;i<=n;i=i*10+1) {
a+=min(n/i,9);
}
cout<<a<<endl;
}
}
CodeForces - 1277B
There are n positive integers a1,a2,…,an. For the one move you can choose any even value c and divide by two all elements that equal c.
For example, if a=[6,8,12,6,3,12] and you choose c=6, and a is transformed into a=[3,8,12,3,3,12] after the move.
You need to find the minimal number of moves for transforming a to an array of only odd integers (each element shouldn’t be divisible by 2).
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains n (1≤n≤2⋅105) — the number of integers in the sequence a. The second line contains positive integers a1,a2,…,an (1≤ai≤109).
The sum of n for all test cases in the input doesn’t exceed 2⋅105.
Output
For t test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by 2).
Example
Input
4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7
Output
4
10
4
0
Note
In the first test case of the example, the optimal sequence of moves can be as follows:
before making moves a=[40,6,40,3,20,1];
choose c=6;
now a=[40,3,40,3,20,1];
choose c=40;
now a=[20,3,20,3,20,1];
choose c=20;
now a=[10,3,10,3,10,1];
choose c=10;
now a=[5,3,5,3,5,1] — all numbers are odd.
Thus, all numbers became odd after 4 moves. In 3 or fewer moves, you cannot make them all odd.
题意:给出一个数组,每次指定一个元素,对数组中的该元素进行除以2的操作,求最少的操作次数。思路是定义一个set数组,对输入数奇偶判断,偶数插入。在set容器中每次对最大的数进行除以二操作,判断后插入容器,容器会自动合并相同的元素,直到容器中没有偶数插入则输出答案
AC代码:
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
int n,m=0,k,ans=0;
set<int> a;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>k;
if(k%2==1) continue;
a.insert(k);
}
while(!a.empty())
{
ans++;
k=*a.rbegin();
a.erase(k);
k/=2;
if(k%2==1) continue;
a.insert(k);
}
cout<<ans<<endl;
}
}
CodeForces - 1281B
Your friend Jeff Zebos has been trying to run his new online company, but it’s not going very well. He’s not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he’s not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he’ll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor’s, then he’ll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor’s!
Given the string s representing Jeff’s product name and the string c representing his competitor’s product name, find a way to swap at most one pair of characters in s (that is, find two distinct indices i and j and swap si and sj) such that the resulting new name becomes strictly lexicographically smaller than c, or determine that it is impossible.
Note: String a is strictly lexicographically smaller than string b if and only if one of the following holds:
a is a proper prefix of b, that is, a is a prefix of b such that a≠b; Input Each test case consists of a single line containing two space-separated strings s and c (2≤|s|≤5000,1≤|c|≤5000). The strings s and c consists of uppercase English letters. It is guaranteed that the sum of |s| in the input is at most 5000 and the sum of the |c| in the input is at most 5000. Output the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than c. In case there are many possible such strings, you can output any of them; 3 Output AMAZON Note It is impossible to improve the product’s name in the second test case and satisfy all conditions. In the third test case, it is possible not to swap a pair of characters. The name “APPLE” is lexicographically smaller than “BANANA”. Note that there are other valid answers, e.g., “APPEL”. 题意:给你两个字符串a和c,要求在a内只交换一对字符,使得交换后的字符串比b小。思路是把a排序,记为b。如果b还是不满足则剪枝,然后找到和原字符串不同的字符记为Sa,Sb。然后再在a中从后往前找到和Sb相同的元素,在a中令这两个字符换位。 AC代码: CodeForces - 1281C We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by si. There is one cursor. The cursor’s location ℓ is denoted by an integer in {0,…,|s|}, with the following meaning: If ℓ=0, then the cursor is located before the first character of s. We also have a string c, which we call our clipboard, which starts out as empty. There are three types of actions: The Move action. Move the cursor one step to the right. This increments ℓ once. Perform the Move action once. It is guaranteed that ℓ≤|s| at any time. Input The first line of each test case contains a single integer x (1≤x≤106). The second line of each test case consists of the initial string s (1≤|s|≤500). It is guaranteed, that s consists of the characters “1”, “2”, “3”. It is guaranteed that the sum of x in a single file is at most 106. It is guaranteed that in each test case before the procedure will stop it will be true that ℓ≤|s| at any time. Output Example 4 Output 25 Note Step 1, Move once: we get ℓ=1. 题意:给你一个x,再给你一个由1,2,3组成的字符串s,长度为|s|,然后定义一个在0到|s|活动的cursor(游标),每移动游标一次,进行复制粘贴的操作(复制游标以后的字符串到clipboard,然后在游标处粘贴游标左边的字符-‘1’次(这里减一是因为之前省略了剪切的操作)),直到游标的位置到了x,求出s的长度。难理解题意的简单模拟。 AC代码: CodeForces - 1281D You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a r×c grid. Each cell on the grid represents a country. Each country has a dominant religion. There are only two religions in your world. One of the religions is called Beingawesomeism, who do good for the sake of being good. The other religion is called Pushingittoofarism, who do murders for the sake of being bad. Oh, and you are actually not really all-powerful. You just have one power, which you can use infinitely many times! Your power involves missionary groups. When a missionary group of a certain country, say a, passes by another country b, they change the dominant religion of country b to the dominant religion of country a. In particular, a single use of your power is this: The following image illustrates one possible single usage of your power. Here, A represents a country with dominant religion Beingawesomeism and P represents a country with dominant religion Pushingittoofarism. Here, we’ve chosen a 1×4 subgrid, the direction NORTH, and s=2 steps. What is the minimum number of usages of your power needed to convert everyone to Beingawesomeism? With god, nothing is impossible. But maybe you’re not god? If it is impossible to make Beingawesomeism the dominant religion in all countries, you must also admit your mortality and say so. Input The first line of each test case contains two space-separated integers r and c denoting the dimensions of the grid (1≤r,c≤60). The next r lines each contains c characters describing the dominant religions in the countries. In particular, the j-th character in the i-th line describes the dominant religion in the country at the cell with row i and column j, where: “A” means that the dominant religion is Beingawesomeism; Output Example 4 Output 2 Note Usage 1: In the third test case, it is impossible to convert everyone to Beingawesomeism, so the answer is “MORTAL”. 题意:模拟题。只有0,1,2,3,4,impossible这五种情况,分类: AC代码: 总结:五道模拟题,难度不高,但要求快速做出存在一定困难。还需多多刷题锻炼思维,来快速应对复杂的模拟。
There exists an integer 1≤i≤min(|a|,|b|) such that ai
The first line of input contains a single integer t (1≤t≤1500) denoting the number of test cases. The next lines contain descriptions of the test cases.
For each test case, output a single line containing a single string, which is either
three dashes (the string “—” without quotes) if it is impossible.
Example
Input
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
“- - -”(三个短杠不太好写)
APPLE
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string “AMAZON” is lexicographically smaller than “APPLE”.#include
D - Cut and Paste
If ℓ=|s|, then the cursor is located right after the last character of s.
If 0<ℓ<|s|, then the cursor is located between sℓ and sℓ+1.
We denote by sleft the string to the left of the cursor and sright the string to the right of the cursor.
The Cut action. Set c←sright, then set s←sleft.
The Paste action. Append the value of c to the end of the string s. Note that this doesn’t modify c.
The cursor initially starts at ℓ=0. Then, we perform the following procedure:
Perform the Cut action once.
Perform the Paste action sℓ times.
If ℓ=x, stop. Otherwise, return to step 1.
You’re given the initial string s and the integer x. What is the length of s when the procedure stops? Since this value may be very large, only find it modulo 109+7.
The first line of input contains a single integer t (1≤t≤1000) denoting the number of test cases. The next lines contain descriptions of the test cases.
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 109+7.
Input
5
231
7
2323
6
333
24
133321333
1438
1101
686531475
Let’s illustrate what happens with the first test case. Initially, we have s= 231. Initially, ℓ=0 and c=ε (the empty string). The following things happen if we follow the procedure above:
Step 2, Cut once: we get s= 2 and c= 31.
Step 3, Paste sℓ= 2 times: we get s= 23131.
Step 4: ℓ=1≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=2.
Step 2, Cut once: we get s= 23 and c= 131.
Step 3, Paste sℓ= 3 times: we get s= 23131131131.
Step 4: ℓ=2≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=3.
Step 2, Cut once: we get s= 231 and c= 31131131.
Step 3, Paste sℓ= 1 time: we get s= 23131131131.
Step 4: ℓ=3≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=4.
Step 2, Cut once: we get s= 2313 and c= 1131131.
Step 3, Paste sℓ= 3 times: we get s= 2313113113111311311131131.
Step 4: ℓ=4≠x=5, so we return to step 1.
Step 1, Move once: we get ℓ=5.
Step 2, Cut once: we get s= 23131 and c= 13113111311311131131.
Step 3, Paste sℓ= 1 times: we get s= 2313113113111311311131131.
Step 4: ℓ=5=x, so we stop.
At the end of the procedure, s has length 25.#include
E - Beingawesomeism
value of x is up to you;
choices will either be NORTH or SOUTH. If you choose a vertical
subgrid, your choices will either be EAST or WEST; You choose the
number s of steps;
that will travel s steps towards direction d. In each step, they
will visit (and in effect convert the dominant religion of) all s
countries they pass through, as detailed above.
missionary groups won’t leave the grid.
You are a being which believes in free will, for the most part. However, you just really want to stop receiving murders that are attributed to your name. Hence, you decide to use your powers and try to make Beingawesomeism the dominant religion in every country.
The first line of input contains a single integer t (1≤t≤2⋅104) denoting the number of test cases.
“P” means that the dominant religion is Pushingittoofarism.
It is guaranteed that the grid will only contain “A” or “P” characters. It is guaranteed that the sum of the r⋅c in a single file is at most 3⋅106.
For each test case, output a single line containing the minimum number of usages of your power needed to convert everyone to Beingawesomeism, or the string “MORTAL” (without quotes) if it is impossible to do so.
Input
7 8
AAPAAAAA
PPPPAAAA
PPPPAAAA
APAAPPPP
APAPPAPP
AAAAPPAP
AAAAPPAA
6 5
AAAAA
AAAAA
AAPAA
AAPAP
AAAPP
AAAPP
4 4
PPPP
PPPP
PPPP
PPPP
3 4
PPPP
PAAP
PPPP
1
MORTAL
4
In the first test case, it can be done in two usages, as follows:
Usage 2:
In the second test case, it can be done with just one usage of the power.
impossible:四周被B包围
0:全A
1:第一行||第一列||最后一行||最后一列 全为A
2:(假设A是高楼B是矮楼)南北方向看A存在一排||东西方向看A存在一排||四角存在A
3:上述都不满足,第一行||第一列||最后一行||最后一列 存在A
4:else#include