不用百度翻译(实际上还是偷偷用了)的第一天!
上午传送门:https://vjudge.net/contest/380939#rank
cf传送门:https://codeforces.com/contest/1348
Phoenix has n coins with weights 21,22,…,2n. He knows that nis 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 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.
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.
Input | Output |
---|---|
2 2 4 |
2 6 |
题意:共有n个硬币,重量分别为21,22,……,2n,n是偶数。把这n个硬币均分成两堆(每堆有 n 2 \frac{n}{2} 2n个),求这两堆硬币重量之差最小是多少。
这里用了找规律的方法:( f 是两队硬币重量之差)
从图中举出的一些例子,较容易可以看出:f 的值是前一个 f 的值*2+2。
#include
int t,n,f;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
f=0;
for(int i=2;i<=n;i+=2)
{
f=f*2+2;
}
printf("%d\n",f);
}
return 0;
}
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.
Input | Output |
---|---|
4 4 2 1 2 2 1 4 3 1 2 2 1 3 2 1 2 3 4 4 4 3 4 2 |
5 1 2 1 2 1 4 1 2 2 1 -1 7 4 3 2 1 4 3 2 |
题意:当一个数组的所有长度为k的连续子数组中元素的和相等,则称原数组是漂亮的。如果能把原数组变漂亮,就输出变漂亮之后的数组和它的长度;如果不能把它变漂亮,就输出-1。
这题的答案不唯一!所以这里就写了最简单粗暴的方法ψ(`∇´)ψ(和例子给出的不一样)
最简单的一种变漂亮方法:构造题目能有的最大的长度的漂亮数组。能有的最大的长度是n*k。如果出现过的数值种类数量比 k 多,那一定不能把原数组变漂亮;如果出现过的数值种类数量比 k 少,那么需要增加新的数值种类,来让原数组长度为 k 的子数组元素和可以一样;如果出现过的数值种类数量与 k 相等,那么不需要添加新的数值种类,只需要增加调整出现过的数就可以。输出时,按顺序输出标记为1 (要出现)的数就星。
#include
#include
using namespace std;
int t,n,k,a[110];
int bj[110],cnt; //bj[i]的值是1,代表子数组中有数i出现;值是0,代表数i未出现
int b[110],temp;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
memset(a,0,sizeof(a));
memset(bj,0,sizeof(bj));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
bj[a[i]]=1;
}
cnt=0;
for(int i=1;i<=n;i++)
{
if(bj[i]==1)
cnt++; //统计有几个出现过的数值
}
if(cnt>k) //出现过的不同的数值太多了,长度为k的子数组没法使它们元素的和都相等
{
printf("-1\n");
continue;
}
if(cnt<k) //出现过的不同的数值太少了,需要增加别的数值
{
for(int i=1;i<=100;i++)
{
if(bj[i]==0) //数i之前没有出现过
{
bj[i]=1; //现在i出现了
cnt++; //出现过的数值多了一种
if(cnt==k) //正好与题目要求的吻合就不加新的数了
break;
}
}
}
memset(b,0,sizeof(b));
temp=0;
for(int i=1;i<=100;i++)
{
if(bj[i]==1)
b[temp++]=i;
}
printf("%d\n",n*k);
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
printf("%d ",b[j]);
}
printf("\n");
}
return 0;
}
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≤jj=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.
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.
Input | Output |
---|---|
6 4 2 baba 5 2 baacb 5 3 baacb 5 3 aaaaa 6 4 aaxxzz 7 1 phoenix |
ab abbc b aa x ehinopx |
需要注意的是第②种情况,即a[0]==a[k-1],且a[k] ~ a[n-1]中每个元素的值都相等的情况。此时,先输出a[k-1],之后输出(n-1)/k个数。
下面定义的是数组s,和图中的数组a是一样的(只是名字换了一下)。
#include
#include
using namespace std;
int t,n,k;
char s[100010];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
scanf("%s",&s);
sort(s,s+n);
if(s[0]!=s[k-1])
printf("%c",s[k-1]);
else
{
if(s[k]==s[n-1])
{
printf("%c",s[k-1]);
for(int i=0;i<(n-1)/k;i++)
printf("%c",s[k+i]);
}
else
{
for(int i=k-1;i<n;i++)
printf("%c",s[i]);
}
}
printf("\n");
}
return 0;
}
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 m 2 \frac{m}{2} 2m 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.
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.
Input | Output |
---|---|
3 9 11 2 |
3 1 0 2 3 1 1 2 1 0 |
题意:第1天有1个细菌,它的质量是1。每天都有 0个 ~ 全部 细菌分裂。白天细菌群的总质量不论是否有分裂都不变,晚上每个细菌的质量都+1。给定一个n,问最少几天能让细菌群的总质量为n。
我们可以发现:分裂在白天并不影响细菌群的总质量,但在晚上会影响细菌群增加的质量。白天分裂后细菌群的个数变为x,那么晚上细菌群的总质量就会+x。
因此,如果每天都让所有细菌都分裂,那么这天总质量就会增加的最多,这样就会最快接近要达到的n。(每个细菌的质量小了,可是细菌数量多呀,总质量就唰唰往上增了)
如果每天都让所有细菌都分裂,正好在某天总质量到达n就很妙,但是最有可能发生的还是第 x 天白天啪分裂完,晚上唰一加,总质量还小于n,第x+1天再裂开增加一手,总质量就超过n了。因此需要考虑这个第x天或之前到底要让几个细菌裂开,才能在第x+1天让细菌群总质量正好等于n。
因为题目中每天晚上增加的质量是当天裂完后细菌的总个数,因此考虑细菌群总质量增加的多少和细菌群个数变成了多少是共通的。
如果正好发生第x天操作完总质量
以第一个测试用例(n=9)为例:
如果每天都让全部细菌裂开,会是这样的情况:
这样第2天晚上增加完质量后,还是没到要求的n(9),因此第3天还需要增加质量。因为这里是举每天都全部分裂的例子,因此第3天白天继续全部分裂,晚上增加完质量后就会超过n,没法正好到达n。
对于第2天来说,n-sum=9-7=2,这说明分裂后细菌群个数的情况要增加一天。
如果分裂后细菌群个数的情况要增加一天,就是这样的:
这样就能正好等于n了。
n-sum的值的多少,代表着某天细菌分裂完变成了多少个。而为了探讨是哪天变成了多少个,可以对每天细菌分裂完的个数进行由小到大排序(细菌分裂本来也就是从少变多的)。
此外,每天与前一天相比增加了几个细菌,就代表着前一天有几个细菌裂开。
如每天裂完后的细菌情况为:1(初始状态)→2→4→5。
说明每天裂开的细菌个数为: 1→2→1。
因此可以用差分来计算每天裂了几个细菌。
#include
#include
#include
typedef long long ll;
using namespace std;
int t,n,sum,len;
vector<int>S;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum=0;
S.clear();
for(int i=1;i+sum<=n;i*=2)
{
sum+=i; //sum代表第log2(i)天操作完后细菌群的总质量
S.push_back(i); //S存放的是每天裂开后细菌变成了多少个
}
if(sum<n)
S.push_back(n-sum);
len=S.size();
printf("%d\n",len-1);
//len要-1是因为S中有初始的1个细菌(不是因为分裂变成的1),其他都是分裂后的细菌个数
//因此-1是为了去掉这个初始状态,算出究竟花了几天
sort(S.begin(),S.end());
for(int i=1;i<len;i++) //输出每天裂了几个菌
printf("%d ",S[i]-S[i-1]);
printf("\n");
}
return 0;
}