题目描述
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 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≤105 , Σn≤7 × 105 ),indicating there areηweights on the table.
In the next line, there are n integers ki (1≤ki≤109), 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

  • 题意
    • n个物品,每个物品重量1/2^k[i],问可不可以分成两份,让这两份的总量都大于1/2
  • 题解
    • 由题可知当存在2个ki=1或 4个ki=2 … 以此类推,该条件即成立,所以我们记录下符合要求的总数量,如果数组剩下的数的总数量小于此时需要的数量时,该条件无解,反之即成立。
  • 代码
    • /*
      要找两份都大于等于1/2的,对于其中一份来说也就是找1个,或者说两个,或者说四个
      
      用一个pre记录当前需要的数的值,然后cnt1,cnt2记录个数,如果cnt1,cnt2都等于0了,那么也就可以了“YES”
      
      相反,如果cnt1+cnt2大于剩下的数了,那么肯定是“NO”了,最后剩下cnt1或者cnt2不为0,那也是“NO”
      
      在扫一遍的过程中记录vis即可
      */
      /*
      由题可知当存在2个ki=1或 4个ki=2 … 以此类推,
      因为剩下的数一定是大于当前数,所以若剩下的数的个数小于当前需要的个数,直接GG
      */
      #include <bits/stdc++.h>
      typedef long long ll;
      using namespace std;
      const int maxn = 1e5+7;
      int T,Case,n;
      struct node{
          int val,id;
          bool operator < (const node &p)const{
              return val<p.val;
          }
      }a[maxn];
      bool vis[maxn];
      int main(){
          for(scanf("%d",&T);T--;){
              scanf("%d",&n);
              for(int i=0;i<n;i++)scanf("%d",&a[i].val),a[i].id = i,vis[i] = 0;
              printf("Case %d: ",++Case);
              sort(a,a+n);
              int cnt1 = 1,cnt2 = 1,pre = 1,flag = 1;
              //cnt代表还需要多少个当前的数字才能合成1
      		//pre代表当前数字
      		//如果还需要的数>剩下的数,那么提前break 
              for(int i=0;i<n;i++){
                  while(cnt1+cnt2<=n-i && pre<a[i].val)
                      cnt1<<=1,cnt2<<=1,pre++;
                  if(cnt1+cnt2>n-i){flag = 0;break;}
                  if(cnt1)--cnt1,vis[a[i].id] = 1;
                  else --cnt2;
                  if(!cnt1&&!cnt2)break;
              }
              if(!flag||cnt1||cnt2){
                  puts("NO");
                  continue;
              }
              puts("YES");
              for(int i=0;i<n;i++)printf("%d",vis[i]);
              puts("");
          }
          return 0;
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注