Skip to main content

RENT - Rent your airplane and make money

Problem Link

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

typedef long long ll;

struct Order
{
ll s;
ll e;
ll p;

Order(){}
Order(ll s, ll e, ll p)
{
this->s = s;
this->e = e;
this->p = p;
}

bool operator <(const Order &other) const {
return e < other.e;
}
};

int findLastNonConflictingOrder(Order arr[], int index)
{

int start = 0;
int end = index -1;
int result = -1;

while(start <= end)
{
int mid = (start + end) / 2;
if(arr[index].s >= arr[mid].e)
{
result = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}

return result;
}

void findMaxProfit(Order arr[], int n)
{
ll dp[n];
dp[0] = arr[0].p;
ll profitInclusive, profitExclusive;

for(int i = 1; i < n; i++)
{
profitInclusive = arr[i].p;
int j = findLastNonConflictingOrder(arr, i);
if(j != -1)
{
profitInclusive += dp[j];
}

profitExclusive = dp[i - 1];

dp[i] = max(profitExclusive, profitInclusive);
}

cout << dp[n - 1] << endl;
}
int main(int argc, char const *argv[])
{
int t, n;
ll s, d, p;

cin >> t;
while(t--)
{
cin >> n;
Order arr[n];

for(int i = 0; i < n; i++)
{
cin >> s >> d >> p;
arr[i] = Order(s, s + d, p);
}

// sort in ascending order of end time
sort(arr, arr + n);
findMaxProfit(arr, n);

}
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...