Skip to main content

Mahesh and his lost array: ANUMLA

Problem Link

#include <iostream>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;

#define ll long long
const int N = 32768;

int main()
{
    int n, t;
    long long arr[N + 1];
   
    cin >> t;

    while(t--)
    {
        cin >> n;

        multiset<ll> s;

        long long sum[N + 1];
        long long element[N + 1];
        int maxIndex = 1 << n;
        for(int i = 0; i < maxIndex; i++)
        {
            cin >> arr[i];
        }

        sort(arr, arr + maxIndex);

        if(n == 1)
        {
            cout << arr[1] << endl;
            continue;
        }

        if(n == 2)
        {
            cout << arr[1] << " " <<  arr[2] << endl;
            continue;
        }


        int index = 0;
        int sumIndex = 0;

        for(int i = 1; i < maxIndex; i++)
        {
            int expected = -1;
            if(!s.empty())
            {
                expected = *s.begin();
            }

            if(arr[i] == expected)
            {
                s.erase(s.begin());
            }
            else
            {
                element[index++] = arr[i];
                int lastIndex = sumIndex;
                for(int j = 0; j < lastIndex; j++)
                {
                    sum[sumIndex] = sum[j] + arr[i];
                    s.insert(sum[sumIndex]);
                    sumIndex++;
                }

                sum[sumIndex++] = arr[i];

            }
        }

        for(int i = 0; i < index; i++)
        {
            cout << element[i] << " ";
        }
        cout << endl;

    }
    return 0;
}

Comments

Popular posts from this blog

ANARC09A

ANARC09A - Seinfeld #include <iostream> #include <stack> using namespace std; stack<char> st; int main() {     string s;     int c, k;     k = 1;     while(true)     {         cin >> s;         c = 0;         if(s[0] == '-')             break;         for(int i = 0; i < s.size(); i++)         {             st.push(s[i]);             //stack contains } and { then we can safely remove them from stack             if(st.top() == '}' && st.size() != 1)             {                 char a = st.top();               ...

Maximum Weight Difference -MAXDIFF

Problem Link #include <iostream> #include <algorithm> using namespace std; int arr[101]; int main() {     int t, n, k;     long sum1, sum2, res;         cin >> t;     while(t--)     {         cin >> n >> k;         for(int i = 0; i < n; i++)         {             cin >> arr[i];         }                 if(k > n / 2)         {             k = n - k;         }                 sort(arr, arr + n);         sum1 = sum2 = 0;                 for(int i = 0; i < k; i++)         {             sum1 ...

MKUHAR - Most Servings Meal

Problem Link #include <iostream> #include <cmath> using namespace std; struct node { int x, y, pm, sm, pv, sv; node(){} node(int x, int y, int pm, int sm, int pv, int sv) { this->x = x; this->y = y; this->pm = pm; this->sm = sm; this->pv = pv; this->sv = sv; } }; int n; node arr[101]; /* Find out if it is possible to have given number of servings for each ingredient with given money */ bool isServingPossible(int totalServings, int money) { int required = 0; /* check if we can have totalServings of each ingredient */ for(int i = 0; i < n; i++) { required = totalServings * arr[i].x; required -= arr[i].y; int minMoneySpent = 99999999; int limit = ceil(required / (double)arr[i].sm); for(int numSmallItem = 0; numSmallItem <= limit; numSmallItem++) { int totalSmallItem = numSmallItem * arr[i].sm; int numLargeItem = (required > totalSmallItem)? ceil((required - totalSmal...