Question: DBOI OA | Maximize the Multiples | Toy Shop | NITRR | 3rd August 2023
1
Entering edit mode

ADD COMMENTlink 16 months ago Delta 2.9k
2
Entering edit mode

// maximise the multiples

#include<bits/stdc++.h>  
using namespace std;
 
#define ll long long
 
int main() {
    ll n,m,k;cin >> n >> m >> k;
 
    vector<ll> vect;
 
    for(int i=0;i<n;i++) {
        ll var;cin >> var;
        vect.push_back(var%m ? m-var%m:var%m);
    }
 
    sort(vect.begin(), vect.end());
 
    ll sum=0;
    for(int i=0;i<n;i++) {
        if(vect[i]>k) break;
        sum++;
        k-=vect[i];
    }
 
    cout << sum << "\n";
     
 
    return 0;
}
ADD COMMENTlink 16 months ago sumeet kumar sahoo • 290
1
Entering edit mode

// 11 question answer :maximize multiples

#include <iostream>

#include <vector>

#include <algorithm>

#include <cmath>

 

using namespace std;

 

int maximizeMultiples(int n, int m, int k, vector<int>& arr) {

    int maxMultiples = 0;

   

    for(int i = 0; i < n; i++) {

        if(arr[i] % m == 0) {

            maxMultiples++;

        } else {

            int diff = m * ceil((double)arr[i] / m) - arr[i];

            int res1 = diff;

            if(res1 <= k) {

                arr[i] += diff;

                k -= diff;

                if(arr[i]%m==0)

                maxMultiples ++;

                else break;

            }

        }

    }

   

    return maxMultiples;

}

 

int main() {

    int n;

    int m;

    int k;

    cin >> n >> m >> k;

   

    vector<int> arr(n);

    for(int i = 0; i < n; ++i) {

        cin >> arr[i];

    }

   

    int result = maximizeMultiples(n, m, k, arr);

    cout << result << endl;

   

    return 0;

}

 

ADD COMMENTlink 16 months ago Amit • 50
0
Entering edit mode

// Question 12: Toy shop problem

 

#include <iostream>

#include <vector>

#include <algorithm>

 

using namespace std;

 

struct Shop {

    int coord;

    int toys;

};

 

int getMaxToys(vector<Shop> &shops, int p, int k) {

    int n = shops.size();

    vector<vector<int>> dp(n + 1, vector<int>(k + 1, 0));

 

    sort(shops.begin(), shops.end(), [](const Shop &a, const Shop &b) {

        return a.coord < b.coord;

    });

 

    for (int i = 1; i <= n; ++i) {

        for (int j = 0; j <= k; ++j) {

            int distance = abs(shops[i - 1].coord - p);

            if (distance <= j) {

                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - distance] + shops[i - 1].toys);

            } else {

                dp[i][j] = dp[i - 1][j];

            }

        }

    }

 

    return dp[n][k];

}

 

int main() {

int t;

cin >>t;

while(t--)

{

    int n, p, k;

    cin >> n >> p >> k;

 

    vector<Shop> shops(n);

    for (int i = 0; i < n; ++i) {

        cin >> shops[i].coord >> shops[i].toys;

    }

 

    int result = getMaxToys(shops, p, k);

    cout << result << endl;

}

    return 0;

}

 

ADD COMMENTlink 16 months ago Amit • 50

Login before adding your answer.

Similar Posts
Loading Similar Posts