不充钱,你怎么AC?
题目:http://codevs.cn/problem/3027/
显然是DP题,先按线段的右端点升序排序
设 f[i] 为dp到第 i 个线段时最大的价值
目标状态为 max(f[i])
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #define N 1001 8 using namespace std; 9 10 struct line 11 { 12 int l,r,c; 13 } 14 a[N]; 15 int f[N],n,ans; 16 bool cmp(line x,line y) 17 { 18 return x.r<y.r; 19 } 20 int main() 21 { 22 int i,j; 23 scanf("%d",&n); 24 for (i=1;i<=n;i++) scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].c); 25 sort(a+1,a+1+n,cmp); 26 for (i=1;i<=n;i++) 27 { 28 for (j=0;j<i;j++) 29 { 30 if (a[i].l>=a[j].r) 31 { 32 f[i]=max(f[i],f[j]+a[i].c); 33 ans=max(f[i],ans); 34 } 35 } 36 } 37 printf("%d ",ans); 38 return 0; 39 }