• D


    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.

    InputThe 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. 
    OutputFor 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.
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<stack>
    #include<queue>
    using namespace std;
    #define MAXN 16
    #define INF 0x3f3f3f3f
    struct node
    {
        string str;//作业名称
        int deadline,need;//截止日和所需时间
    }a[MAXN];
    struct DP
    {
        int now,sum,pos,next;//分别是当前状态下的 时间,所扣分数,作业的下标,做的上一个作业的下标
    }dp[1<<MAXN];
    
    void put_ans(int x)//递归输出答案
    {
        if(dp[x].next!=-1)
        {
            put_ans(dp[x].next);
            cout<<a[dp[x].pos].str<<endl;
        }
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
                cin>>a[i].str>>a[i].deadline>>a[i].need;
            dp[0].sum = dp[0].now = 0;
            dp[0].next = dp[0].pos = -1;//递归停止条件
            for(int i=1;i<(1<<n);i++)//从0000...1 一直到 11111...1 在这里1表示作业已经完成,0表示未完成
            {
                dp[i].sum = INF;
                for(int j=0;j<n;j++)
                {
                    if(i&(1<<j))//如果当前状态下第j个为1,那么可以由第j位为零的情况转化来
                    {
                        int k = i - (1<<j);
                        int v = dp[k].now + a[j].need - a[j].deadline;//这是在第j位为0(任务j没做)的情况下达到当前状态i所扣分
                        v = max(v,0);
                        if(dp[k].sum+v<=dp[i].sum)//取最优解,在这里必须是小于等于——因为我们要保证字典序最小,所以应该尽量让字典序大的作业(j下标大的)后加入,比如110和101两种状态都能到达111而且都是最优解,那么我们应该选择101,因为在这种情况下是先完成的任务1(第1位为1)
                        {
                            dp[i].sum = dp[k].sum +v;
                            dp[i].now = dp[k].now + a[j].need;
                            dp[i].next = k;
                            dp[i].pos = j;
                        }
                    }
                }
            }
            printf("%d
    ", dp[(1<<n)-1].sum);
            put_ans((1<<n)-1);
        }
        return 0;
    }
  • 相关阅读:
    字符串类型
    数据类型之整型
    数据类型
    两个版本的区别
    变量
    DHCP
    MySQL数据库编译及入门
    NFS网络文件系统
    Rsync 数据同步
    互联网数据分享平台
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6617659.html
Copyright © 2020-2023  润新知