Question: Amadeus , Recent Online Assessment Questions (17th August 2023) | Memory Distribution | Watering Plants
1
Entering edit mode

ADD COMMENTlink 15 months ago PoGo 2.4k
2
Entering edit mode

WATERING PLANTS

#include <bits/stdc++.h>
#include <algorithm>
#include <stack>
using namespace std;
#define pb push_back
#define ll long long int
#define MAXN 1000001
#define MOD 1000000007


int main() {
//    int t;
//    cin>>t;
//    while(t--)
//    {}
    int n;
    cin >> n;
    int c;
    cin >> c;
    vector<int> a(n);
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int i = 0, cur = c;
    while (i < n)
    {
        ans++;
        if (cur > a[i])
        {
            cur -= a[i];
        }
        else
        {
            int num = ceil(a[i] / (1.0 * c));
            ans += (i + i  + ((num - 1) * (i + 1) * 2));
            if ((a[i] % c) != 0)cur = (c - (a[i] % c));
            else cur = 0;
        }
        i++;
    }
    cout << ans  << endl;


    return 0;
}

ADD COMMENTlink 12 months ago mg2002 • 40
1
Entering edit mode

MEMORY DISTRIBUTION:-https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/practice-problems/algorithm/memory-distribution-39b62752/

 

#include<bits/stdc++.h>

using namespace std;

 

void solve(int n,int sum,vector<int>&v,int temp,int &ans,int blocks,bool &flag){

    if(temp>sum )return ;

    if(temp==sum){

        ans=max(ans,blocks);

        flag=true;

        return ;

    }

 

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

        solve(n,sum,v,temp+v[i],ans,blocks+1,flag);

        if(flag) return;

    }

    return ;

}

 

int Memory (int N, int A, int B, int C) {

    vector<int>v;

    v.push_back(A);

    v.push_back(B);

    v.push_back(C);

    sort(v.begin(),v.end());

    int ans=0,blocks=0;

    bool flag=false;

   solve(3,N,v,0,ans,blocks,flag);

    return ans;

   

}

int main() {

 

    ios::sync_with_stdio(0);

    cin.tie(0);

    int N;

    cin >> N;

    int A;

    cin >> A;

    int B;

    cin >> B;

    int C;

    cin >> C;

 

    int out_;

    out_ = Memory(N, A, B, C);

    cout << out_;

}

ADD COMMENTlink 12 months ago SAHIL VERMA • 10

Login before adding your answer.

Similar Posts
Loading Similar Posts