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 ...
随机推荐
- error: field has incomplete type
在头文件使用某一自定义的类的指针或引用时,只需要前置声明该类即可,然而如果该类中有静态成员时,必须包含该类的头文件,而不是使用前置声明.
- hdu4366 Successor (dfs序+zkw线段树)
Successor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Fluuter常遇到的问题
The ADB binary found at XX is obsolete and has seriousperformance problems with the Android Emulator ...
- httpservlet的service()、doget()、dopost方法
HttpServlet里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletReques ...
- Finding Comments in Source Code Using Regular Expressions
Many text editors have advanced find (and replace) features. When I’m programming, I like to use an ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- LIS与LCS的nlogn解法
LIS(nlogn) #include<iostream> #include<cstdio> using namespace std; ; int a[maxn]; int n ...
- poj 2769 感觉♂良好 (单调栈)
poj 2769 感觉♂良好 (单调栈) 比尔正在研发一种关于人类情感的新数学理论.他最近致力于研究一个日子的好坏,如何影响人们对某个时期的回忆. 比尔为人的一天赋予了一个正整数值. 比尔称这个值为当 ...
- Openjudge2729 Blah数集(单调队列)
2729:Blah数集 总时间限制: 3000ms 内存限制: 65536kB 描述 大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以a为基的集合Ba定义如下:(1) a是集合Ba ...
- [Xcode 实际操作]六、媒体与动画-(14)使用SystemSoundId播放简短声音
目录:[Swift]Xcode实际操作 本文将演示如何播放音频素材. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[Add Files to "DemoApp"],往项目中导入文 ...