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♪(・ω・)ノ

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章