动态规划 UVA 11908

                                                                                         Skyscraper

Description

The Build n' Profit construction company is about to build its tallest building. It will be huge, the tallest building in the world by a wide margin. It will house hundreds of thousands of people and have rocket-powered elevators to the upper floors. They even plan for a shuttle docking station instead of a helipad on its roof. But any building of that size is extremely costly to build and maintain, and even after selling and renting out all the floor-space it will be very difficult to meet the costs. Luckily, they have come up with a great solution. They will place advertisements on the outer walls of the building for a hefty charge. This will help offset some of the costs and bring in a profit.

However, feedbacks from prospective buyers of this advertisement space have brought up a new problem. Each customer wants a specific sized advertisement placed at a specific height, and they will pay a certain amount of money for it. Each advertisement order specifies its position (i.e. the lowest floor of the advertisement) and its size (i.e. the number of floors it covers, including the starting floor). Each advertisement spans the whole face of the building, so no two advertisements can occupy the same floor and no floors can be partially covered. Each order also includes the amount to be earned if that advertisement is placed on the building. Of course, no money is earned if only part of an advertisement is placed, or it is placed in any other position.

Since many of the advertisements want some of the same floors as others, it is often impossible to choose all of them. Can you help choosing which of the orders to accept so that the above constraints are fulfilled and the amount of profit is maximized?

Input

The first line of input will contain T (≤ 50) denoting the number of cases.

Each case starts with an integer N (1 ≤ N ≤ 30000) denoting the number of advertisement orders. Each of the next Nlines represents an advertisement by three integers A (0 ≤ A ≤ 105), B (1 ≤ B ≤ 105andC (1 ≤ C ≤ 1000)denoting the lowest floor, the number of floors the advertisement covers (including the lowest floor) and the amount of money earned for placing it, respectively.

Output

For each case, print the case number and the maximum profit they can achieve.

Sample Input

1
3
1 5 1
2 10 3
7 12 1

Sample Output

Case 1: 3

 

#include < iostream >
#include
< stdio.h >
#include
< algorithm >
#include
< string .h >
using namespace std;

struct ad
{
int begin,end,distance,glod;
}a[
30001 ];

bool cmp(ad x,ad y)
{
if (x.end != y.end)
return x.end < y.end;
else
return
x.glod
< y.glod;//可以不安价值排序
}
int main()

{
int t;
int d[ 30001 ];
scanf(
" %d " , & t);
for ( int l = 1 ;l <= t;l ++ )
{
int n;
scanf(
" %d " , & n);
for ( int i = 1 ;i <= n;i ++ )
{
scanf(
" %d %d %d " , & a[i].begin, & a[i].distance, & a[i].glod);
a[i].end
= a[i].begin + a[i].distance - 1 ;
}
sort(a
+ 1 ,a + n + 1 ,cmp);
d[
1 ] = a[ 1 ].glod;
for ( int i = 2 ;i <= n;i ++ )
{
int k = 0 ;
for ( int j = i - 1 ;j > 0 ;j -- )
{
if (a[j].end < a[i].begin)
{
k
= j;
break ;
}
}
if (d[i - 1 ] < d[k] + a[i].glod)
d[i]
= d[k] + a[i].glod;
else
d[i]
= d[i - 1 ];
}
printf(
" Case %d: %d\n " ,l,d[n]);
}
return 0 ;
}

感谢yy17yy的指导

转载于:https://www.cnblogs.com/cyiner/archive/2011/05/16/2048258.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值