RENT - Rent your airplane and make money SPOJ Solution using Operator Overloading

 #include <bits/stdc++.h>
using namespace std;
struct job{
  int start;
  int finish;
  int cost;
};
struct comp{
  bool operator()(const job& x,int y){
    return x.finish > y;
  }
  bool operator()(int x,const job& y){
    return x < y.finish;
  }
  bool operator()(const job& x,const job& y){
    return x.finish < y.finish;
  }
};
int main(){  
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    vector<job> a(n);
    for(int i=0;i<n;i++){
      int s,d,p;
      cin>>s>>d>>p;
      a[i].start=s;
      a[i].finish=s+d;
      a[i].cost=p;
    }
    sort(a.begin(),a.end(),comp());
    vector<int> dp(n);
    dp[0]=a[0].cost;
    for(int i=1;i<n;i++){
      int j=upper_bound(a.begin(),a.begin()+i,(int)a[i].start, comp())-a.begin();
      j--;
      if(j==i-1 && a[i-1].finish>a[i].start)dp[i]=max(a[i].cost,dp[i-1]);
      else dp[i]=max(dp[j]+a[i].cost,dp[i-1]);
    }
    cout<<dp[n-1]<<'\n';
  }
}

Comments