题面:https://www.luogu.org/problem/P1280 本题唯一需要注意的是要倒序枚举,因为若正序枚举的话,在转移i的时候还没有转移i+t,所以只能倒序转移.Code:#includeiostream#includecstdlib#includecmath#includecst
题面:https://www.luogu.org/problem/P1280
本题唯一需要注意的是要倒序枚举,因为若正序枚举的话,在转移i的时候还没有转移i+t,所以只能倒序转移. Code: #include<iostream> #include<cstdlib> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=10005; int n,k,sum[N],f[N]; struct Node{ int st,last; }work[N]; int cmp(Node a,Node b){ return a.st>b.st; } int main() { long int i,j; scanf("%d%d",&n,&k); for(i=1;i<=k;i++){ scanf("%d%d",&work[i].st,&work[i].last); sum[work[i].st]++; } sort(work+1,work+1+k,cmp); int num=1; for(i=n;i>=1;i--){ if(sum[i]==0){ f[i]=f[i+1]+1; } else{ for(j=1;j<=sum[i];j++){ if(f[i+work[num].last]>f[i]){ f[i]=f[i+work[num].last]; } num++; } } } printf("%d\n",f[1]); return 0; }