题意:
时间为n,有k个任务,每个任务有一个开始时间和持续时间,从第一分钟开始,如果有开始的任务就要做,问最大空闲时间
n,k<=1e5
思路:
设 dp[i]为i~n时间中最大空闲时间,vector v[i]保存i开始的任务,倒推即可
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional> #define fst first #define sc second #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std; typedef double db; typedef long double ldb; typedef long long ll; typedef long long LL; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL; const db eps = 1e-6; const int mod = 1e9+7; const int maxn = 2e3+100; const int maxm = 2e6+100; const int inf = 0x3f3f3f3f; const db pi = acos(-1.0); int n, k; int a[maxn]; vector<int>v[maxn]; int dp[maxn]; int main(){ scanf("%d %d", &n, &k); for(int i = 1; i <= k; i++){ int x, y ; scanf("%d %d", &x, &y); v[x].pb(y); } for(int i = n; i >= 1; i --){ if(v[i].size()==0){ dp[i] = dp[i+1]+1; } else{ for(int j = 0; j < (int)v[i].size(); j++){ dp[i] = max(dp[i+v[i][j]],dp[i]); } } } printf("%d",dp[1]); return 0; } /**/