Codeforces Round #640 (Div. 4)

好长时间没来博客了 cf第一次div4 写个题解吧 嘿嘿
A. Sum of Round Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A positive (strictly greater than zero) integer is called round if it is of the form d00…0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 1 to 9 (inclusive) are round.

For example, the following numbers are round: 4000, 1, 9, 800, 90. The following numbers are not round: 110, 707, 222, 1001.

You are given a positive integer n (1≤n≤104). Represent the number n as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number n as a sum of the least number of terms, each of which is a round number.

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 is a line containing an integer n (1≤n≤104).

Output
Print t answers to the test cases. Each answer must begin with an integer k — the minimum number of summands. Next, k terms must follow, each of which is a round number, and their sum is n. The terms can be printed in any order. If there are several answers, print any of them.

Example
inputCopy
5
5009
7
9876
10000
10
outputCopy
2
5000 9
1
7
4
800 70 6 9000
1
10000
1
10
题解:把数拆成 x00…0的和 看样例就能懂了

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
int n,k;
int main()
{
    int K;cin>>K;
    while(K--)
    {
        string s;
        cin>>s;
        n=s.length();int v=1;
        s="l"+s;int ans=0;
        for(int i=n;i>=1;i--)if((s[i]-'0')!=0)ans++;cout<<ans<<endl;
        for(int i=n;i>=1;i--)
        {
            if((s[i]-'0')!=0)
            cout<<(s[i]-'0')*v<<" ";v*=10;
        }cout<<endl;
    }
    return 0;
}

B. Same Parity Summands
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two positive integers n (1≤n≤109) and k (1≤k≤100). Represent the number n as the sum of k positive integers of the same parity (have the same remainder when divided by 2).

In other words, find a1,a2,…,ak such that all ai>0, n=a1+a2+…+ak and either all ai are even or all ai are odd at the same time.

If such a representation does not exist, then report it.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.

Each test case is two positive integers n (1≤n≤109) and k (1≤k≤100).

Output
For each test case print:

YES and the required values ai, if the answer exists (if there are several answers, print any of them);
NO if the answer does not exist.
The letters in the words YES and NO can be printed in any case.

Example
inputCopy
8
10 3
100 4
8 7
97 2
8 8
3 10
5 3
1000000000 9
outputCopy
YES
4 2 4
YES
55 5 5 35
NO
NO
YES
1 1 1 1 1 1 1 1
NO
YES
3 1 1
YES
111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120
题解:问这个数能否拆分为k个奇数或k个偶数的和
如果是个奇数就判断是否可以用k-1个1 和一个 n-(k-1)构成 注意判断是否成立 同理偶数的话就是用k-1个2和一个n-2*(k-1)构成 注意判断是否成立

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
int n,k;
int main()
{
    int K;cin>>K;
    while(K--)
    {
        cin>>n>>k;
        if(n%2==0&&n>=2*k)
        {
            cout<<"YES\n";
            for(int i=1;i<k;i++)cout<<2<<" ";
            cout<<n-2*(k-1)<<endl;
        }
        else if((n-k+1)>0&&((n-k+1)&1))
        {
            cout<<"YES\n";
            for(int i=1;i<k;i++)cout<<1<<" ";
            cout<<n-k+1<<endl;
        }
        else cout<<"NO\n";
    }
    return 0;
}

C. K-th Not Divisible by n
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two positive integers n and k. Print the k-th positive integer that is not divisible by n.

For example, if n=3, and k=7, then all numbers that are not divisible by 3 are: 1,2,4,5,7,8,10,11,13…. The 7-th number among them is 10.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.

Each test case is two positive integers n (2≤n≤109) and k (1≤k≤109).

Output
For each test case print the k-th positive integer that is not divisible by n.

Example
inputCopy
6
3 7
4 12
2 1000000000
7 97
1000000000 1000000000
2 1
outputCopy
10
15
1999999999
113
1000000001
1
题解:从1开始不能被n整除的第k个数 就是排除n 2n 3n 数k个数 看代码就能懂

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
long long n,k;
int main()
{
    int K;cin>>K;
    while(K--)
    {
        cin>>n>>k;
        long long vis=(k/(n-1))*n;
        if(k%(n-1)==0)vis--;else vis+=k%(n-1);
        cout<<vis<<endl;
    }
    return 0;
}

D. Alice, Bob and Candies
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n candies in a row, they are numbered from left to right from 1 to n. The size of the i-th candy is ai.

Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.

The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).

Alice makes the first move. During the first move, she will eat 1 candy (its size is a1). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.

On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.

For example, if n=11 and a=[3,1,4,1,5,9,2,6,5,3,5], then:

move 1: Alice eats one candy of size 3 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5].
move 2: Alice ate 3 on the previous move, which means Bob must eat 4 or more. Bob eats one candy of size 5 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3].
move 3: Bob ate 5 on the previous move, which means Alice must eat 6 or more. Alice eats three candies with the total size of 1+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3].
move 4: Alice ate 6 on the previous move, which means Bob must eat 7 or more. Bob eats two candies with the total size of 3+5=8 and the sequence of candies becomes [5,9,2,6].
move 5: Bob ate 8 on the previous move, which means Alice must eat 9 or more. Alice eats two candies with the total size of 5+9=14 and the sequence of candies becomes [2,6].
move 6 (the last): Alice ate 14 on the previous move, which means Bob must eat 15 or more. It is impossible, so Bob eats the two remaining candies and the game ends.
Print the number of moves in the game and two numbers:

a — the total size of all sweets eaten by Alice during the game;
b — the total size of all sweets eaten by Bob during the game.
Input
The first line contains an integer t (1≤t≤5000) — the number of test cases in the input. The following are descriptions of the t test cases.

Each test case consists of two lines. The first line contains an integer n (1≤n≤1000) — the number of candies. The second line contains a sequence of integers a1,a2,…,an (1≤ai≤1000) — the sizes of candies in the order they are arranged from left to right.

It is guaranteed that the sum of the values of n for all sets of input data in a test does not exceed 2⋅105.

Output
For each set of input data print three integers — the number of moves in the game and the required values a and b.

Example
inputCopy
7
11
3 1 4 1 5 9 2 6 5 3 5
1
1000
3
1 1 1
13
1 2 3 4 5 6 7 8 9 10 11 12 13
2
2 1
6
1 1 1 1 1 1
7
1 1 1 1 1 1 1
outputCopy
6 23 21
1 1000 0
2 1 2
6 45 46
2 2 1
3 4 2
4 4 3
题解:两端 a从1向右开始吃 b从n向左开始吃 每次都要比对方上次吃的多 而且是要一整块的吃 如果最后剩下的还有 轮到谁谁就吃了剩下的 a先吃 问最多吃多少次 各吃了多少
两个指针分别指向a吃到哪 b吃到哪 开始时l=1 r=n 每吃一次记录吃的总数 然后两个人轮流吃 r

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
int n,k;
int a[1010];
int main()
{
    int K;cin>>K;
    while(K--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        long long x=0,y=0,now=0,ans=0;
        int l=1,r=n;k=1;
        while(l<=r)
        {
            ans++;long long s=0;
            if(k&1)
            {
                k=0;
                for(int i=l;i<=n;i++)
                {
                    s+=a[i];
                    if(s>now||a[i]==0||r==i){l=i+1;now=s;x+=s;break;}
                    a[i]=0;
                }
            }
            else
            {
                k=1;
                for(int i=r;i>=1;i--)
                {
                    s+=a[i];
                    if(s>now||a[i]==0||l==i){r=i-1;now=s;y+=s;break;}
                    a[i]=0;
                }
            }
        }
        cout<<ans<<" "<<x<<" "<<y<<endl;
    }
    return 0;
}

E. Special Elements
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard output
Pay attention to the non-standard memory limit in this problem.

In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.

The array a=[a1,a2,…,an] (1≤ai≤n) is given. Its element ai is called special if there exists a pair of indices l and r (1≤l

Print the number of special elements of the given array a.

For example, if n=9 and a=[3,1,4,1,5,9,2,6,5], then the answer is 5:

a3=4 is a special element, since a3=4=a1+a2=3+1;
a5=5 is a special element, since a5=5=a2+a3=1+4;
a6=9 is a special element, since a6=9=a1+a2+a3+a4=3+1+4+1;
a8=6 is a special element, since a8=6=a2+a3+a4=1+4+1;
a9=5 is a special element, since a9=5=a2+a3=1+4.
Please note that some of the elements of the array a may be equal — if several elements are equal and special, then all of them should be counted in the answer.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Then t test cases follow.

Each test case is given in two lines. The first line contains an integer n (1≤n≤8000) — the length of the array a. The second line contains integers a1,a2,…,an (1≤ai≤n).

It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 8000.

Output
Print t numbers — the number of special elements for each of the given arrays.

Example
inputCopy
5
9
3 1 4 1 5 9 2 6 5
3
1 1 2
5
1 1 1 1 1
8
8 7 6 5 4 3 2 1
1
1
outputCopy
5
1
0
4
0
题解 :先求出给出数组的前缀和 暴力求出特殊元素进行删除

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
int n,k;
int a[8010],b[8010];
int main()
{
    int K;cin>>K;
    while(K--)
    {
        cin>>n;multiset<int>q;int ans=0;
        for(int i=1;i<=n;i++){cin>>a[i];q.insert(a[i]);b[i]=a[i]+b[i-1];}
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++){ while(q.find(b[j]-b[i-1])!=q.end()){q.erase(q.find(b[j]-b[i-1]));ans++;}}
        }
        cout<<ans<<endl;
    }
    return 0;
}

F. Binary String Reconstruction
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For some binary string s (i.e. each character si is either ‘0’ or ‘1’), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 2 were written. For each pair (substring of length 2), the number of ‘1’ (ones) in it was calculated.

You are given three numbers:

n0 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 0;
n1 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 1;
n2 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 2.
For example, for the string s=“1110011110”, the following substrings would be written: “11”, “11”, “10”, “00”, “01”, “11”, “11”, “11”, “10”. Thus, n0=1, n1=3, n2=5.

Your task is to restore any suitable binary string s from the given values n0,n1,n2. It is guaranteed that at least one of the numbers n0,n1,n2 is greater than 0. Also, it is guaranteed that a solution exists.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Then test cases follow.

Each test case consists of one line which contains three integers n0,n1,n2 (0≤n0,n1,n2≤100; n0+n1+n2>0). It is guaranteed that the answer for given n0,n1,n2 exists.

Output
Print t lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.

Example
inputCopy
7
1 3 5
1 1 1
3 9 3
0 1 0
3 1 2
0 0 3
2 0 0
outputCopy
1110011110
0011
0110001100101011
10
0000111
1111
000
题解:因为题中说明给定值一定可以构成字符串
那么 设三个数分别为a b c
那么字符串长度为a+b+c+1
这时该字符串可以这样去构成 c+1个‘1’后面a+b暂时全为‘0’
这时只要注意 b的值就行了 如果b为奇数 那么最后一个字符一定是0 然后依次每隔一个0往前构造1直到b满足就行 这是a也一定满足
如果b为偶数 那么最后一个字符一定是1 同理每隔一个0往前构造1直到b满足
注意排除 b,c为零的情况 最后输出字符串即可

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
int n,k;
int a,b,c;
int main()
{
    int K;cin>>K;
    while(K--)
    {
        cin>>a>>b>>c;
        int len=a+b+c+1;
        string s="o";
        if(c>0||b>0)
        for(int i=1;i<=c+1;i++)s+="1";
        else s="00";
        for(int i=c+2;i<=len;i++)s+="0";
        if(b%2==0&&b>0)
        {
            s[len]='1';
            b-=2;
            for(int i=len-2;i>=c+2;i-=2)
            {
                if(b==0)break;
                s[i]='1';b-=2;
            }
        }
        else if(b&1)
        {
            s[len]='0';b--;
            for(int i=len-1;i>=c+2;i-=2)
            {
                if(b==0)break;
                s[i]='1';b-=2;
            }
        }
        s.erase(0,1);
        cout<<s<<endl;
    }
    return 0;
}

G. Special Permutation
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A permutation of length n is an array p=[p1,p2,…,pn], which contains every integer from 1 to n (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5] is a permutation of length 5.

For a given number n (n≥2), find a permutation p in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 2 and 4, inclusive. Formally, find such permutation p that 2≤|pi−pi+1|≤4 for each i (1≤i

Print any such permutation for the given integer n or determine that it does not exist.

Input
The first line contains an integer t (1≤t≤100) — the number of test cases in the input. Then t test cases follow.

Each test case is described by a single line containing an integer n (2≤n≤1000).

Output
Print t lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.

Example
inputCopy
6
10
2
4
6
7
13
outputCopy
9 6 10 8 4 7 3 1 5 2
-1
3 1 4 2
5 3 6 2 4 1
5 1 3 6 2 4 7
13 9 7 11 8 4 1 3 5 2 6 10 12
题解:给出n问是否可以构成1-n的任意排列使相邻元素之间的差的绝对值为2-4 如果可以输出任何一种 不可以输出-1
可以发现n<4时一定是无解的
n>=4时一定有解
观察如下 n=4时的合法排列
3 1 4 2
则n=5时可以把数放在上述数列最左边
5 3 1 4 2
n=6时可以把数放在上述数列最右边
5 3 1 4 2 6
n=7时可以把数放在上述数列最左边
7 5 3 1 4 2 6
n=8时可以把数放在上述数列最右边
7 5 3 1 4 2 6 8
依次可推出n>=4时的任意n值的合法序列

#include 
using namespace std;
#define pb push_back
#define fast ios_base::sync_with_stdio(0), cin.tie(0)
int n,k;
int a;
int main()
{
    fast;int K;cin>>K;
    while(K--)
    {
        cin>>a;
        if(a<=3)cout<<-1<<endl;
        else {
                for(int i=a;i>4;i--)if(i&1)cout<<i<<" ";
                cout<<"3 1 4 2 ";
                for(int i=6;i<=a;i++)if(i%2==0)cout<<i<<" ";cout<<endl;
        }
    }
    return 0;
}

你可能感兴趣的:(codeforces,题解)