CCPC 2018 吉林 C "JUSTICE" (数学)

传送门

 

参考资料:

  [1]:https://blog.csdn.net/mmk27_word/article/details/89789770

 

CCPC 2018 吉林 C "JUSTICE" (数学)
题目描述
    Put simply, the Justice card represents justice, fairness, truth and the law.You are being called to account for your actions and will be judged accordingly. If you have acted in a way that is in alignment with your Higher Self and for the greater good of others, you have nothing to worry about. However, if you have acted in a way that is out of alignment, you will be called out and required to own up to your actions. If this has you shaking in your boots, know that the Justice card isn't as black and white as you may think.
(以上是故事情节么...........)
    On the table there are n weights. 
    On the body of the i-th weight carved a positive integer ki, indicating that its weight is  1/(2^ki) gram. 
    Is it possible to divide then weights into two groups and make sure that the sum of the weights in each group is greater or equal to 1/2 gram? 
    That's on your call. And please tell us how if possible.

输入
    In the first line of the input there is a positive integer T (1≤T≤2000), indicating there are T testcases.
    In the first line of each of the T testcases, there is a positive integer n (1≤n≤1e5 , Σ n ≤ 7×1e5 ),indicating there areηweights on the table.
    In the next line, there are n integers ki (1≤ki≤1e9), indicating the number carved on each weight.


输出
    For each testcase, first print Case i : ANSWER in one line, i indicating the case number starting from 1 and ANSWER should be either YES or NO, indicating whether or not it is possible to divide the weights. Pay attention to the space between : and ANSWER.
    If it's possible, you should continue to output the dividing solution by print a 0 / 1 string of length n in the next line. 
    The i-th character in the string indicating whether you choose to put the i-th weight in group 0 or group 1.
题目描述 CCPC 2018 吉林 C "JUSTICE" (数学)
样例输入
3
3
2 2 2
3
2 2 1
2
1 1

样例输出
Case 1: NO
Case 2: YES
001
Case 3: YES
10
样例输入输出

 

题解看上述参考资料,下面谈谈我的进一步理解,以及,比赛时思路出现的错误:

  组成一个 1 / 2 需要

    21个 2, 22个23 , ....... , 2x-1个2x;

  那么,从幂最小的k向上递推,判断是否存在 x,y 使得 x 出现的次数 ≥ 2x-1 , y 出现的次数 ≥ 2y-1 次;

AC代码:

 

比赛时,看到 n 最大为 1e5 ,而1 / 2k 组成 1 / 2 至少需要 2k-1 个 k ,然后,找到了2k ≤ 1e5 的最大的 k = 17;

然后,就特判,当 ki > 20 时,就不管;

对于 ki ≤ 20 的情况,定义一个数组a,a[i] : k = i 的 k 的总个数;

每次都更新一遍数组 a;

1 for(int i=20;i >= 2;--i)
2 {
3   a[i-1] += a[i]/2;//两个i构成一个i-1
4   a[i] %= 2;
5 }

最后判断一下a[1]的个数,如果 < 2,输出"NO";

反之,输出"YES",并输出分组;

 

然后,今天下午debug了一下午,学弟给了我一组数据,顿悟了,debug成功;

当 k > 17 时,也可以组成 1 / 2;

CCPC 2018 吉林 C "JUSTICE" (数学)

x最大可取1e5,所以说,k最大也可到达1e5;

 

上一篇:Java基础_21. 赋值运算符


下一篇:Java基础知识