Codevs 3027 线段覆盖2

Codevs 3027 线段覆盖 2

题目描述 Description

数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段,使得这些线段两两不覆盖(端点可以重合)且线段价值之和最大。n<=1000


输入描述 Input Description

第一行一个整数n,表示有多少条线段。
接下来n行每行三个整数, ai bi ci,分别代表第i条线段的左端点ai,右端点bi(保证左端点<右端点)和价值ci。


输出描述 Output Description

输出能够获得的最大价值


样例输入 Sample Input
3
1 2 1
2 3 2
1 3 4


样例输出 Sample Output
4


数据范围及提示 Data Size & Hint

数据范围
对于40%的数据,n≤10;
对于100%的数据,n≤1000;
0<=ai,bi<=1000000
0<=ci<=1000000

分析

segment[i].s,e,v分别表示左右端点及价值;
以f[i]表示选到第i条线段且选第i条线段的总价值,那么
f[i]:=max(f[i],f[j]+segment[i].v);{其中 1<=j<=i-1};
表示选到第j条线段再选上第i条线段的最大值;


代码如下

program p3027;
type rec=record
      s,e,v:longint;
     end;
var n,i,j,ans:longint;
    f:array[1..1000] of longint;
    segment:array[1..1000] of rec;
function max(a,b:longint):longint;
begin
 if a>b then exit(a);
 exit(b);
end;

procedure qsort(l,r:longint);
var temp:rec;
    i,j,mid:longint;
begin
 i:=l;
 j:=r;
 mid:=segment[(i+j)>>1].e;
 while i<=j do
  begin
   while segment[i].e<mid do inc(i);
   while segment[j].e>mid do dec(j);
   if i<=j
    then
     begin
      temp:=segment[i];
      segment[i]:=segment[j];
      segment[j]:=temp;
      inc(i);
      dec(j);
     end;
  end;
 if i<r then qsort(i,r);
 if l<j then qsort(l,j);
end;

begin
 readln(n);
 for i:=1 to n do
  begin
   readln(segment[i].s,segment[i].e,segment[i].v);
  end;
 qsort(1,n);
 for i:=1 to n do f[i]:=segment[i].v;
 for i:=2 to n do
  for j:=1 to i-1 do
   begin
    if segment[i].s>=segment[j].e
     then
      begin
       f[i]:=max(f[i],f[j]+segment[i].v);
      end;
   end;
 ans:=0;
 for i:=1 to n do
  ans:=max(ans,f[i]);
 write(ans);
end.

评测结果

测试通过 Accepted

总耗时: 7 ms 0 / 0 数据通过测试. 运行结果 测试点#1.in 结果:AC 内存使用量: 256kB
时间使用量: 0ms 测试点#2.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms
测试点#3.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms 测试点#4.in 结果:AC
内存使用量: 256kB 时间使用量: 2ms 测试点#5.in 结果:AC 内存使用量: 256kB
时间使用量: 3ms

呃呃

相信未来还有。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值