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
The ith problem with the original mark of Ai(Ai≤106) ,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 Ai−Bi∗x 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(Ci≤t) 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.
For each testcase, there are two integers in the first line n(1≤n≤1000) and t(1≤t≤3000) 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.
1 4 10 110 5 9 30 2 1 80 4 8 50 3 2
88
题解: 这道题考察的是贪心思想和动态规划。
首先我们考虑,假如我们已经确定了要做哪些题目,按什么顺序做这些题目最好。
假设已经确定了要做其中的m道题,某一个方案中做题的顺序是依次做x1,x2→xm,那么对于这个方案中任意的相邻两项xi,xi+1,考虑交换这两项的顺序,方案是否会变得更优,交换方案中的相邻两项,只会对这两道题的得分有影响,对其余的题目不会产生影响。
如果不交换这两项,损失的分数是 Cxi∗Bxi+1+K,如果交换这两项,损失的分数是Cxi+1∗Bxi+K (K是一个常数) 所以只需要判断是否 Cxi∗Bxi+1≤Cxi+1∗Bxi+K,如果此不等式成立,那么应该交换这两项。对上式移项得 Bxi+1/Cxi+1>Bxi/Cxi。所以对于一个确定的题目集合,做题的最优顺序只与每道题目的Bi/Ci有关,按每道题目扣分速度与做题时间的比值排序,按照比值从大到小做题。
因此我们先对所有的题目按照这个比值进行排序,接下来,只要按照排好的顺序,选择做哪些题目就可以了。这相当于一个简单的“背包问题”,使用动态规划来解决。dpi表示恰好用了i分钟的最高得分。状态转移方程为dpi=max1≤j≤ndpi−Cj+Aj−(i∗Bj)。
最终答案是max0≤i≤tdpi。
#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;
}