码迷,mamicode.com
首页 > 其他好文 > 详细

[省赛训练(DP)]Course Selection System

时间:2020-04-05 22:35:03      阅读:93      评论:0      收藏:0      [点我收藏+]

标签: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?

Input

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++.

Output

For each case, you should output one integer denoting the maximum comfort.

Sample Input

2
3
10 1
5 1
2 10
2
1 10
2 10

Sample Output

191
0

Hint

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!