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

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

  • 相关阅读:
    简易文法
    词法分析实验报告
    0909上级作业
    vs2008的快捷键
    一道C++笔试题说一些知识
    Effective C++读书笔记
    鼠标拖动物体DEMO
    CFileDialog类的使用以及在非MFC程序下使用MFC的类资源
    3D制作魔方
    使用DXUT框架简单处理鼠标事件
  • 原文地址:https://www.cnblogs.com/leafsblogowo/p/12639190.html
Copyright © 2011-2022 走看看