标签:pre def col cin max 技术 lines describe span
题面:
There are n courses in the course selection system of Marjar University. The i-th course is described by two values: happiness Hi and credit Ci. If a student selects m courses x1, x2, ..., xm, then his comfort level of the semester can be defined as follows:
Edward, a student in Marjar University, wants to select some courses (also he can select no courses, then his comfort level is 0) to maximize his comfort level. Can you help him?
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains a integer n (1 ≤ n ≤ 500) -- the number of cources.
Each of the next n lines contains two integers Hi and Ci (1 ≤ Hi ≤ 10000, 1 ≤ Ci ≤ 100).
It is guaranteed that the sum of all n does not exceed 5000.
We kindly remind you that this problem contains large I/O file, so it‘s recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.
For each case, you should output one integer denoting the maximum comfort.
2 3 10 1 5 1 2 10 2 1 10 2 10
191 0
For the first case, Edward should select the first and second courses.
For the second case, Edward should select no courses.
这题嘛,刚开始看的时候 隐约觉得是DP,但是细想又不太好写DP的样子
然后一顿胡乱分析……
哦 原来要h大一点会好一点……
所以定义了DP的状态:dp[i][j]表示前i次课j个学分能选到的最大happiness
然后再跑一遍dp数组求这个Comfortable level的最值就好
然后就是 dp[i][j]可以滚动数组 其实就变成0 1 背包的变种了
代码:
#include<bits/stdc++.h> using namespace std; const int maxn = 50050; long long dp[maxn]; long long h[maxn],c[maxn]; int tot; int main(){ int t; cin>>t; while(t--){ int n; long long ans = -1; scanf("%d",&n); for(int i = 0; i < n; i++){ scanf("%lld%lld",h+i,c+i); tot += c[i]; } for(int i = 0; i < n; i++){ for(int j = tot; j >= c[i]; j--) dp[j] = max(dp[j], dp[j-c[i]] + h[i]); } for(int i = 0; i <= tot; i++){ long long x = dp[i]*dp[i] - dp[i]*i - i*i; ans = max(ans,x); } cout<<ans<<endl; memset(dp,0,sizeof(dp)); tot = 0; } return 0; }
qwq这道题 这种丢出一个乱七八糟公式当烟雾弹的题
不要怕难啥的,分析一下实质,简化一下问题,就会比较好写=-=
[省赛训练(DP)]Course Selection System
标签:pre def col cin max 技术 lines describe span
原文地址:https://www.cnblogs.com/leafsblogowo/p/12639190.html