给定比赛时间T和n个题目,你可以在任意时间提交题目,每个题目有一个初始分数maxPoints[i],每个单位时间题目的分数将会减少pointsPerMinute[i],即如果在时间t解决了第i个题目,那么获得的分数为maxPoints[i] - t * pointsPerMinute[i],另外做每个题目需要requiredTime[i]的时间,求能够获得的最大分数是多少?
题解
由于问题解决的先后,获得的分数是不一样的,因为我们首先得确定做题的选择顺序,根据相邻交换法,对于问题a和问题b,如果requiredTime[b]*pointsPerMinute[a]>requiredTime[a]*pointsPerMinute[b],先解决a再解决b获得的分数会比先b后a多,所以我们可以先根据这个条件对题目进行排序,之后就是01背包问题了
代码:
1 struct node 2 { 3 int mp, pp, rt; 4 }; 5 node a[55]; 6 int dp[100005]; 7 bool cmp(node a, node b) 8 { 9 return (LL)b.rt * a.pp > (LL)a.rt * b.pp; 10 } 11 class TheProgrammingContestDivOne 12 { 13 public: 14 LL find(int T, vector <int> maxPoints, vector <int> pointsPerMinute, vector <int> requiredTime) 15 { 16 int n = maxPoints.size(); 17 for (int i = 0; i < n; i++) 18 { 19 a[i + 1].mp = maxPoints[i]; 20 a[i + 1].pp = pointsPerMinute[i]; 21 a[i + 1].rt = requiredTime[i]; 22 } 23 sort(a + 1, a + n + 1, cmp); 24 memset(dp, 0, sizeof(dp)); 25 for (int i = 1; i <= n; i++) 26 for (int j = T; j >= a[i].rt; j--) 27 if (a[i].mp - (LL)a[i].pp * j >=0) 28 dp[j] = max(dp[j], dp[j - a[i].rt] + a[i].mp - a[i].pp * j); 29 int ans = 0; 30 for (int i = 0; i <= T; i++) ans = max(ans, dp[i]); 31 return ans; 32 } 33 };