Codeforces Round #638 (Div. 2)A-D

A

A. Phoenix and Balance
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Phoenix has n
coins with weights 21,22,…,2n. He knows that n

is even.

He wants to split the coins into two piles such that each pile has exactly n2
coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a−b|, the absolute value of a−b

.
Input

The input consists of multiple test cases. The first line contains an integer t
(1≤t≤100

) — the number of test cases.

The first line of each test case contains an integer n
(2≤n≤30; n

is even) — the number of coins that Phoenix has.
Output

For each test case, output one integer — the minimum possible difference of weights between the two piles.
Example
Input
Copy

2
2
4

Output
Copy

2
6

Note

In the first test case, Phoenix has two coins with weights 2
and 4. No matter how he divides the coins, the difference will be 4−2=2

.

In the second test case, Phoenix has four coins of weight 2
, 4, 8, and 16. It is optimal for Phoenix to place coins with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.

思路

如果要分成两堆且差最小,我可以先把最大的放在一边,这样就是另一边要尽量大,接近这个最大数,又因为硬币都是2的n次,所以接近最大数就是最大数之后的n/2个硬币,然后再把剩下的放到最大的那边。
在这里插入图片描述

代码

#include <bits/stdc++.h>

using namespace std;
#define ll long long
int sum;
int main()
{
    
    
    ios::sync_with_stdio(0);
    int t,n;
    cin>>t;
    for(int k=0;k<t;k++)
    {
    
    
        cin>>n;
        int a=1;
        sum=0;
        for(int i=1;i<n/2;i++)
            {
    
    a*=2;sum+=a;}
        for(int i=n/2;i<n;i++)
            {
    
    a*=2;sum-=a;}
        sum+=a*2;
        cout<<sum<<endl;
    }


B

B. Phoenix and Beauty
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length k

have the same sum. A subarray of an array is any sequence of consecutive elements.

Phoenix currently has an array a
of length n. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between 1 and n

inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.
Input

The input consists of multiple test cases. The first line contains an integer t
(1≤t≤50

) — the number of test cases.

The first line of each test case contains two integers n
and k (1≤k≤n≤100

).

The second line of each test case contains n
space-separated integers (1≤ai≤n

) — the array that Phoenix currently has. This array may or may not be already beautiful.
Output

For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.

The first line should contain the length of the beautiful array m
(n≤m≤104). You don’t need to minimize m

.

The second line should contain m
space-separated integers (1≤bi≤n) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array a. You may print integers that weren’t originally in array a

.

If there are multiple solutions, print any. It’s guaranteed that if we can make array a
beautiful, we can always make it with resulting length no more than 104

.
Example
Input
Copy

4
4 2
1 2 2 1
4 3
1 2 2 1
3 2
1 2 3
4 4
4 3 4 2

Output
Copy

5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2

Note

In the first test case, we can make array a
beautiful by inserting the integer 1 at index 3 (in between the two existing 2s). Now, all subarrays of length k=2 have the same sum 3

. There exists many other possible solutions, for example:

2,1,2,1,2,1

1,2,1,2,1,2

In the second test case, the array is already beautiful: all subarrays of length k=3
have the same sum 5

.

In the third test case, it can be shown that we cannot insert numbers to make array a

beautiful.

In the fourth test case, the array b
shown is beautiful and all subarrays of length k=4 have the same sum 10. There exist other solutions also.

思路

1.和相同:
在这里插入图片描述
如果k=4,如上图,和一样就要保证a==e

2.不存在的情况:如果不同数的个数大于k,那么肯定会出现不满足上述情况
3.先排序,然后去重,我们就可以知道是不是不存在的,然后前k个数一定是包含了所有不同数,题说不要求最小数列,所以我们可以把每一个数都扩充成前k个数的数列,这样总数列长就是n*k

代码

#include <bits/stdc++.h>

using namespace std;
#define ll long long
int c[10010];
int main()
{
    
    
    ios::sync_with_stdio(0);
    int t,n,k;
    cin>>t;
    while(t--)
    {
    
    
        cin>>n>>k;
        for(int i=0;i<n;i++)
        {
    
    
            cin>>c[i];
        }
        sort(c,c+n);
        int sum=unique(c,c+n)-c;
        if(sum>k)
            cout <<-1<<endl;
        else
        {
    
    
            cout <<n*k<<endl;
            for(int i=0;i<n;i++)
            {
    
    
                for(int j=0;j<k;j++)
                    cout <<c[j]<<" ";
            }
            cout <<endl;
        }
    }

    return 0;
}

C

C. Phoenix and Distribution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Phoenix has a string s
consisting of lowercase Latin letters. He wants to distribute all the letters of his string into k non-empty strings a1,a2,…,ak such that every letter of s goes to exactly one of the strings ai. The strings ai do not need to be substrings of s. Phoenix can distribute letters of s and rearrange the letters within each string ai

however he wants.

For example, if s=
baba and k=2

, Phoenix may distribute the letters of his string in many ways, such as:

ba and ba
a and abb
ab and ab
aa and bb 

But these ways are invalid:

baa and ba
b and ba
baba and empty string (ai

should be non-empty) 

Phoenix wants to distribute the letters of his string s
into k strings a1,a2,…,ak to minimize the lexicographically maximum string among them, i. e. minimize max(a1,a2,…,ak). Help him find the optimal distribution and print the minimal possible value of max(a1,a2,…,ak)

.

String x
is lexicographically less than string y if either x is a prefix of y and x≠y, or there exists an index i (1≤i≤min(|x|,|y|)) such that xi < yi and for every j (1≤j<i) xj=yj. Here |x| denotes the length of the string x

.
Input

The input consists of multiple test cases. The first line contains an integer t
(1≤t≤1000

) — the number of test cases. Each test case consists of two lines.

The first line of each test case consists of two integers n
and k (1≤k≤n≤105) — the length of string s and the number of non-empty strings, into which Phoenix wants to distribute letters of s

, respectively.

The second line of each test case contains a string s
of length n

consisting only of lowercase Latin letters.

It is guaranteed that the sum of n
over all test cases is ≤105

.
Output

Print t
answers — one per test case. The i-th answer should be the minimal possible value of max(a1,a2,…,ak) in the i

-th test case.
Example
Input
Copy

6
4 2
baba
5 2
baacb
5 3
baacb
5 3
aaaaa
6 4
aaxxzz
7 1
phoenix

Output
Copy

ab
abbc
b
aa
x
ehinopx

Note

In the first test case, one optimal solution is to distribute baba into ab and ab.

In the second test case, one optimal solution is to distribute baacb into abbc and a.

In the third test case, one optimal solution is to distribute baacb into ac, ab, and b.

In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a.

In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x.

In the sixth test case, one optimal solution is to distribute phoenix into ehinopx.

思路

1.这个题要求找到字符串最小的那个组,然后输出组里面最大的
2.首先了解字符串大小的比较,从第一个开始比,直到有一个不同,所以abbc<abc,同时短的更小,也就是a<aa
3.通过分析例子,我们可以得到一个结论:第k组的第一个字母就是整个字符串排序后的第k个字母,因为这样保证了第一个字母最小,也就是所有组里最小的
4.然后出现两种情况,第一个字母全部相同和第一个字母有不同。
5.第一个字母不同,那么最大的就是组里的第k个(首字母就已经是最大了),根据第三个和第五个例子,我们可以得到这种情况下,剩余字母全分配到别的组,直接输出第k个字母
6.第一个字母不同又能分成剩余字母相同和剩余字母不同,剩余字母相同的话那就是平均分配(要输出大的,也就是字母多的),剩余字母不同,参考第二个例子,abbc<abc,所以剩余字母全部放到一个组里,输出这个组

代码

#include <bits/stdc++.h>

using namespace std;
#define ll long long
string s;
int n,k;
bool same()
{
    
    
    for(int i=1;i<k;i++)
    {
    
    
        if(s[i]!=s[i-1])
            return false;
    }
    return true;
}
bool same2()
{
    
    
    for(int i=k+1;i<n;i++)
    {
    
    
        if(s[i]!=s[i-1])
            return false;
    }
    return true;
}
int main()
{
    
    
    ios::sync_with_stdio(0);
    int t;
    cin>>t;

    while(t--)
    {
    
    
        cin>>n>>k;
        cin>>s;
        string s2;
        sort(s.begin(),s.end());
        s2+=s[k-1];
        if(k==1)
            cout <<s<<endl;
        else
        {
    
    
            if(same())
            {
    
    
                if(same2())
                {
    
    
                    cout <<s2;
                    int sum=(n-k)/k;
                    if((n-k)%k>0)
                        sum++;
                    for(int i=0;i<sum;i++)
                        cout <<s[k];
                    cout <<endl;
                }
                else
                {
    
    
                    for(int i=k-1;i<n;i++)
                        cout <<s[i];
                    cout <<endl;
                }

            }
            else
                {
    
    
                    cout <<s2<<endl;
                }
        }
    }
    return 0;
}

D

D. Phoenix and Science
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.

Initially, on day 1
, there is one bacterium with mass 1

.

Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m
splits, it becomes two bacteria of mass m2 each. For example, a bacterium of mass 3 can split into two bacteria of mass 1.5

.

Also, every night, the mass of every bacteria will increase by one.

Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly n

. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!
Input

The input consists of multiple test cases. The first line contains an integer t
(1≤t≤1000

) — the number of test cases.

The first line of each test case contains an integer n
(2≤n≤109

) — the sum of bacteria masses that Phoenix is interested in.
Output

For each test case, if there is no way for the bacteria to exactly achieve total mass n

, print -1. Otherwise, print two lines.

The first line should contain an integer d

— the minimum number of nights needed.

The next line should contain d
integers, with the i-th integer representing the number of bacteria that should split on the i

-th day.

If there are multiple solutions, print any.
Example
Input
Copy

3
9
11
2

Output
Copy

3
1 0 2
3
1 1 2
1
0

Note

In the first test case, the following process results in bacteria with total mass 9

:

Day 1

: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5
each.
Night 1
: All bacteria’s mass increases by one. There are now two bacteria with mass 1.5
.
Day 2
: None split.
Night 2
: There are now two bacteria with mass 2.5
.
Day 3
: Both bacteria split. There are now four bacteria with mass 1.25
.
Night 3
: There are now four bacteria with mass 2.25

. 

The total mass is 2.25+2.25+2.25+2.25=9. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.

In the second test case, the following process results in bacteria with total mass 11

:

Day 1

: The bacterium with mass 1 splits. There are now two bacteria with mass 0.5
.
Night 1
: There are now two bacteria with mass 1.5
.
Day 2
: One bacterium splits. There are now three bacteria with masses 0.75, 0.75, and 1.5
.
Night 2
: There are now three bacteria with masses 1.75, 1.75, and 2.5
.
Day 3
: The bacteria with mass 1.75 and the bacteria with mass 2.5 split. There are now five bacteria with masses 0.875, 0.875, 1.25, 1.25, and 1.75
.
Night 3
: There are now five bacteria with masses 1.875, 1.875, 2.25, 2.25, and 2.75

. 

The total mass is 1.875+1.875+2.25+2.25+2.75=11. It can be proved that 3 is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.

In the third test case, the bacterium does not split on day 1
, and then grows to mass 2 during night 1.

思路

1.这个题看上去花里胡哨,实际上都是整数运算
2.要找的就是一个符合条件的数列,让数列和等于n-1,然后输出数列差
3.可以看出如果每天都是全部分裂,每天多出的那就是a=2,4,8,16…,每天最多和就是b=2,6,14,30…,根据这个就可以得到最小天数L,是小等于于最多和的那天
4.累加数列a,再加上最后差就可以了
5.以9为例就是需要多8个,小于14所以是3天,然后需要2 4以及8-6=2(6是前面的和),排序得到1 2 2 4,差是1 0 2(一开始1个,所以第一天增加是2-1=1个)
6.以20为例就是需要多19个,小于30所以是4天,然后需要 2 4 8以及19-14=5,排序得到1 2 4 5 8,差是 1 2 1 3

代码

#include <bits/stdc++.h>

using namespace std;
#define ll long long
ll sum[33];
ll a[50];
int main()
{
    
    
    ios::sync_with_stdio(0);
    int t;
    ll n;
    cin>>t;
    sum[0]=2;
    for(int i=1;i<32;i++)
    {
    
    
        sum[i]=sum[i-1]*2;
    }

    while(t--)
    {
    
    
        vector<int> v;
        ll sum2=0;
        cin>>n;
        n--;
        int l;
        for(l=1;;l++)
        {
    
    
            if(sum[l]-2>=n)
                break;
            else
            {
    
    
                v.push_back(sum[l-1]);
                sum2+=sum[l-1];
            }

        }
        if(sum2!=n)
            v.push_back(n-sum2);
        cout <<l<<endl;
        v.push_back(1);
        sort(v.begin(),v.end());
        for(int i=1;i<v.size();i++)
            cout <<v[i]-v[i-1]<<" ";
        cout <<endl;
    }

    return 0;
}

有表述不清楚的地方可以在评论区指出(原谅我的语文) Thanks♪(・ω・)ノ

猜你喜欢

转载自blog.csdn.net/qq_44616044/article/details/105891200