Skip to content

Instantly share code, notes, and snippets.

@taikoubou
Created December 12, 2013 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taikoubou/7930810 to your computer and use it in GitHub Desktop.
Save taikoubou/7930810 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll C,S,T,N;
ll solve(ll,ll,ll ,ll);
ll work[2][101];
short int dp[5000][100001];
int main(){
int p = 0;
cin >> C >> S >> T;
cin >> N;
for(ll i=0;i<5000;i++)for(long j=0;j<100001;j++)dp[i][j] = -1;
for(int i=0;i<N;i++){
cin >> work[0][i] >> work[1][i];
}
cout << solve(0,S,C,0) << endl;
return 0;
}
ll solve(ll month,ll i_s,ll i_c,ll i_n){
if(dp[month][i_s] != -1)return dp[month][i_s];
if(i_s >= T){
return dp[month][i_s] = month;
}
else{
ll res=-1,rec,t = i_s+i_c,tm = i_s+i_c+work[1][i_n]-work[0][i_n];
if(i_s >= work[0][i_n] && i_n < N)res = solve(month+1,tm,work[1][i_n]+i_c,i_n+1);
rec = solve(month+1,t,i_c,i_n);
if(res != -1){
if(rec < res)t = tm;
rec = min(res,rec);
}
return dp[rec][t] = rec;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment