首页 > 代码库 > HDU4968-Improving the GPA
HDU4968-Improving the GPA
题目链接
题意:每个成绩范围对应一个绩点,给出平均分avg,课程数n,求能得到的平均绩点的最大值和最小值。
思路:先预处理出每个成绩所对应的绩点,然后递推出所有情况,d[i][k]表示i个人有k分的绩点总数,所以可以得到动态转移方程。
当求最大值时d[i][k] = max(d[i][k], d[i - 1][k - j] + gpa[j])(j表示课程分数)
当求最小值时d[i][k] = min(d[i][k], d[i - 1][k - j] + gpa[j])(j表示课程分数)
注意初始化就可以了。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1005; const int INF = 100000.0; double dp1[15][MAXN], dp2[15][MAXN]; double gpa[MAXN]; void init() { for (int i = 60; i <= 69; i++) gpa[i] = 2.0; for (int i = 70; i <= 74; i++) gpa[i] = 2.5; for (int i = 75; i <= 79; i++) gpa[i] = 3.0; for (int i = 80; i <= 84; i++) gpa[i] = 3.5; for (int i = 85; i <= 100; i++) gpa[i] = 4.0; } void solve() { memset(dp1, 0, sizeof(dp1)); for (int i = 60; i <= 100; i++) dp1[1][i] = gpa[i]; for (int i = 2; i <= 10; i++) for (int j = 60; j <= 100; j++) for (int k = j; k <= MAXN; k++) if (dp1[i - 1][k - j] != 0) dp1[i][k] = max(dp1[i][k], dp1[i - 1][k - j] + gpa[j]); for (int i = 0; i <= 10; i++) for (int j = 0; j <= MAXN; j++) dp2[i][j] = INF; for (int i = 60; i <= 100; i++) dp2[1][i] = gpa[i]; for (int i = 2; i <= 10; i++) for (int j = 60; j <= 100; j++) for (int k = j; k <= MAXN; k++) if (dp2[i - 1][k - j] != INF) dp2[i][k] = min(dp2[i][k], dp2[i - 1][k - j] + gpa[j]); } int main() { int cas; scanf("%d", &cas); init(); solve(); while (cas--) { int avg, n; scanf("%d%d", &avg, &n); printf("%.4lf %.4lf\n", dp2[n][avg * n] / n, dp1[n][avg * n] / n); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。