Codeforces Round #739 (Div. 3)


Codeforces Round #739 (Div. 3)

div3掉大分,不考算法考手速真來不及,還是得多練

目錄:

A. Dislike of Threes

B. Who's Opposite?

C. Infinity Table

D. Make a Power of Two

E. Polycarp and String Transformation

F2. Nearest Beautiful Number (hard version)

 

 

 

A. Dislike of Threes

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
input

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
output

——————————————————————————

題意:

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;
} 
Accepted
 
B. Who's Opposite?

time limit per test : 1 second
memory limit per test : 256 megabytes

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.

A sample of a circle of 6 persons. The orange arrows indicate who is looking at whom.

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 ab, and c, no such circle exists, output -1.

Input

The first line contains one integer tt (1t10^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
input
Output

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
output
Note

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

 ==返回目錄

 

C. Infinity Table

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 figure shows the placement of the numbers from 1 to 10. The following actions are denoted by the arrows.

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.

Input

The first line contains one integer t (1t100) — the number of test cases. Then tt test cases follow.

Each test case consists of one line containing one integer k (1k10^9) which location must be found.

7
11
14
5
4
1
2
1000000000
input
Output

For each test case, output in a separate line two integers r and c (r,c1) 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
output

——————————————————————————

題意:

從左上角開始,延圖示路線先向下,再向右增大數字。記豎向坐標為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;
}
Accepted

 ==返回目錄

 

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

  ==返回目錄

 

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
input

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
output

—————————————————————————— 

題意:

思路:
從后往前掃,前綴和預處理

代碼:

 ==返回目錄

 

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
input

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
output

—————————————————————————— 

題意:

思路:
dfs暴力

代碼:

 ==返回目錄

吃飯去了 回來繼續碼= =


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM