Course Selection System

在这里插入图片描述
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.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
struct node
{
    int h,c;
}p[501];
//用SH表示对h求和,用SC表示对c求和,dp[j]数组 j表示SC dp 的值表示对应SC下的SH
//转化为01背包问题
//c取不同的值SH不同
int main()
{
    int i,j,T,n;
    long long int sum,dp[50005];
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].h,&p[i].c);
        }
        for(i=1;i<=n;i++)
        {
            for(j=50000;j;j--)
            {
                if(p[i].c>j) break;
                dp[j]=max(dp[j],dp[j-p[i].c]+p[i].h);//算包不包含第i个c时的SH的最大值
            }
        }
        sum=0;
        for(i=1;i<=50000;i++) //i表示SC
        {
            sum=max(sum,dp[i]*dp[i]-i*dp[i]-(long long )i*i); //根据公式
        }
        printf("%lld\n",sum);

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
while (true) { String str = input.nextLine(); if (str.equals("end")) { break; } else { String[] nextLine = str.split(" "); if (nextLine.length == 3) { Course course = new Course(nextLine[0], nextLine[1], nextLine[2]); if (course.getCourseNature().equals("必修") && course.getAssessmentMethod().equals("考察")) { System.out.println(course.getCourseName() + " : course type & access mode mismatch"); continue; } if (RepetitiveCourses(course,courses)) continue; courses.add(course); } else if (nextLine.length == 5) { if (Integer.parseInt(nextLine[3]) > 100 || Integer.parseInt(nextLine[3]) < 0 || Integer.parseInt(nextLine[4]) > 100 || Integer.parseInt(nextLine[4]) < 0) { System.out.println("wrong format"); continue; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new ExaminationResults(Integer.parseInt(nextLine[3]), Integer.parseInt(nextLine[4])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { courseSelections.add(new CourseSelection(course, student, score)); } } } } else if (nextLine.length == 4) { if (Integer.parseInt(nextLine[3]) > 100 || Integer.parseInt(nextLine[3]) < 0) { System.out.println("wrong format"); continue; } Student student = new Student(nextLine[0], nextLine[1]); Iterator<Student> iterator = students.iterator(); while (iterator.hasNext()) { Student stu = iterator.next(); if (stu.getStudentNumber().equals(student.getStudentNumber())) { iterator.remove(); } } students.add(student); if (isCourseExist(nextLine[2], courses, nextLine.length, student)) { Score score = new AssessmentResults(Integer.parseInt(nextLine[3])); for (Course course:courses ) { if (course.getCourseName().equals(nextLine[2])) { CourseSelection courseSelection = new CourseSelection(course, student, score); if (RepetitiveScores(courseSelection,courseSelections)) continue; courseSelections.add(courseSelection); } } } } } } 将以上代码改进一下
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值