Codeforces Round #739 (Div. 3)
div3掉大分,不考算法考手速真来不及,还是得多练
E. Polycarp and String Transformation
F2. Nearest Beautiful Number (hard version)
time limit per test : 1 second
memory limit per test : 256 megabytes
Polycarp doesn't like integers that are divisible by 3 or end with the digit 3 in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.
Polycarp starts to write out the positive (greater than 0) integers which he likes: 1,2,4,5,7,8,10,11,14,16,…. Output the k-th element of this sequence (the elements are numbered from 1).
Input
The first line contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing one integer k (1≤k≤1000).

10 1 2 3 4 5 6 7 8 9 1000
Output
For each test case, output in a separate line one integer x — the k-th element of the sequence that was written out by Polycarp.

1 2 4 5 7 8 10 11 14 1666
——————————————————————————
题意:
Polycarp不喜欢是①3的倍数②结尾有3的正整数。输入k,输出第k个满足条件的数。
思路:
k的范围只有1000,预处理打表秒杀= =
代码:

#include<bits/stdc++.h> using namespace std; int main() { int a[1005],t,k,n=1; for(int i=1;;i++) { if(i%3!=0&&i%10!=3) a[n++]=i; if(n>1000) break; } cin>>t; while(t--) { cin>>k; cout<<a[k]<<endl; } return 0; }
Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number 1. Each person is looking through the circle's center at the opposite person.

You don't know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number a is looking at the person with the number b (and vice versa, of course). What is the number associated with a person being looked at by the person with the number c? If, for the specified a, b, and c, no such circle exists, output -1.
The first line contains one integer tt (1≤t≤10^4) — the number of test cases. Then tt test cases follow.
Each test case consists of one line containing three distinct integers a, b, c (1≤a,b,c≤10^8).

7 6 2 4 2 3 1 2 4 10 5 3 4 1 3 2 2 5 4 4 3 2
For each test case output in a separate line a single integer dd — the number of the person being looked at by the person with the number cc in a circle such that the person with the number aa is looking at the person with the number bb. If there are multiple solutions, print any of them. Output −1 if there's no circle meeting the given conditions.

8 -1 -1 -1 4 1 -1
In the first test case, there's a desired circle of 8 people. The person with the number 66 will look at the person with the number 2 and the person with the number 8 will look at the person with the number 4.
In the second test case, there's no circle meeting the conditions. If the person with the number 2 is looking at the person with the number 3, the circle consists of 2 people because these persons are neighbors. But, in this case, they must have the numbers 1 and 2, but it doesn't meet the problem's conditions.
In the third test case, the only circle with the persons with the numbers 2 and 4 looking at each other consists of 4 people. Therefore, the person with the number 10 doesn't occur in the circle.
——————————————————————————
题意:
有偶数个人站成一个环,从1~2k顺时针编号。输入a,b,c,已知a与b面对面,求c对面是谁。
思路:
结合题图很容易读懂题意,然后可以发现,两个相对的人序号差值的绝对值等于半圈的长。计算w=abs(a-b)得到半圈长,ww=2*w得到总人数。若a,b,c中任何一个比总人数的值还要大,则证明不成立,输出-1。其余情况都成立,若c<=w,则c在前半圈,输出c+w。若c>w,则c在后半圈,输出c-w。
代码:

#include<bits/stdc++.h> using namespace std; int main() { long long t,a,b,c,w,ww; cin>>t; while(t--) { cin>>a>>b>>c; w=abs(a-b); ww=2*w; if(a>ww||b>ww||c>ww) printf("-1\n"); else printf("%d\n",c>w?c-w:c+w); } return 0; }
time limit per test : 1 second
memory limit per test : 256 megabytes
Polycarp has found a table having an infinite number of rows and columns. The rows are numbered from 1, starting from the topmost one. The columns are numbered from 1, starting from the leftmost one.
Initially, the table hasn't been filled and Polycarp wants to fix it. He writes integers from 1 and so on to the table as follows.

The leftmost topmost cell of the table is filled with the number 1. Then he writes in the table all positive integers beginning from 2 sequentially using the following algorithm.
First, Polycarp selects the leftmost non-filled cell in the first row and fills it. Then, while the left neighbor of the last filled cell is filled, he goes down and fills the next cell. So he goes down until the last filled cell has a non-filled neighbor to the left (look at the vertical arrow going down in the figure above).
After that, he fills the cells from the right to the left until he stops at the first column (look at the horizontal row in the figure above). Then Polycarp selects the leftmost non-filled cell in the first row, goes down, and so on.
A friend of Polycarp has a favorite number k. He wants to know which cell will contain the number. Help him to find the indices of the row and the column, such that the intersection of the row and the column is the cell containing the number k.
The first line contains one integer t (1≤t≤100) — the number of test cases. Then tt test cases follow.
Each test case consists of one line containing one integer k (1≤k≤10^9) which location must be found.

7 11 14 5 4 1 2 1000000000
For each test case, output in a separate line two integers r and c (r,c≥1) separated by spaces — the indices of the row and the column containing the cell filled by the number k, respectively.

2 4 4 3 1 3 2 1 1 1 1 2 31623 14130
——————————————————————————
题意:
从左上角开始,延图示路线先向下,再向右增大数字。记竖向坐标为r,横向坐标为c。输入序号k,输出k所在的位置坐标。
思路:
首先要判断k在第几层,由于图的每一层走完刚好是i²,所以直接得到u=(int)sqrt(k),若k不是平方数,则还需要u+=1。记t=k-u*u,然后判断t,如果t<=u则数字k在向下的过程中,否则就在向左的过程中。每一层的最外圈个数恰好是竖着u个,横着u-1个。很容易就可以推出结果。
代码:

#include<bits/stdc++.h> using namespace std; int main() { int t,k,u; cin>>t; while(t--) { cin>>k; u=sqrt(k); k-=u*u; if(k==0) k=2*u-1; else u++; if(k<=u) cout<<k<<" "<<u<<endl; else cout<<u<<" "<<2*u-k<<endl; } return 0; }
D. Make a Power of Two
time limit per test : 1 second
memory limit per test : 256 megabytes
You are given an integer n. In 1 move, you can do one of the following actions:
erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is "empty");
add one digit to the right.
The actions may be performed in any order any number of times.
Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number 301 the digit 3, the result is the number 01 (not 1).
You need to perform the minimum number of actions to make the number any power of 2 (i.e. there's an integer k (k≥0) such that the resulting number is equal to 2……k). The resulting number must not have leading zeroes.
E.g. consider n=1052. The answer is equal to 2. First, let's add to the right one digit 4 (the result will be 10524). Then let's erase the digit 5, so the result will be 1024 which is a power of 2.
E.g. consider n=8888. The answer is equal to 3. Let's erase any of the digits 8 three times. The result will be 8 which is a power of 2.
Input
The first line contains one integer t (1≤t≤10^4) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing one integer n (1≤n≤10^9).
Output
For each test case, output in a separate line one integer m — the minimum number of moves to transform the number into any power of 2.
——————————————————————————
题意:
给定整数n,可以从整数n中取出任意个数的数字,在n的最右侧添加任意个数的数字,使其满足操作后的数字是2^k,其中k≥0。
思路:
用字符型读入整数n,预处理一个表,表中记录2的0次到2的62次(2^63就爆long long了,且2^62有17位,本题上限16位整数)。然后用n去对应2^0到2^62,一位一位暴力匹配,维护最小值即可得到答案。比如1024的第一位是1,那就在n里面从左往右找到1,同理去按照顺序找0 2 4,其余的都需要被丢掉。举个例子的例子:99109992匹配1024,1024的第一个是1,所以在n里面找到最前的1,然后把1前面的两个99去掉。然后匹配0,不需要擦欧洲哦。再下一个是2,0到最近的2中间隔了999,操作3次把这个三个999去掉。此时一共操作了5次,把99109992变成了102,最后操作1次在最右侧添加4,得到1024。所以99109992匹配1024总共需要2+3+1=6次,同理从2^0匹配到2^62。
代码:

#include<bits/stdc++.h> using namespace std; int t,n,ans,k,f,cnt; char ch[70][20]; void init() { long long temp,pow2=1; for(int i=0;i<63;i++) { temp=pow2; cnt=0; while(temp) { ch[i][++cnt]=temp%10+'0'; temp/=10; } ch[i][0]=cnt; pow2*=2; } } int main() { init(); cin>>t; while(t--) { char s[15]; cin>>s; n=strlen(s); ans=n+1; for(int i=0;i<63;i++) { f=1; cnt=0; k=ch[i][0]; for(int j=0;j<n;j++) { if(s[j]==ch[i][k]) { k--; if(k<1) { f=0; ans=min(ans,cnt+n-j-1); break; } } else cnt++; } if(f) ans=min(ans,cnt==n?cnt+1:cnt+k); } cout<<ans<<endl; } return 0; }
E. Polycarp and String Transformation
time limit per test : 3.0 s
memory limit per test : 256 megabytes
Polycarp has a string s. Polycarp performs the following actions until the string s is empty (t is initially an empty string):
he adds to the right to the string t the string s, i.e. he does t=t+s, where t+s is a concatenation of the strings t and s;
he selects an arbitrary letter of s and removes from s all its occurrences (the selected letter must occur in the string s at the moment of performing this action).
Polycarp performs this sequence of actions strictly in this order.
Note that after Polycarp finishes the actions, the string s will be empty and the string t will be equal to some value (that is undefined and depends on the order of removing).
E.g. consider s="abacaba" so the actions may be performed as follows:
t="abacaba", the letter 'b' is selected, then s="aacaa";
t="abacabaaacaa", the letter 'a' is selected, then s="c";
t="abacabaaacaac", the letter 'c' is selected, then s="" (the empty string).
You need to restore the initial value of the string s using only the final value of t and find the order of removing letters from s.
Input
The first line contains one integer T (1≤T≤10^4) — the number of test cases. Then T test cases follow.
Each test case contains one string t consisting of lowercase letters of the Latin alphabet. The length of t doesn't exceed 5⋅10^5. The sum of lengths of all strings t in the test cases doesn't exceed 5⋅10^5.

7 abacabaaacaac nowyouknowthat polycarppoycarppoyarppyarppyrpprppp isi everywherevrywhrvryhrvrhrvhv haaha qweqeewew
Output
For each test case output in a separate line:
−1, if the answer doesn't exist;
two strings separated by spaces. The first one must contain a possible initial value of s. The second one must contain a sequence of letters — it's in what order one needs to remove letters from s to make the string t. E.g. if the string "bac" is outputted, then, first, all occurrences of the letter 'b' were deleted, then all occurrences of 'a', and then, finally, all occurrences of 'c'. If there are multiple solutions, print any one.

abacaba bac -1 polycarp lcoayrp is si everywhere ewyrhv -1 -1
——————————————————————————
题意:
思路:
从后往前扫,前缀和预处理
代码:
F2. Nearest Beautiful Number (hard version)
time limit per test : 1 second
memory limit per test : 256 megabytes
It is a complicated version of problem F1. The difference between them is the constraints (F1: k≤2, F2: k≤10).
You are given an integer n. Find the minimum integer x such that x≥n and the number x is k-beautiful.
A number is called k-beautiful if its decimal representation having no leading zeroes contains no more than k different digits. E.g. if k=2, the numbers 3434443, 55550, 777 and 21 are k-beautiful whereas the numbers 120, 445435 and 998244353 are not.
Input
The first line contains one integer t (1≤t≤10^4) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing two integers n and k (1≤n≤10^9, 1≤k≤10).

6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10
Output
For each test case output on a separate line x — the minimum k-beautiful integer such that x≥n.

2021 181111 34533 724542 999999999 12345678
——————————————————————————
题意:
思路:
dfs暴力
代码:
吃饭去了 回来继续码= =