[Codeforces Round #638 (div2)]1348


1348A - Phoenix and Balance[思维]
1348B - Phoenix and Beauty[思维]
1348C - Phoenix and Distribution[排序][分类]
1348D - Phoenix and Sciences[思维][好题]
1348E - Phoenix and Berries[ d p dp dp]
1348F - Phoenix and Memory[ ]





目录

1348A - Phoenix and Balance[思维]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

Phoenix has n coins with weights 2 1 , 2 2 , … , 2 n 2^1,2^2,…,2^n 21,22,,2n. He knows that n is even.

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

Input

The input consists of multiple test cases. The first line contains an integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases.

The first line of each test case contains an integer n ( 2 ≤ n ≤ 30 ; n n (2≤n≤30; n n(2n30;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

2
2
4

Example output

2
6

Hit

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

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

分析:

题意:
2 1 → 2 n 2^1 \rightarrow 2^n 212n 这些数分成数量相同的两堆数
要使得两堆数的和差值尽可能小

做法:
显然在 2 n 2^n 2n 所在的堆要放前 n − 1 n-1 n1 个最小的数

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e3 + 5;
const int maxm = 3e6 + 5;

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        ll t = pow(2, n + 1) - 2;
        ll t2 = pow(2, n / 2) - 2 + pow(2, n);
        printf("%lld\n", t2 - (t - t2));
    }
    return 0;
}






目录

1348B - Phoenix and Beauty[思维]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length k k 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 1 1 and n n 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 ) t (1≤t≤50) t(1t50) — the number of test cases.

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

The second line of each test case contains n n n space-separated integers ( 1 ≤ a i ≤ n ) (1≤a_i≤n) (1ain) — 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 -1 1. Otherwise, print two lines.

The first line should contain the length of the beautiful array m ( n ≤ m ≤ 1 0 4 ) m (n≤m≤10^4) m(nm104). You don’t need to minimize m.

The second line should contain m m m space-separated integers ( 1 ≤ b i ≤ n ) (1≤b_i≤n) (1bin) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array a a a. You may print integers that weren’t originally in array a a 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 1 0 4 10^4 104.

Example input

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

Example output

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

Hit

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

2 , 1 , 2 , 1 , 2 , 1 1 , 2 , 1 , 2 , 1 , 2 2,1,2,1,2,1\\1,2,1,2,1,2 2,1,2,1,2,11,2,1,2,1,2
In the second test case, the array is already beautiful: all subarrays of length k = 3 k=3 k=3 have the same sum 5 5 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 b b shown is beautiful and all subarrays of length k = 4 k=4 k=4 have the same sum 10 10 10. There exist other solutions also.

分析:
题意:
给定长度为 n n n 的数字数组,可以在该数组的任意位置添加任意值
要使得 ∀ i ∈ [ 1 , n − k ] \forall i \in [1, n-k] i[1,nk] 都满足 b [ i ] = b [ i + k ] b[i] = b[i + k] b[i]=b[i+k]

做法:
简单说就是 ∀ i ∈ [ 0 , n ) , b [ i ] = b [ i % k ] \forall i \in [0,n), b[i] = b[i\%k] i[0,n),b[i]=b[i%k](这里为了方便用取余做下标从 0 0 0 开始)
显然这相当于每 k k k 个为一个周期
那么一个周期也就最多只能放 k k k 个数,
即原数组最多只能有 k k k 个不相同的数,才有可能存在这样对应的数组
为了实现是由原有 n n n 个数的数组 a a a 添加得到的
则可以重复 n n n 个周期,每个周期 k k k 个元组
这样就一定能保证每个周期都可以取出对应原数组顺序的数

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int maxm = 3e6 + 5;

bool vis[maxn];
vector<int> v;

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n, k, x; v.clear();
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; ++i) vis[i] = false;
        for(int i = 0; i < n; ++i) {
            scanf("%d", &x);
            if(!vis[x])     v.push_back(x);
            vis[x] = true;
        }
        if(v.size() > k) {
            puts("-1");
        }else{
            int m = k * n;
            for(int i = (int)v.size(); i <= k; ++i)
                v.push_back(1);
            printf("%d\n", m);
            for(int i = 0; i < m; ++i)
                printf("%d%c", v[i % k], i == m - 1 ? '\n' : ' ');
        }
    }

    return 0;
}





目录

1348C - Phoenix and Distribution[排序][分类]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

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

For example, if s = b a b a s= baba s=baba and k = 2 k=2 k=2, Phoenix may distribute the letters of his string in many ways, such as:

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

But these ways are invalid:

b a a baa baa and b a ba ba
b b b and b a ba ba baba and empty string ( a i a_i ai should be non-empty)
Phoenix wants to distribute the letters of his string s s s into k k k strings a 1 , a 2 , … , a k a_1,a_2,…,a_k a1,a2,,ak to minimize the lexicographically maximum string among them, i . e . i. e. i.e. minimize m a x ( a 1 , a 2 , … , a k ) max(a_1,a_2,…,a_k) max(a1,a2,,ak). Help him find the optimal distribution and print the minimal possible value of m a x ( a 1 , a 2 , … , a k ) max(a_1,a_2,…,a_k) max(a1,a2,,ak).

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

Input

The input consists of multiple test cases. The first line contains an integer t ( 1 ≤ t ≤ 1000 ) t (1≤t≤1000) t(1t1000) — the number of test cases. Each test case consists of two lines.

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

The second line of each test case contains a string s s s of length n n n consisting only of lowercase Latin letters.

It is guaranteed that the sum of n over all test cases is ≤ 1 0 5 ≤10^5 105.

Output

Print t t t answers — one per test case. The i i i-th answer should be the minimal possible value of m a x ( a 1 , a 2 , … , a k ) max(a_1,a_2,…,a_k) max(a1,a2,,ak) in the i i i-th test case.

Example input

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

Example output

ab
abbc
b
aa
x
ehinopx

Hit

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

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

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

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

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

In the sixth test case, one optimal solution is to distribute phoenix into e h i n o p x ehinopx ehinopx.

分析:
题意:
原串 s s s 长度为 n n n
现要将原串分为 k k k 个串
使得这 k k k 个串中字典序最大的尽可能小
输出这个字典序最大的串

做法:
观察样例可以发现分别有不一样的情况,由此作出分类
首先对 s s s 串进行排序
这里我假设当前串中字典序最小的字符为 A A A,
第二小的字符为 B B B(这里说的第几小是去掉重复的字符的)
C C C 是第 k k k 小的字符(这里说的第几小是没有去掉重复的字符),
最终求的串为 t t t
c n t [ i ] cnt[i] cnt[i] 分别表示他们的个数

  1. c n t [ A ] < k cnt[A] < k cnt[A]<k, t = C t = C t=C
    t t t 串必然为第 k k k 小的开头,且仅有这个字符的串
  2. c n t [ A ] = = n cnt[A] == n cnt[A]==n, t = A ∗ ⌈ n k ⌉ t = A * \lceil \frac{n}{k} \rceil t=Akn
    这里的乘意思是有几个 A A A
    如果字符是一样的,显然最大的串要么正好平分成 k k k 个串
    要么多一个
  3. c n t [ A ] + c n t [ B ] = = n cnt[A] + cnt[B] == n cnt[A]+cnt[B]==n, t = A + B ∗ ⌈ c n t [ B ] k ⌉ t = A + B * \lceil \frac{cnt[B]}{k} \rceil t=A+Bkcnt[B]
    这里的 + + + 是接在 A A A 后面的意思
    这一部分跟 2. 2. 2. 很相似
  4. 否则,就要在 A A A 后面接上 第 k + 1 k + 1 k+1 (这里是不去重的情况)及其之后的所有字符

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int maxm = 3e6 + 5;

char s[maxn];
int cnt[150];

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n, k, kind = 0, tmp, pos;
        scanf("%d%d%s", &n, &k, s + 1);
        sort(s + 1, s + 1 + n);
        for(int i = 0; i < 150; ++i) cnt[i] = 0;
        for(int i = 1; i <= n; ++i)
            cnt[s[i]] += 1;
        
        int m = 0;
        if(k > cnt[s[1]]) {
            s[1] = s[k], m = 1;
        }else if(cnt[s[1]] == n) {
            m = n / k + (n % k == 0 ? 0 : 1);
        }else if(cnt[s[1]] == k && cnt[s[cnt[s[1]] + 1]] == n - k) {
            pos = s[cnt[s[1]] + 1];
            m = cnt[pos] / k + (cnt[pos] % k == 0 ? 0 : 1) + 1;
            for(int i = 2; i <= m; ++i)
                s[i] = pos;
        }else{
            m = n - k + 1;
            for(int i = k + 1; i <= n; ++i)
                s[i - k + 1] = s[i];
        }
        s[m + 1] = 0;
        printf("%s\n", s + 1);
    }

    return 0;
}





目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值