A. Phoenix and Balance
来源:http://codeforces.com/contest/1348/problem/A
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
2
2
4
outputCopy
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.
题意:
给你一个整数n,将2的1次-n次方作为一个数组,将其分成两部分,使两部分的和的差最小
思路:
很明显是贪心的题目,将最大的一个和前n/2-1个小的放在一起,其他的放在一起就ok了,注意提前定义数组储存2的n次幂
#include<iostream>
using namespace std;
typedef long long ll;
ll s[33];
int x(int n)
{
if (n == 1)
return 2;
else
return 2 * x(n - 1);
}
int main()
{
for (int i = 1; i <= 30; i++)//存储
s[i] += s[i - 1] + x(i);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
if (n == 2)
cout << 2 << endl;
else
{
int t = n / 2 - 1;
cout << (s[t] + x(n) - (s[n - 1] - s[t])) << endl;
}
}
return 0;
}
B. Phoenix and Beauty
来源:http://codeforces.com/contest/1348/problem/B
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
4
4 2
1 2 2 1
4 3
1 2 2 1
3 2
1 2 3
4 4
4 3 4 2
outputCopy
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.
题意:
给出一个n个数的序列a,如何可以插入几个数使其长度为k的子序列和相同。例如,n=4,k=2,a = 1 2 2 1,一个答案是1 2 1 2 1,和都为3
思路:
为了使某个数组的k美观,该数组必须是周期为k的周期。 如果数组a中存在超过k个不同的数字,则没有答案,打印-1(因为数组不能以周期k为周期)
具体实现见代码:
#include <cstdio>
#include <algorithm>
using namespace std;
int a[105], vis[105] = { 0 }, c[105], cn;
int main()
{
int t, i, b, n, k, cnt, j;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
vis[a[i]] = 1;
}//vis[i]=1表示,数据1出现过
cnt = 0;
for (i = 1; i <= 100; i++)
if (vis[i])
cnt++;//记录数组中的值不雷同的种类数量
if (cnt > k)
{
printf("-1\n");
continue;
}//数组中的值不雷同的种类数量,过多
if (cnt < k)//数组中的值不雷同的种类数量,过少
for (i = 1; i <= 100; i++)
if (!vis[i])
{
vis[i] = 1;
cnt++;//添加新值到周期中
if (cnt == k)
break;
}
cn = 0;
for (i = 1; i <= 100; i++)
if (vis[i]==1)
c[++cn] = i;//记录一个周期内,数组的值
printf("%d\n", n * k);
for (i = 1; i <= n; i++)//打印数据
for (j = 1; j <= k; j++)
printf("%d ", c[j]);
printf("\n");
}
return 0;
}
C. Phoenix and Distribution
来源:http://codeforces.com/contest/1348/problem/C
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
6
4 2
baba
5 2
baacb
5 3
baacb
5 3
aaaaa
6 4
aaxxzz
7 1
phoenix
outputCopy
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.
题意:
给你n个字符,要求你将其分为k份,每份个数至少有1个,使得新组合的字符串中字典序最大的字符串的字典序尽可能小,打印这个字符串。
思路:
首先把s排序,
如果t=1,直接返回排好的s
如果s第t位和第1位不同,答案就是s[t],如t=4,可以分成a…,a…,a…,b把剩下的东西分给a…就好了
如果相同,有两种情况,剩下的字母全一样,那么平分,如aabbbb分成abb,abb
只要有一个不一样,如aabbbbc,分成a,abbbbc,这个b或者c分给前面的a都不合适,因为会把c位置提前字典序变大
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
char s[N];
int main() {
int T;
cin >> T;
while (T--)
{
int n, t;
cin >> n >> t;
cin >> s + 1;
sort(s + 1, s + n + 1);
if (t == 1)
{
cout << s + 1 << "\n";
continue;
}
if (s[t] != s[1]) cout << s[t] << "\n";
else
{
if (s[t + 1] == s[n])
{
cout << s[t];
int cnt = (n - t) / t;
if ((n - t) % t) cnt++;
while (cnt--) cout << s[t + 1];
puts("");
}
else
cout << s + t << "\n";
}
}
return 0;
}
如有错误,欢迎各路da lao指正