Justice

这是一道关于权重分配的编程题目,要求将n个刻有正整数的权重分成两组,使得每组的权重之和大于等于1/2克。题目提供样例输入和输出,并附有解题思路。解题的关键在于找到合适的分配方式,以满足条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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

思路:

当k = 1时,此时是1/2,那么分成两部分,两个组都需要一个1/2,我们设cnt1为第一组组成1/2需要的k的数量,cnt2为第二组组成1/2需要的k的数量,那么当k = 1时,cnt1 = 1, cnt2 = 1。循环着走,我们知道当k = 2时,cnt1 = 2,cnt2 = 2,依次循环,每次我们找在剩余的数中能否满足条件。
AC代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int ans[N];

struct node
{
    int val, idx;
    bool operator < (const node &t) const
    {
        return val < t.val;
    }
}a[N];

int main()
{
    int t; cin >> t;
    for (int k = 1; k <= t; k ++)
    {
        int n; cin >> n;
        for (int i = 1; i <= n; i ++) { cin >> a[i].val; a[i].idx = i; }

        sort(a + 1, a + n + 1);

        memset(ans, 0, sizeof ans);

        bool flag = 0;
        int cnt1 = 1, cnt2 = 1, p = 1;
        for (int i = 1; i <= n; i ++)
        {
        ///n - i + 1表示剩余的数,因为我们是排了序的。循环完了的时候p = a[i].val。
            while (cnt1 + cnt2 <= n - i + 1 && p < a[i].val)
            {
                cnt1 *= 2;
                cnt2 *= 2;
                p ++;
            }

			///因为现在的数字只有n - i + 1个数字没有被使用过,所以如果需要的总数超过了这个,说明是不能够成立的。
            if (cnt1 + cnt2 > n - i + 1) { flag = 1; break; }

			///这里是进行分组。如果是第一组需要,我们分给第一组(这里也可以分给第二组,顺序没有影响)。
            if (cnt1) { cnt1 --; ans[a[i].idx] = 1; }
            else cnt2 --;

			///这里是条件成立的情况,就是恰好给两组分完。
            if (!cnt1 && !cnt2) break;
        }

        if (flag || cnt1 || cnt2) printf("Case %d: NO\n", k);
        else
        {
            printf("Case %d: YES\n", k);
            for (int i = 1; i < n; i ++) cout << ans[i];
            cout << ans[n] << endl;
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值