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 ...
随机推荐
- java——构造方法重载
class Person { private String name ; private int age ; public Person() { } public Person(String n,in ...
- elasticsearch2.x安装部署
目录 一.安装es以及插件 二.建立索引和映射,添加数据 三.备注 一.安装es以及插件 ElasticSearch-2.3.1版本,系统为CentOS 7.0位. 对应的网上下载地址都有: elas ...
- iView之DatePicker的datetimerange校验
使用DatePicker的type是datetimerange时,处理开始--结束的持续时间校验如下.遇到的问题:时间弹出校验提示,但是程序还是会继续往下走,所以调完校验后,再做判断开始时间是否为tr ...
- 使用c语言实现的常用函数
/* 为了面试准备的,有些在工作中也可以用用,本人算法方面比较欠缺,如果有更优秀的算法麻烦告诉我啊 */ /* strcat的实现 */ #include <assert.h> char* ...
- js基础(补10.10)
1.内嵌式: <html> <head> <title></title> </head> <body> <a href=& ...
- yzm10与战地信使 yzm10原创系列
yzm10与战地信使 M国与R国正进行着激烈的鏖战,此时的yzm10从R国窃取了最高军事机密,这份情报将是此次战役的转折点,如果M国得到了这份情报,就能够取得这次战争的胜利.yzm10当然是站在M国这 ...
- EF外键保存数据
using (DataContext dbcontext=new DataContext ()) { //emp.department.ID = dep.ID; //emp.department = ...
- mysql 快速导入大SQL文件
进入mysql mysql -u root -p 创建数据库 CREATE DATABASE 数据库名; 设置参数 set sql_log_bin=OFF;//关闭日志 ;//关闭autocommit ...
- 剑指Offer的学习笔记(C#篇)-- 二维数组中的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- linux 之基本命令学习总结
前言:从今天开始写这系列linux博客了(是学习刘遄老师的<linux就该这么学>),视频学习的资源可以在b站上找到:https://www.bilibili.com/video/av45 ...