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

传送门

 

参考资料:

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

 

题目描述
    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.
题目描述
样例输入
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代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 #define mem(a,b) memset(a,b,sizeof(a))
 7 const int maxn=1e5+50;
 8  
 9 int n;
10 struct Date
11 {
12     int k;
13     int id;
14     bool operator < (const Date &obj) const
15     {
16         return k < obj.k;
17     }
18 }_date[maxn];
19 int vis[maxn];
20  
21 void Solve()
22 {
23     mem(vis,0);
24     sort(_date+1,_date+n+1);
25  
26     int k=1;
27     int cnt1=1;//2^1只需要一个
28     int cnt2=1;//2^1只需要一个
29     for(int i=1;i <= n;++i)
30     {
31         //假设 d=_date[i].k-k
32         //那么,从需要 cnt*2^d 个_date[i].k
33         //类乘 d 次就会得到所需的cnt
34         //此处是不可能超时的
35         //因为n-i+1 最大才1e5,而2^17 > 1e5,所以,单次最多累加17次
36         //所以说 cnt1+cnt2 <= n-i+1 很关键
37         //以及后面的if(cnt1==0 && cnt2==2) break;也很关键
38         //如果不break,那么这个while中的cnt1+cnt2=0,k最多会加到1e9,指定TLE
39         while(cnt1+cnt2 <= n-i+1 && k < _date[i].k)
40         {
41             cnt1 *= 2;
42             cnt2 *= 2;
43             k++;
44         }
45         if(cnt1+cnt2 > n-i+1)//这之后没有cnt1+cnt2个数,那,肯定组不成2个1/2
46         {
47             puts("NO");
48             return ;
49         }
50         if(cnt1)
51         {
52             cnt1--;
53             vis[_date[i].id]=1;
54         }
55         else
56             cnt2--;
57  
58         if(!cnt1 && !cnt2)//关键,去掉会TLE
59             break;
60     }
61     if(cnt1 || cnt2)
62         puts("NO");
63     else
64     {
65         puts("YES");
66         for(int i=1;i <= n;++i)
67             printf("%d",vis[i]);
68         printf("\n");
69     }
70 }
71 int main()
72 {
73     int test;
74     scanf("%d",&test);
75     for(int kase=1;kase <= test;++kase)
76     {
77         scanf("%d",&n);
78         for(int i=1;i <= n;++i)
79         {
80             scanf("%d",&_date[i].k);
81             _date[i].id=i;
82         }
83         printf("Case %d: ",kase);
84         Solve();
85     }
86     return 0;
87 }
View Code

 

反思总结:

比赛时,看到 n 最大为 1e5 ,而1 / 2k 组成 1 / 2 至少需要 2k-1 个 2k ,然后,找到了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;

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

posted @ 2019-05-06 18:54  HHHyacinth  阅读(713)  评论(0编辑  收藏  举报