HDU 5501 The Highest Mark (贪心+DP,经典)
题意:
有n道题目,每道题目的初始分数为Ai,分数每分钟减少Bi,完成此题需要Ci分钟,问在t分钟内最多能获得多少分?
思路:
好题~
如果没有B的话,就是一道裸的01背包的题目了。每道题目的得分为:v=A-B*g (其中g为完成这道题目的时刻),想要用背包解的话是可以的,但是完成的次序不同的话,得分也可能受到影响。那就排个序得了,问题在于如何排序?假设已经确定要做哪些题了,则如果交换了相邻两道题的位置,对总分数是有益的,那么肯定是换了,而且他们对其他的题目完全无影响,不妨假设只有两道题要完成,现在要决定他们的完成次序。假设先完成题目1,再完成题目2,设t=C1+C2,那么总分为v1=A1-B1*C1 + A2-B2*t;如果交换次序,则总分为v2=A2-B2*C2 + A1-B1*t,那么v1<=v2时,明显有必要交换位置。
由v1<=v2得到 A1-B1*C1 + A2-B2*t <= A2-B2*C2 + A1-B1*t
化简得到 B1*C1 + B2*t >= B2*C2 + B1*t
那么在排序时直接用这条式子就行了。
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <deque>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define back que[rear-1]
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
struct node
{
int a, b, c;
double rate;
}seq[N];
int n, time, dp[N*];
int cmp(node a,node b)
{
int t2=a.c+b.c;
return b.b*b.c+a.b*t2>=a.b*a.c+b.b*t2;
}
int cal()
{
sort(seq+,seq+n+, cmp);
memset(dp,-,sizeof(dp));
dp[]=;
for(int i=; i<=n; i++)
for(int j=time; j>=seq[i].c; j--)
{
if( dp[j-seq[i].c]< ) continue;
int v=seq[i].a - j*seq[i].b;
dp[j]=max( dp[j], dp[j-seq[i].c]+v );
}
int ans=;
for(int i=time; i>=; i--)
ans=max(ans, dp[i]);
return ans;
} int main()
{
freopen("input.txt", "r", stdin);
int t;cin>>t;
while(t--)
{
scanf("%d%d",&n,&time);
for(int i=; i<=n; i++)
scanf("%d%d%d", &seq[i].a,&seq[i].b,&seq[i].c);
cout<<cal()<<endl;
}
return ;
}
AC代码
用官方题解的代码:
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <deque>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define back que[rear-1]
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
struct node
{
int a, b, c;
double rate;
}seq[N];
int n, time, dp[N*];
int cmp(node a,node b)
{
return a.rate>b.rate;
}
int cal()
{
sort(seq+,seq+n+, cmp);
memset(dp,-,sizeof(dp));
dp[]=;
for(int i=; i<=n; i++)
for(int j=time; j>=seq[i].c; j--)
{
if( dp[j-seq[i].c]< ) continue;
int v=seq[i].a - j*seq[i].b;
dp[j]=max( dp[j], dp[j-seq[i].c]+v );
}
int ans=;
for(int i=time; i>=; i--)
ans=max(ans, dp[i]);
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
int t;cin>>t;
while(t--)
{
scanf("%d%d",&n,&time);
for(int i=; i<=n; i++)
{
scanf("%d%d%d", &seq[i].a,&seq[i].b,&seq[i].c);
seq[i].rate=1.0*seq[i].b/seq[i].c;
}
cout<<cal()<<endl;
}
return ;
}
AC代码
HDU 5501 The Highest Mark (贪心+DP,经典)的更多相关文章
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 5501 The Highest Mark
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5501 The Highest Mark Accepts: 32 Submissions: 193 ...
- HDU 5501——The Highest Mark——————【贪心+dp】
The Highest Mark Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- hdu 5501 The Highest Mark(贪心+01背包)
题意:类似cf的赛制,每道题目有A,B,C三个值,A表示初始分数,B表示每分钟题的分数会减少B,C表示做这道题需要C分钟,数据保证分数不会变为负数.现在给出比赛时长,问安排做题的顺序,求最大得分. 思 ...
- HDU 5501:The Highest Mark 01背包
The Highest Mark Accepts: 71 Submissions: 197 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- HDU5501/BestCoder Round #59 (div.2)The Highest Mark dp+贪心
The Highest Mark 问题描述 2045年的SD省队选拔,赛制和三十年前已是完全不同.一场比赛的比赛时间有 tt 分钟,有 nn 道题目. 第 ii 道题目的初始分值为 A_i(A_i \ ...
- HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的 ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu5501 The Highest Mark
Problem Description The SDOI in 2045 is far from what it was been 30 years ago. Each competition has ...
随机推荐
- centos6.5安装sublime Text3破解版
安装后的效果图: 在csdn上找到了一个破解版. http://download.csdn.net/download/phpscott2/5356561 安装后,能够运行,但每次打开都提示没有安装py ...
- 条款32:确定你的public继承塑模出is-a的关系
Make sure public inheritance models "is –a " 如果令clsss D 以public的形式继承class B,你便是告诉编译器说,每一个类 ...
- 3-C++程序的结构1.5
多文件结构和编译预处理命令 1.c++程序的一般组织结构 通常一个项目至少划分为三个文件:类定义文件(*.h文件).类实现文件(*.cpp文件)和类的使用文件(*.cpp,主函数文件).如下: 这三个 ...
- login.aspx.cs
using System; //指令+系统(命名空间)using Sys ...
- Django 的 之 视图
Django的View(视图) 一个视图函数(类),简称视图, 是个简单的python函数(类),它接受wed请求并且返回web 响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...
- 牛客多校3 A-PACM Team(状压降维+路径背包)
PACM Team 链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144 ...
- Eclipse与IntelliJ IDEA区别
1.没有workspace,新增modules(Workspace-Project,Project-Module) 2.没有perspectives,自动根据上下文调用相关工具 3.没有保存按钮,自动 ...
- 记一次前端面试~终于拿到理想中的offer!
2019年已经过去一半,终于拿到一直想去的公司offer,也算是实现了今年的一个小目标. 由于这家公司是我从去年到现在最想去的公司,本次换工作一直没有投,希望先积累下面试经验再投. 没有想到居然先在b ...
- 洛谷 - P3952 - 时间复杂度 - 模拟
https://www.luogu.org/problemnew/show/P3952 这个模拟,注意每次进入循环的时候把新状态全部入栈,退出循环的时候就退栈. 第一次就错在发现ERR退出太及时,把剩 ...
- SVN版本控制图标未显示或显示异常解决方法
SVN版本控制图标未显示问题解决方法,刚开始遇到这个问题的时候,好苦恼.经过“千辛万苦”的努力,终于得以解决,分享给大家,希望能帮到各位哦! 工具/原料 SVN安装包 方法/步骤 首先安装 ...