• Doing Homework


    Doing Homework
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
     

    Input

    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
    Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework). 

    Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier. 
     

    Output

    For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one. 
     

    Sample Input

    2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3
     

    Sample Output

    2 Computer Math English 3 Computer English Math

    Hint

     In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order. 
             
    /*
    题意:给你n门功课,和对应的最后完成的日期,做完这门功课需要的时间,每门功课如果延迟的上交的话,每迟一天,就会扣一分,问怎么样安排
        做功课的顺序才能是的减的分数最少。
    
    初步思路:状压DP,最多15门功课,状态最多也就是2^15,设dp[i]为i状态的时候最少扣的分数状态转移方程为:
    
        dp[i]表示的是i状态扣得最少的分数,算出”加上!!!“第k这节课后所用的总时间time,time-第k节课期限,就是加上第k节课所需要的
        扣的分数,然后下面的的状态转移方程就明白了
    
        dp[i|(1<<k)]=max(dp[i|(1<<k)] ,dp[i]+time)
    
        剩下的就是记录路径了,这个很简单就是用path数组记录一下状态转移的过程,在状态转移的过程中增加的课程就是咱们想要的到的结果
    
    
    错误:眼瞎,path数组开小了,没看出来
    */
    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    using namespace std;
    struct node{
        char name[105];
        int end_time;
        int use_time;
    }course[16];
    int dp[(1<<16)];
    int path[(1<<16)];//用来记录路径,path[i]=j,表示i状态是由j状态转移过来的
    int t,n;
    void init(){
        memset(dp,INF,sizeof dp);
        memset(path,-1,sizeof path);
    }
    void print(int k){
        if(k==0) return ;//直到尽头,就是没有前一个状态了
    
        for(int i=0;i<n;i++){
            //条件:k状态有这一位,并且去除这一位之后 是 转移到 k 的状态
            if(( k&(1<<i) )!=0 && path[k] == ( k-(1<<i) ) ){//说明 k 状态和 k|(1<<i) 状态存在转移的关系
                // cout<<k<<" "<<( k^i )<<endl;
                print( path[k] ) ;
                printf("%s
    ",course[i].name);
                break;
            }
        }
    }
    int main(){
        // freopen("in.txt","r",stdin);
        scanf("%d
    ",&t);
        while(t--){
            init();
            scanf("%d
    ",&n);
            for(int i=0;i<n;i++){
                scanf("%s %d %d
    ",course[i].name,&course[i].end_time,&course[i].use_time);
                // cout<<course[i].name<<" "<<course[i].end_time<<" "<<course[i].use_time<<endl;
            }//处理输入
    
            // for(int i=0;i<n;i++){
            //     cout<<course[i].name<<" "<<course[i].end_time<<" "<<course[i].use_time<<endl;
            // }
    
            int tol=(1<<n);//最多的状态为tol-1;
            // cout<<tol<<endl;
            dp[0]=0;//重要的初始化,什么课也不选的话就是不用扣分
            for(int i=0;i<tol;i++){//枚举状态
                for(int j=n-1;j>=0;j--){//枚举没门功课加还是不加
                    if(( i&(1<<j) )==0){//如果这门功课还没选的话
                        int time=0;//记录使用的时间
                        for(int k=0;k<n;k++){//计算时间
                            if(( i&(1<<k) )){//i状态下如果第k门功课选了就记录下他的时间
                                time+=course[k].use_time;
                            }
                        }
                        time+=course[j].use_time;
                        time=time-course[j].end_time;
                        if(time<0) time=0;//因为不可能出现扣出来负数的情况
                        // cout<<time<<endl;
                        if(dp[i|(1<<j)]>dp[i]+time){
                            dp[i|(1<<j)]=dp[i]+time;//更新加上第j门功课的状态
                            //记录一下
                            path[i|(1<<j)]=i;
                            // cout<<(i|(1<<j))<<" "<<i<<endl;
                        }
                    }
                }
            }
            printf("%d
    ",dp[tol-1]);
            // cout<<"**********"<<endl;
            print(tol-1);
            // cout<<"**********"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    [转]如何从无到有建立推荐系统
    sql语句查询重复值
    推荐系统开发中十个关键点整理
    mongodb中的副本集搭建实践
    Unicode对象
    5W1H
    Python中实现switch分支结构
    数据结构-跳跃表
    redis入门笔记(3)
    redis入门笔记(2)
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6550902.html
Copyright © 2020-2023  润新知