zoukankan      html  css  js  c++  java
  • 状态压缩DP,附上例题(HDU

    状态压缩DP:本生有很多状态,然后压缩成一种状态。

    很多时候都会使用位运算

    Doing Homework

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10077    Accepted Submission(s): 4832


    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 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种作业,将作业做完,损失的分数最小是多少,并将作业按顺序输出

    用二进制的方法表示作业的完成情况,比如4个作业完成情况是1010,1代表完成,0代表没有完成,所以作业的总完成量为2.

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 int const INF = 0x3f3f3f3f;
     8 int const MAX = (1<<15) + 10;
     9 int pre[MAX],dp[MAX], t[MAX];
    10 char s[20][110];
    11 
    12 void print(int n) {//存储时候从后往前,所以输出时,先递归进行输出,从后往前输出
    13     if(n == 0) return ;
    14     print(n-(1 << pre[n]));
    15     printf("%s
    ",s[pre[n]]);
    16 }
    17 int main()
    18 {
    19     int T,n;
    20     int dl[20],dr[20];
    21     while(scanf("%d",&T) != EOF) {
    22         while(T--){
    23             scanf("%d",&n);
    24             for(int i = 0;i < n; i++)
    25                 scanf("%s%d%d",s[i],&dl[i],&dr[i]);
    26             int w = 1<<n;
    27             for(int i = 1; i < w; i++) {//从1种到1<<n种情况进行枚举
    28                 dp[i] = INF;
    29                 for(int j = n-1 ; j >=0 ;j--) {//用二进制表示选用第几个的情况
    30                     int cnt = 1 << j;
    31                     if( !(i&cnt))continue;//没有选用第j个
    32                     int score = t[i - cnt] - dl[j] + dr[j];
    33                     if(score < 0) score = 0;//分数小于0说明没有超出时限
    34                     if(dp[i] > dp[i-cnt] + score) {
    35                         dp[i] = dp[i-cnt] +score;
    36                         t[i] = t[i-cnt] + dr[j];
    37                         pre[i] = j;
    38                     }
    39                 }
    40             }
    41             printf("%d
    ",dp[w-1]);
    42             print(w-1);
    43         }
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    Max Sum of Max-K-sub-sequence(单调队列)
    Matrix Swapping II(求矩阵最大面积,dp)
    重温世界杯(贪心)
    Pie(求最小身高差,dp)
    Matrix(多线程dp)
    Python 实现自动导入缺失的库
    分布式系统session一致性解决方案
    数据结构 【链表】
    【数字图像处理】gamma变换
    【数字图像处理】顶帽变换和底帽变换
  • 原文地址:https://www.cnblogs.com/creativepower/p/7498596.html
Copyright © 2011-2022 走看看