时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2046

解决:894

题目描述:
    每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为“bg”。参加不同团体的bg会有不同的感觉,我们可以用一个非负整数为每个 bg定义一个“快乐度”。现给定一个bg列表,上面列出每个bg的快乐度、持续长度、bg发起人的离校时间,请你安排一系列bg的时间使得自己可以获得最 大的快乐度。

例如有4场bg:
    第1场快乐度为5,持续1小时,发起人必须在1小时后离开;
    第2场快乐度为10,持续2小时,发起人必须在3小时后离开;
    第3场快乐度为6,持续1小时,发起人必须在2小时后离开;
    第4场快乐度为3,持续1小时,发起人必须在1小时后离开。
    则获得最大快乐度的安排应该是:先开始第3场,获得快乐度6,在第1小时结束,发起人也来得及离开;再开始第2场,获得快乐度10,在第3小时结束,发起人正好来得及离开。此时已经无法再安排其他的bg,因为发起人都已经离开了学校。因此获得的最大快乐度为16。

注意bg必须在发起人离开前结束,你不可以中途离开一场bg,也不可以中途加入一场bg。
又因为你的人缘太好,可能有多达30个团体bg你,所以你需要写个程序来解决这个时间安排的问题。

输入:
    测试输入包含若干测试用例。每个测试用例的第1行包含一个整数N (<=30),随后有N行,每行给出一场bg的信息:
    h l t
    其中 h 是快乐度,l是持续时间(小时),t是发起人离校时间。数据保证l不大于t,因为若发起人必须在t小时后离开,bg必须在主人离开前结束。

当N为负数时输入结束。

输出:

每个测试用例的输出占一行,输出最大快乐度。

样例输入:
3
6 3 3
3 2 2
4 1 3
4
5 1 1
10 2 3
6 1 2
3 1 1
-1
样例输出:
7
16

第一眼的感觉就是这题不是贪心就是DP,果然---DP。

DP不熟啊!

参考代码:http://blog.csdn.net/wtyvhreal/article/details/42076485

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <map>
#include <string>
#include <queue>
#define INF 100000
using namespace std;
const int maxn = ;
typedef long long ll;
int m[][maxn];
int n;
struct bg_typ{
int h;
int l;
int t;
bool operator < (const bg_typ& A) const {
return A.t > t;
}
};
bg_typ bg[]; int main(){
while( cin >> n && n >= ){
int mmax = ;
for(int i=; i<=n; i++){
cin >> bg[i].h >> bg[i].l >> bg[i].t ;
if( bg[i].t > mmax ) mmax = bg[i].t;
}
sort(bg+,bg+n+); for(int i=; i<=n; i++)
m[i][] = m[][i] = ;
for(int i=; i<=n; i++){
for(int j=; j<=mmax; j++){
if( j<=bg[i].t && j-bg[i].l>=){
m[i][j] = max(m[i-][j],m[i-][j-bg[i].l]+bg[i].h);
} else m[i][j] = m[i-][j];
}
}
int result=m[n][mmax];
for(int j=mmax;j>=;--j)
if(result<m[n][j])
result=m[n][j];
cout<<result<<endl;
}
return ;
}

翻了翻题解,看到有的用dfs也做出来了,就测试了下是DP好,还是DFS好。

DFS代码:

#include <cstdio>
#include <algorithm> using namespace std; const int N = + ; struct Node
{
int h;
int l;
int t;
}; int n;
Node node[N];
int ans;
int w; bool cmp(const Node &a, const Node &b); void dfs(int cur, int t, int h); int main()
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &n) == ) {
if (n < )
break; w = ;
for (int i = ; i < n; i++) {
scanf("%d%d%d", &node[i].h, &node[i].l, &node[i].t);
w += node[i].h;
}
sort(node, node + n, cmp);
ans = ;
dfs(, , );
printf("%d\n", ans);
} return ;
} bool cmp(const Node &a, const Node &b)
{
if (a.t != b.t)
return a.t < b.t; return (double)a.h / a.l > (double)b.h / b.l;
} void dfs(int cur, int t, int h)
{
if (cur == n) {
if (ans < h)
ans = h;
return;
} w -= node[cur].h;
if (t + node[cur].l <= node[cur].t) {
dfs(cur + , t + node[cur].l, h + node[cur].h);
} if (h + w > ans)
dfs(cur + , t, h); w += node[cur].h;
}

在杭电测试的,上面的是动态规划,下面的是dfs。

 Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
18227261 2016-09-11 10:40:16 Accepted 1881 46MS 1928K 1480 B C++ Asimple
18227252 2016-09-11 10:39:32 Accepted 1881 62MS 1720K 1405 B C++ Asimple

各有各的优势吧!

每日一九度之 题目1030:毕业bg的更多相关文章

  1. 九度oj 题目1030:毕业bg

    题目描述:     每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为“bg”.参加不同团体的bg会有不同的感觉,我们可以用一个非负整数为每个bg定义一个“快乐度”.现给定一个b ...

  2. 每日一九度之 题目1076:N的阶乘

    时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:7601 解决:2749 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...

  3. 每日一九度之 题目1043:Day of Week

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7336 解决:2563 题目描述: We now use the Gregorian style of dating in Russia. ...

  4. 每日一九度之 题目1042:Coincidence

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3007 解决:1638 题目描述: Find a longest common subsequence of two strings. 输入 ...

  5. 每日一九度之 题目1041:Simple Sorting

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4883 解决:1860 题目描述: You are given an unsorted array of integer numbers. ...

  6. 每日一九度之 题目1040:Prime Number

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  7. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  8. 每日一九度之 题目1039:Zero-complexity Transposition

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3372 解决:1392 题目描述: You are given a sequence of integer numbers. Zero-co ...

  9. 每日一九度之 题目1033:继续xxx定律

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5502 解决:1351 题目描述:     当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,将3称为关键数, ...

随机推荐

  1. MFC项目中包含atlimage.h导致fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>

    因为要用到CImage所以包含了atlimage.h 报这个错误的话你只需要把atlimage.h放在afxwin.h的下方即可,不能让它在afxwin.h的上方

  2. [c++基本语法]——构造函数初始化列表

    c++构造函数初始化成员变量列表: #pragma once class Node { public: int data; // 权值 Node *parent; // 父节点 Node *left; ...

  3. IntelliJ IDEA 项目相关的几个重要概念介绍

    必备材料介绍 IntelliJ IDEA 对其他 IDE 转过来的用户有特别优待,对其专门整理了非常棒的资料,还请其他 IDE 过来的用户抽时间查看,会有很大帮助:Eclipse 用户可以看:http ...

  4. Idea 安装 lombok

    idea 目前是Java开发者最流行的一款编辑器.为了让java开发更加的简便idea 也提供了lombok的插件. 插件的按钮方式为: 1.进入idea的.setting面板 2.按照以下图进行操作 ...

  5. 学习OpenCV——Kmean(C++)

    从前也练习使用过OpenCV的Kmean算法,但是那版本低,而且也是基于C的开发.这两天由于造论文的需要把它重新翻出来在研究一下C++,发现有了些改进 kmeans C++: doublekmeans ...

  6. PostgreSQL Replication之第十三章 使用PL/Proxy扩展(2)

    13.2 设置 PL/Proxy 简短的理论介绍之后,我们可以继续前进并运行一些简单的PL/Proxy设置.要做到这一点,我们只需安装PL/Proxy并看看这是如何被使用的. 安装PL/Proxy是一 ...

  7. SQL Sever 身份验证 sa用户设置

    1.用windows身份验证登陆数据库找到sa用户 2.鼠标右键sa->属性->常规,设置密码. 3.选择状态->登陆选择已启用 4.选中当前数据库 鼠标右键->属性 5.选择 ...

  8. 【Origin】 叹文

    行文如流水, 千字挥手就: 偏偏伤脑筋, 哪得轻松事. -作于二零一五年五月三十日

  9. yii添加行的增删改查

    效果图: 控制器: <?phpnamespace backend\controllers;use Yii;use yii\web\Controller;use backend\models\Zh ...

  10. 查看在线EBS用户的相关信息

    --查看在线EBS用户的相关信息 SELECT PAP.FULL_NAME, FU.USER_NAME, FAT.APPLICATION_NAME, FRT.RESPONSIBILITY_NAME, ...