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 在这里插入图片描述 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

思路
这题要求求出两个大于1/2的物质,由题可知当存在2个ki=1或 4个ki=2 … 以此类推,该条件即成立,所以我们记录下符合要求的总数量,如果数组剩下的数的总数量小于此时需要的数量时,该条件无解,反之即成立。

代码实现

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=1e5+5;
typedef long long ll;
bool vis[N];
struct node
{
    int id,v;
}k[N];
int n,T;
bool cmp(node a,node b)
{
    return a.vn-i+1)
            {
                flag=true;
                break;
            }
            if(ne1)
            {
                ne1--;
                vis[k[i].id]=true;
            }
            else ne2--;
            if(!ne1 && !ne2) break;
        }
        if(flag || ne1 || ne2) printf("Case %d: NO\n",id);
        else
        {
            printf("Case %d: YES\n",id);
            for(int i=1;i<=n;i++) printf("%d",vis[i]);
            printf("\n");
        }
    }
    return 0;
}

你可能感兴趣的:(思维)