当前位置:网站首页>Question g: candy

Question g: candy

2022-06-11 02:00:00 Jihai

problem G: candy

The time limit : 1.000 Sec Memory limit : 128 MB

Title Description

Yes n Two children , Yes m Bag of candy , The first i The number of sweets in the bag is a[i]. It is known that n<=m<2n. Each child should have at least one bag of candy , You can only have two bags of candy at most . The goal of the assignment is to put this m All the bags of candy are divided , And make sure that each child gets the same amount of candy . If it can be done, output ”possible”, If not, output ”impossible”. Be careful : Can't open a bag of candy , It must be distributed in whole bags .

Input

first line , A positive integer R, Express R Group test data .1<=R<=10.
The format of each set of test data is :
first line , Two integers n and m.1<=n<=50.n<=m<2n.
The second line ,m It's an integer , The first i An integer is a[i].1<=a[i]<=1000.

Output

common R That's ok , One string per line ,”possible” perhaps ”impossible”, Double quotation marks do not need to be output .

The sample input

4
1 1
47 
3 5
10 8 10 1 1 
3 5
3 9 10 7 1 
6 11
1 1 1 2 1 1 1 1 1 1 1 

Sample output  

possible
impossible
possible
possible

Code :

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int w;
    cin >> w;
    while (w--) {
        int peo, n, peo_n[55]{}, candy[110], i, t;
        cin >> peo >> n;
        if (peo > n || n > 2 * peo)
        {
            cout << "impossible";
            continue;
 
        }
        for (i = 0; i < n; i++)
        {
            cin >> candy[i];
        }
        sort(candy, candy + n);
        for (i = n - 1; i >= n - peo; i--)
        {
            peo_n[n - i - 1] = peo_n[n - i - 1] + candy[i];
 
        }
        if (n > peo)
        {
            for (i = n - peo; i > 0; i--)
            {
 
                peo_n[i] += candy[i - 1];
 
 
            }
        }
        int temp = peo_n[0];
        bool qq = 0;
        for (i = 1; i < peo; i++)
        {
            if (temp != peo_n[i])
            {
                qq = 1;
            }
        }
        if (qq == 1)
            cout << "impossible";
        else
            cout << "possible";
        cout << endl;
    }
    return 0;
 
}

原网站

版权声明
本文为[Jihai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020622358303.html