Problem solving report: luogu P1280

Link to the topic: P1280 Nick's mission
No, it's flattering, and then it was trampled on.
Consider pushing from the back to the front. If there is no task to start here, you can take a break, and if so, update the value at this location.
Let \ (dp [i] \) be the maximum idle time from \ (i \) to \ (n \) .
Then:
If there is no starting task here:

\[dp[i]=dp[i+1]+1 \]

There are tasks to start:

\[dp[i]=dp[i+r_i] \]

Of course, “end” here means to end at the last minute of this minute, which can be understood as ending at the beginning of the next minute without occupying this minute.
The time complexity is \ (\ mathcal O (n + k) \) , you can pass this question.

\(Code\):

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

#define MAXN 10005
#define read(x) scanf("%d",&x)  
#define ll long long 

int n,k;
struct node{int l,r;}a[MAXN]; 
int b[MAXN]={0},ans=0,dp[MAXN]={0};

bool cmp(node x,node y){return x.l>y.l;}

int main()
{
	read(n),read(k);
	for(int i=1;i<=k;i++) read(a[i].l),read(a[i].r),b[a[i].l]++;
	sort(a+1,a+k+1,cmp);
	int k=1;
	for(int i=n;i>=1;i--)
	{
		if(!b[i]) dp[i]=dp[i+1]+1;
		else for(int j=1;j<=b[i];j++) dp[i]=max(dp[i],dp[i+a[k].r]),k++;
	}
	printf("%d\n",dp[1]);
	return 0;
}

Guess you like

Origin www.cnblogs.com/tlx-blog/p/12695533.html