当前位置: 首页 > news >正文

Xtreme9.0 - Car Spark 动态规划

Car Spark

题目连接:

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark

Description

John is a computer programmer and is highly known for his achievements in his field. In addition to being a passionate software professional, he is also passionate about motorcars and motorbikes.

So, after ending his successful and lengthy software career, he decides to take up his passion. He starts an organization by the name "Car Spark" (CS). CS is an organization from which you can rent luxury cars of your choice on an hourly rental basis.

CS would like to accept bookings for the weekend in advance, and then decide which bookings to process based on the profits that would be earned. When placing an order, customers quote the amount that they are willing to pay for that vehicle during that particular timespan. Since a car can only be given to one customer during a particular time period, CS must be careful about which bookings to process.

Initially CS has only one vehicle available for rent. To be the first hire for CS, you must develop a program to maximize revenue on bookings for this vehicle.

Input

Input begins with a single integer T, 1 <= T <= 100, which denotes number of test cases.

Each test case begins with a single integer N, 1 <= N <= 2000, which is the number of bookings John received.

The remainder of the test case consists of N lines containing three integers Bs, Be, and Ai each separated by a space, where Bs is the booking start time, Be is the booking end time, and Ai is the amount that the customer is willing to spend for the entire booking. Note that 0 <= Bs < Be <= 48 and 1 <= Ai <= 100000.

Note: The car may only be rented during the weekend, meaning from 12:00 AM on Saturday to 12:00 AM on Monday. Since the two days in the weekend have 48 hours, 12 noon on a Sunday would be the (24+12) 36th hour. Similarly, if the booking start time is 10:00 PM on Saturday and the booking end time is 12:00 AM on Sunday, then Bs would be 22 and Be would be 24.

Output

You are to output a single line for each test case, giving the maximum revenue John can make from the orders he received.

Sample Input

2
4
1 2 100
2 3 200
3 4 1600
1 3 2100
3
1 10 2000
2 5 100
6 9 400

Sample Output

3700
2000

Hint

For the first test case, for the time slot 1-3 maximum revenue John can make is 2100 (Max(100+200, 2100)) and for slot 3-4 he can make 1600. The maximum total revenue is 3700 (2100+1600).

Similarly for second test case, the maximum revenue he can generate is 2000.

题意

给你n个区间,每个区间有权值,然后让你选择不相交的区间,使得权值和最大。

题解

按照右端点排序之后,然后跑dp

这道题数据范围很小,所以直接暴力跑就行了。

代码

#include<bits/stdc++.h>
using namespace std;

int dp[50];
struct node{
    int l,r;
    int val;
};
bool cmp(node a,node b)
{
    if(a.r==b.r)return a.l<b.l;
    return a.r<b.r;
}
node p[3005];
void solve()
{
    memset(dp,0,sizeof(dp));
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].val);
    sort(p,p+n,cmp);
    for(int i=0;i<n;i++)
        for(int j=p[i].l;j>=0;j--)
            dp[p[i].r]=max(dp[p[i].r],dp[j]+p[i].val);
    int ans = 0;
    for(int i=0;i<50;i++)
        ans=max(ans,dp[i]);
    cout<<ans<<endl;
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/5957599.html

相关文章:

  • java 计算距离现在几分,几个小时,几天
  • pragma
  • VC/MFC使用OLE操作 EXCEL
  • js定时器的使用(实例讲解)
  • 1 storm基本概念 + storm编程规范及demo编写
  • 清北学堂模拟day6 花
  • awk之shell快速修改文件名
  • ajax测试Demo以及json简单的转化
  • 《深入理解JavaScript》—— JSON
  • VCS仿真 Dump Memory
  • 【读书笔记】《编程珠玑》第二章之算法设计的重要性
  • Web:AJAX的网络请求
  • Lambda表达式详解(转载)
  • JMeter 配置元件之计数器Counter
  • signalr-源码
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • chrome扩展demo1-小时钟
  • C学习-枚举(九)
  • HTML中设置input等文本框为不可操作
  • iOS小技巧之UIImagePickerController实现头像选择
  • Java教程_软件开发基础
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • overflow: hidden IE7无效
  • PHP变量
  • Swift 中的尾递归和蹦床
  • 闭包,sync使用细节
  • 从0实现一个tiny react(三)生命周期
  • 分类模型——Logistics Regression
  • 后端_MYSQL
  • 简单易用的leetcode开发测试工具(npm)
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 前端工程化(Gulp、Webpack)-webpack
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 数据可视化之 Sankey 桑基图的实现
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 以太坊客户端Geth命令参数详解
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • UI设计初学者应该如何入门?
  • ​2020 年大前端技术趋势解读
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • # 透过事物看本质的能力怎么培养?
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (003)SlickEdit Unity的补全
  • (pytorch进阶之路)扩散概率模型
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (算法二)滑动窗口
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (原創) 物件導向與老子思想 (OO)