zoukankan      html  css  js  c++  java
  • [省赛训练(DP)]Course Selection System

    题面:

    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:

    $$(sum_{i=1}^{m} H_{x_i})^2-(sum_{i=1}^{m} H_{x_i})	imes(sum_{i=1}^{m} C_{x_i})-(sum_{i=1}^{m} C_{x_i})^2$$

    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这道题 这种丢出一个乱七八糟公式当烟雾弹的题

    不要怕难啥的,分析一下实质,简化一下问题,就会比较好写=-=

  • 相关阅读:
    单线程的JavaScript是如何实现异步的
    前端优化之 -- 使用 require.context 让项目实现路由自动导入
    插入排序
    选择排序
    冒泡排序
    强缓存和协商缓存
    ES6 Set求两个数组的并集、交集、差集;以及对数组去重
    实现一个new操作符
    我理解的浅拷贝和深拷贝
    javascript专题系列--js乱序
  • 原文地址:https://www.cnblogs.com/leafsblogowo/p/12639190.html
Copyright © 2011-2022 走看看