- 遭遇战
- 2009-02-27 20:52:57 @
哪位大牛提供下这题DP的代码,我的N^2 DP60分,WA了不知道为什么
3 条评论
-
贱人在我右边 LV 9 @ 2017-03-18 18:03:47
我想写个线段树
-
2017-03-18 18:03:35@
这道题线段树好写吗
-
2016-09-25 15:43:04@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn=10010;
struct section
{
int L,R,c;
section() {}
section(int left,int right,int money):L(left),R(right),c(money) {}
}p[maxn];int n,s,e,cnt;
int ans[maxn];inline bool cmp(const section a,const section b)
{
return (a.L<b.L||a.L==b.L&&a.R<b.R);
}void init()
{
scanf("%d%d%d",&n,&s,&e); cnt=0;
for (int i=1;i<=n;i++)
{
int L,R,c; scanf("%d%d%d",&L,&R,&c);
if (R<s||L>e) continue;
p[++cnt]=section(L,R,c);
}
sort(p+1,p+1+cnt,cmp); memset(ans,-1,sizeof(ans));
}void work()
{
int i=0;
while (p[++i].L<=s)
{
ans[i]=p[i].c;}
for (;i<=cnt;i++)
{
for (int j=1;j<i;j++) if (ans[j]!=-1&&p[j].R+1>=p[i].L&&p[j].R<p[i].R)
{
if (ans[i]==-1||ans[i]!=-1&&ans[j]+p[i].c<ans[i])
{
ans[i]=ans[j]+p[i].c;
}
}
}
int mi=-1;
for (i=1;i<=cnt;i++) if (ans[i]!=-1&&p[i].R>=e)
{
if (mi==-1||mi!=-1&&ans[i]<mi)
{
mi=ans[i];
}
}
printf("%d\n",mi);
}int main()
{
init();
work();return 0;
}
- 1