Cerinta completa

Jesse loves cookies and wants the sweetness of some cookies to be greater than value . To do this, two cookies with the least sweetness are repeatedly mixed. This creates a special combined cookie with:

sweetness Least sweet cookie 2nd least sweet cookie).

This occurs until all the cookies have a sweetness .

Given the sweetness of a number of cookies, determine the minimum number of operations required. If it is not possible, return .

Example

The smallest values are .
Remove them then return to the array. Now .
Remove and return to the array. Now .
Remove , return and .
Finally, remove and return to . Now .
All values are so the process stops after iterations. Return .

Function Description
Complete the cookies function in the editor below.

cookies has the following parameters:

  • int k: the threshold value
  • int A[n]: an array of sweetness values

Returns

  • int: the number of iterations required or

Input Format

The first line has two space-separated integers, and , the size of and the minimum required sweetness respectively.

The next line contains space-separated integers, .

Constraints



Sample Input

STDIN               Function
-----               --------
6 7                 A[] size n = 6, k = 7
1 2 3 9 10 12       A = [1, 2, 3, 9, 10, 12]  

Sample Output

2

Explanation

Combine the first two cookies to create a cookie with sweetness =
After this operation, the cookies are .
Then, combine the cookies with sweetness and sweetness , to create a cookie with resulting sweetness =
Now, the cookies are .
All the cookies have a sweetness .

Thus, operations are required to increase the sweetness.


Limbajul de programare folosit: cpp14

Cod:

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    if (!(cin >> n >> k)) return 0;
    priority_queue<long long, vector<long long>, greater<long long>> pq;
    for (int i = 0; i < n; ++i) {
        long long x; cin >> x;
        pq.push(x);
    }
    int ops = 0;
    while (!pq.empty() && pq.top() < k) {
        if (pq.size() < 2) {
            cout << -1 << '\n';
            return 0;
        }
        long long a = pq.top(); pq.pop();
        long long b = pq.top(); pq.pop();
        pq.push(a + 2 * b);
        ops++;
    }
    cout << ops << '\n';
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464654796

Link challenge: https://www.hackerrank.com/challenges/jesse-and-cookies/problem

Jesse and Cookies