zoukankan      html  css  js  c++  java
  • HDU 1074-Doing Homework(状态压缩dp)

                                                                                                             Doing Homework       
    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
    题意:主人公有N门功课要做,每门功课做完需要一定的时间,而且每门功课有一个最后期限,如果该门功课延后一天交就得扣一分,而且每做一门功课主人公就一定把它做完为止,不会中途停下来再去做其他的。问怎样安排可使扣的分最少,如果有多组解,输出字典序最小的。
     
    解析:由于N很小,可以考虑dp状态压缩搜索,如果选了第i门功课,则二进制对应的位变为1,直到全部选完,更新最小值。打印时可以根据
    dp[]保存的值去找,比如当前状态为now,第i门功课没选,如果dp[now]==dp[now+(1<<i)]+GetTime(...),则可以选它,再往下搜,直到找到所有的解。
     
    代码如下:
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<iterator>
    #include<utility>
    #include<sstream>
    #include<iostream>
    #include<cmath>
    #include<stack>
    using namespace std;
    const int INF=1000000007;
    const double eps=0.00000001;
    int N,deadline[15],day[15];   //最后期限,完成功课要花的时间
    vector<string> ans_name;
    string name[15];              
    int dp[1<<15];                
    int Get(int id,int pass)
    {
        if(pass+day[id]<=deadline[id])  return 0;   //如果在期限之前完成
        return pass+day[id]-deadline[id];
    }
    void get_ans(int pick,int date)
    {
        if(pick==((1<<N)-1))  return;
    
        for(int i=0;i<N;i++)
        {
            if(pick&(1<<i))  continue;
            if(dp[pick]==dp[pick+(1<<i)]+Get(i,date))   //找到符合条件的
            {
                ans_name.push_back(name[i]);
                get_ans(pick+(1<<i),date+day[i]);
                break;
            }
        }
        return;
    }
    int dfs(int pick,int date)    //pick代表当前已选的状态,date代表现在是第几天
    {
        if(pick==((1<<N)-1))  return dp[pick]=0;    //全部选完
        int& ret=dp[pick];
        if(ret!=-1)  return ret;             //记忆化
        ret=INF;
        for(int i=0;i<N;i++)
        {
            if(pick&(1<<i))  continue;
            ret=min(ret,dfs(pick+(1<<i),date+day[i])+Get(i,date));  //取最大值
        }
        return ret;
    
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cin>>N;
            for(int i=0;i<N;i++)  cin>>name[i]>>deadline[i]>>day[i];
            memset(dp,-1,sizeof(dp));
            int ans=dfs(0,0);   //得到最大值
            printf("%d
    ",ans);
            ans_name.clear();
            get_ans(0,0);        //保存答案
            for(int i=0;i<ans_name.size();i++)  cout<<ans_name[i]<<endl;
        }
        return 0;
    }
    
     
     
     
  • 相关阅读:
    Linux CentOS6环境下MySQL5.1升级至MySQL5.5版本过程
    linux卸载mysql
    springboot2.x+security对明文密码加密
    AJAX模拟登录注册
    Jedis工具类
    java连接Redis
    centOS 6.5 以及7以上的开启端口,以及重启防火墙指令
    Mybatis连接数据库错误he server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one
    Git clone时出现Please make sure you have the correct access rights and the repository exists.问题已解决。
    Spring Cloud-集群配置的eureka
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/4744139.html
Copyright © 2011-2022 走看看