HDOJ 5501The Highest Mark(贪心+动态规划)

来源:互联网 发布:cms架构图 编辑:程序博客网 时间:2024/05/16 07:10

The Highest Mark

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 701    Accepted Submission(s): 290


Problem Description
The SDOI in 2045 is far from what it was been 30 years ago. Each competition has t minutes and n problems.

The ith problem with the original mark of Ai(Ai106),and it decreases Bi by each minute. It is guaranteed that it does not go to minus when the competition ends. For example someone solves the ith problem after x minutes of the competition beginning. He/She will get AiBix marks.

If someone solves a problem on x minute. He/She will begin to solve the next problem on x+1 minute.

dxy who attend this competition with excellent strength, can measure the time of solving each problem exactly.He will spend Ci(Cit) minutes to solve the ith problem. It is because he is so godlike that he can solve every problem of this competition. But to the limitation of time, it's probable he cannot solve every problem in this competition. He wanted to arrange the order of solving problems to get the highest mark in this competition.
 

Input
There is an positive integer T(T10) in the first line for the number of testcases.(the number of testcases with n>200 is no more than 5)

For each testcase, there are two integers in the first line n(1n1000) and t(1t3000) for the number of problems and the time limitation of this competition.

There are n lines followed and three positive integers each line Ai,Bi,Ci. For the original mark,the mark decreasing per minute and the time dxy of solving this problem will spend.


Hint:
First to solve problem 2 and then solve problem 1 he will get 88 marks. Higher than any other order.
 

Output
For each testcase output a line for an integer, for the highest mark dxy will get in this competition.
 

Sample Input
14 10110 5 930 2 180 4 850 3 2
 

Sample Output
88
 

Source
BestCoder Round #59 (div.1)
 

题解: 这道题考察的是贪心思想和动态规划。

首先我们考虑,假如我们已经确定了要做哪些题目,按什么顺序做这些题目最好。

假设已经确定了要做其中的mmm道题,某一个方案中做题的顺序是依次做x1,x2→xmx_{1},x_{2}\rightarrow{x}_{m}x1,x2xm,那么对于这个方案中任意的相邻两项xi{x}_{i}xi,xi+1{x}_{i+1}xi+1,考虑交换这两项的顺序,方案是否会变得更优,交换方案中的相邻两项,只会对这两道题的得分有影响,对其余的题目不会产生影响。

如果不交换这两项,损失的分数是 Cxi∗Bxi+1+KC_{x_{i}} * B_{x_{i+1}} + KCxiBxi+1+K,如果交换这两项,损失的分数是Cxi+1∗Bxi+KC_{x_{i+1}} * B_{x_{i}} + KCxi+1Bxi+K (K是一个常数) 所以只需要判断是否 Cxi∗Bxi+1≤Cxi+1∗Bxi+KC_{x_{i}} * B_{x_{i+1}} \leq C_{x_{i+1}} * B_{x_{i}} + KCxiBxi+1Cxi+1Bxi+K,如果此不等式成立,那么应该交换这两项。对上式移项得 Bxi+1/Cxi+1>Bxi/CxiB_{x_{i+1}} / C_{x_{i+1}} > B_{x_{i}} / C_{x_{i}}Bxi+1/Cxi+1>Bxi/Cxi。所以对于一个确定的题目集合,做题的最优顺序只与每道题目的Bi/Ci B_i / C_iBi/Ci有关,按每道题目扣分速度与做题时间的比值排序,按照比值从大到小做题。

因此我们先对所有的题目按照这个比值进行排序,接下来,只要按照排好的顺序,选择做哪些题目就可以了。这相当于一个简单的“背包问题”,使用动态规划来解决。dpi{dp}_idpi表示恰好用了iii分钟的最高得分。状态转移方程为dpi=max1≤j≤ndpi−Cj+Aj−(i∗Bj){dp}_i = \max_{1\leq j\leq n}{dp}_{i-C_j} + A_j - (i * B_j)dpi=max1jndpiCj+Aj(iBj)

最终答案是max0≤i≤tdpi\max_{0 \leq i \leq t}{dp_i}max0itdpi



#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 3010;int dp[N];struct node{int a, b, c;bool operator<(const node & X) const {return b * X.c > c * X.b;}}Node[N];int main() {int T;scanf("%d", &T);while (T--) {int n, t;scanf("%d%d", &n, &t);for (int i = 0; i < n; ++i) scanf("%d%d%d", &Node[i].a, &Node[i].b, &Node[i].c);sort(Node, Node + n);int a, b, c;memset(dp, -1, sizeof(dp));dp[0] = 0;for (int i = 0; i < n; ++i) {a = Node[i].a;b = Node[i].b;c = Node[i].c;for (int j = a / b - c; j >= 0; --j) {if (j + c <= t && dp[j + c] < dp[j] + a - (j + c) * b)dp[j + c] = dp[j] + a - (j + c) * b;}}int ans = 0;for (int i = 1; i <= t; ++i)if (ans < dp[i]) ans = dp[i];printf("%d\n", ans);}return 0;}


0 0