dp题,状态转移方程大概是这样
if(edge[i].l>=edge[j].r) //
edge[i].maxn=max()
if(edge[i].l>=edge[j].r) //如果不发生覆盖
edge[i].maxn=max(edge[i].maxn,edge[j].maxn+edge[i].v)
下面是我写的代码
如果没算错的话,时间复杂度应该是O(n^2) 【不确定对,因为我计算复杂度这块很弱- -】
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct eeee{
int l,r,v,maxn;
}edge[1001];
bool cmp(eeee a,eeee b)
{
if(a.l!=b.l)return a.l<b.l?1:0;
else return a.r<b.r?1:0;
}
inline int g_n()
{
int t=0;
bool flag=0;
char c;
while((c=getchar())==' '||c=='\r'||c=='\n');
if(c=='-')
{
flag=1;
c=getchar();
}
while(isdigit(c))
{
t*=10;
t+=c;
t-='0';
c=getchar();
}
return t*(flag?-1:1);
}
int main()
{
int n;
n=g_n();
for(int i=1;i<=n;++i)
{
edge[i].l=g_n();
edge[i].r=g_n();
edge[i].v=g_n();
}
for(int i=1;i<=n;++i)//test
//cout<<edge[i].l<<' '<<edge[i].r<<' '<<edge[i].v<<'\n';//test
//cout<<'\n';//test
sort(edge+1,edge+n+1,cmp);
//for(int i=1;i<=n;++i)//test
//cout<<edge[i].l<<' '<<edge[i].r<<' '<<edge[i].v<<'\n';//test
int maxv=0;
for(int i=1;i<=n;++i)
{
for(int j=0;j<i;++j)
{
if(edge[i].l>=edge[j].r)
{
edge[i].maxn=max(edge[i].maxn,edge[j].maxn+edge[i].v);
//cout<<max(edge[i].maxn,edge[j].maxn+edge[i].v)<<' ';//test
}
}
//cout<<i<<' '<<edge[i].maxn<<' '<<maxv<<'\n';//test
if(edge[i].maxn>maxv)maxv=edge[i].maxn;
}
cout<<maxv;
return 0;
}