D - 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.

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\n", dp[(1<<n)-1].sum);
        put_ans((1<<n)-1);
    }
    return 0;
}
时间: 2024-10-17 01:50:32

D - Doing Homework的相关文章

HDU 1789 Doing Homework again(贪心)

Doing Homework again Problem 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 deadlin

uva 1489 - Math teacher&#39;s homework(数位dp)

题目链接:uva 1489 - Math teacher's homework 题目大意:给定n,k,以及序列m1,m2,-,mn, 要求找到一个长度为n的序列,满足0<=xi<=mi, 并且x1XORx2XOR-XORxn=k 解题思路:数位dp,在网上看了别人的代码,高大上... 假设有二进制数 k : 00001xxxx mi:0001xxxxx, 那么对于xi即可以满足任意的x1XORx2XOR-XORxi?1XORxi+1XOR-XORxn,根据这一点进行数位dp. dp[i][j]

HDU 1074 Doing Homework 状压DP

Problem 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 r

HDU 1789 Doing Homework again

在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案直接交换就可以了. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; struct HomeWork { int de

[2016-03-28][HDU][1074][Doing Homework]

时间:2016-03-28 18:46:36 星期一 题目编号:[2016-03-28][HDU][1074][Doing Homework] 题目大意:给定n门科作业的期限时间和完成耗时,每科每超过一天就扣一份,求最少扣分数 分析:n只有15,二进制枚举,状态压缩,枚举每种科目完成的状态,更新下一个状态,求最小值 #include <cstring> #include <cstdio> using namespace std; const int maxstu = 1 <&

Homework (7th,Mar.)

第一题: 1 /* 2 定义一个水果类(fruit),水果类中有 3 属性:颜色(color).价格(price).重量(weigth), 4 再定义一个<测试类>, 5 创建一个苹果(apple)的对象, 颜色是"红色",价格是5.5,重量10g. 6 然后再创建一个香蕉(banana)的对象,颜色是"黄色",价格是4.2,重量5g. 7 最后输出:苹果的颜色.价格.重量. 8 香蕉的颜色.价格.重量. 9 */ 10 package Homework

hdu 5298 Solid Geometry Homework(几何)

题目链接:hdu 5298 Solid Geometry Homework 每个圈或者是平面将划分出两个区域,每次把一边区域取反即可.最后判断一下是否满足. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int maxn = 3000; typedef long long ll; struct Poi

HDU 1074 Doing Homework(状压dp)

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6299    Accepted Submission(s): 2708 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a l

HDU1789Doing Homework again(贪婪)

HDU1789Doing Homework again(贪心) 题目链接 题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数.问如何安排能够使得扣分最少. 解题思路:贪心,将扣分多的作业排在前面,扣分同样的依照最后期限前的排前面,然后用一个数组来表示第i天是否有安排.每次都将第i个作业放到它的最后期限的那天完毕,但假设这一天被占了,那么就仅仅能往前移动,找空暇的天.假设一直找到了0.那么说明这个作业是无法按时完毕了,就加上分数.假设某项作业完毕的最后期限比n还大,那么这个作业一定是能

Doing Homework 状态压缩DP

Doing Homework 题目抽象:给出n个task的name,deadline,need.  每个任务的罚时penalty=finish-deadline;   task不可以同时做.问按怎样的顺序做使得penalty最小.同时输出顺序.如果有多个满足条件的顺序,按字典序输出. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #inc