hdu 1074 Doing Homework(状压)

题意:有n个作业,有截止日期和需要完成的时间,超出期限一天就扣一分。问最少扣多少分

思路:因为最多15门课,而15!太大了,所以进行状压,而2^15只有3w多,所以是可以进行的,状压后,把每种情况都枚举一下,然后同时进行时间和分数的dp,最后把分数都转移到bit-1,虽然我懂了是怎样状压dp的,但我不知道是怎么打印出来的,只知道是类似于用并查集维护一样,我打印了并查集数据也没看出来,还是不行呐

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=(1<<15)+7;
const int inf=0x3f3f3f3f;
int n,dp[maxn],t[maxn],dea[maxn],fin[maxn],fa[maxn];
char s[20][15];

void solve(int x)
{
    if(!x)return ;
    solve(x-(1<<fa[x]));
    printf("%s\n",s[fa[x]]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s%d%d",&s[i],&dea[i],&fin[i]);
        }
        int bit=1<<n;
        for(int i=1;i<bit;i++){
            dp[i]=inf;
            for(int j=n-1;j>=0;j--){
                int temp=1<<j;
                if(!(i&temp))continue;
                int ans=t[i-temp]+fin[j]-dea[j];
                if(ans<0) ans=0;
                if(dp[i]>dp[i-temp]+ans){
                    dp[i]=dp[i-temp]+ans;
                    t[i]=t[i-temp]+fin[j];
                    fa[i]=j;
                }
            }
        }
//        for(int i=0;i<bit;i++){
//            printf("fa[%d] == %d\n",i,fa[i]);
//        }
        printf("%d\n",dp[bit-1]);
        solve(bit-1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lalalatianlalu/p/8452170.html

时间: 2024-11-02 22:04:07

hdu 1074 Doing Homework(状压)的相关文章

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

[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 <&

HDU 4924 Football Manager(状压DP)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4924 题意 : n(<=20)个人选出11个人作为首发组成a-b-c阵容,每个人都有自己擅长的位置,并且每个人和其他人会有单向的厌恶和喜欢关系,每个人对于自己擅长的位置都有两个值CA和PA,有喜欢或者厌恶关系的两个人在一起也会影响整个首发的CA总值,要求选出一套阵容使得CA最大,或者CA一样的情况下PA最大. 思路 : 状压搞,dp[s]s的二进制表示20个人中选了那几个人,然后规定选进来的顺序

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

HDU 4909 String 统计+状压

因为连续异或满足区间减法性质,所以可以状压之后用异或来判断是否为符合条件的单词并且存储次数 一开始用map,一直超时.虽然直接用开1<<26的数组内存存的下,但是memset的时间肯定会超,但是只要在每次循环之后把加过的值减掉就可以绕过memset了. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits>

HDU 1074 Doing Homework DP 状压DP

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目描述: 给你所有课程的截止时间和工作时长, 一次只能做一种作业, 问最少罚时几天 N <= 15 解题思路: 由于N很小, 所以第一反应就是状压DP, 我们可以用一个15位二进制数来表示各个课程做完还是没做完, 然后从 S 从 1 到 1 << N 枚举 i 从 1 到 N 枚举, 如果S & (1<<i) 有效则说明i 属于情况 S, 这样我们从上一步S -

状压DP [HDU 1074] Doing Homework

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