Sort by

recency

|

353 Discussions

|

  • + 0 comments

    I am using min heap to solve this problem

    #include <bits/stdc++.h>
    #define lli long long int
    using namespace std;
    
    struct greaters {
        bool operator() (const long&a, const long&b) const {
            return a > b;
        }
    };
    
    int main (int argc, char *argv[]) {
    
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
    
        vector<int> v;
        lli ans = 0;
    
        lli n, k;
        cin >> n >> k;
        while(n--) {
            lli x;
            cin >> x;
            v.push_back(x);
        }
        // min heap
        make_heap(v.begin(), v.end(), greaters());
    		// check if there's a minimum element < k
        if (v.front() < k) {
            while(v.size() > 1) {
                int firstMinVal = v.front();
                // delete min heap
                pop_heap(v.begin(), v.end(), greaters());
                v.pop_back();
    
                int secondMinVal = v.front();
                // delete min heap
                pop_heap(v.begin(), v.end(), greaters());
                v.pop_back();
    
                int formula = firstMinVal + (2*secondMinVal);
    						// push heap
                v.push_back(formula);
                push_heap(v.begin(), v.end(), greaters());
    
                ans++;
    
                int minVal = v.front();
                if (minVal >= k) {
                    break;
                }
            }
        }
    
        if (v.size() == 1 && v.front() < k) {
            cout << -1 << "\n";
        } else {
            cout << ans << endl;
        }
    
    
        return 0;
    }
    
  • + 0 comments

    The problem state that the sweetness have to be greater than k But there's a test where the last sweet is exactly k and it still consider valid, not return -1 What do you even mean?

  • + 0 comments

    def cookies(k,A): # convert list A to priority queue heapq heapq.heapify(A)

    iteration = 0
    while any(c < k for c in A) and len(A) > 1:
        mix = heapq.heappop(A) + 2 * heapq.heappop(A)
        heapq.heappush(A,mix)
        iteration += 1
    
    
    if not all(c >= k for c in A):
        return -1
    
    return iteration
    
  • + 0 comments

    def cookies(k, A): A.sort(reverse = True) queue = [] mixes = 0 while True: if len(A) <= 1 and queue: A = queue[::-1] + A queue = [] if not A and not queue: return mixes if queue: if A[-1] <= queue[0]: last_num = A.pop() else: last_num = queue.pop(0) else: last_num = A.pop() if last_num >= k: next elif len(A) == 0 and last_num < k: return -1 else: if not queue: sec_num = A.pop() else: if A[-1] <= queue[0]: sec_num = A.pop() else: sec_num = queue.pop(0) new_num = last_num + 2 * sec_num queue.append(new_num) mixes += 1

  • + 0 comments

    Is it a good idea to use it for my content based website of cookieclickerunblocked, I'm running this website on WordPress right now, Please give me a honest advice. Thanks