zoukankan      html  css  js  c++  java
  • 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 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
    ***************************************************************************************************************************
    解释看代码中
    ***************************************************************************************************************************
     1 /*
     2 状态压缩枚举状态数,
     3 适合记录状态,
     4 方便在不打乱次序的状态下,
     5 记录最优的状态。
     6 值得学习。
     7 */
     8 #include<iostream>
     9 #include<string>
    10 #include<cstring>
    11 #include<cmath>
    12 #include<cstdio>
    13 #include<vector>
    14 #define inf 0x7fffffff
    15 using namespace std;
    16 int n,m,i,j,k;
    17 struct node
    18 {
    19     int d,c;
    20     char ch[101];
    21 }q[1001];
    22 struct stx
    23 {
    24     int now,pre,time,rs;
    25 }dp[1<<15];
    26 void solve()
    27 {
    28     int it,jt,kt;
    29     for(it=1;it<=m;it++)
    30      dp[it].rs=inf;
    31      dp[0].time=0;
    32     for(it=0;it<=m;it++)
    33      for(jt=0;jt<n;jt++)//二进制 ,枚举每一种状态
    34      {
    35        if(!(it&(1<<jt)))//确保不存在重复
    36        {
    37            int sta=(it|(1<<jt));//记录状态
    38            int time=dp[it].time+q[jt].c;
    39            int rs=max(0,time-q[jt].d);//记录罚时
    40            rs+=dp[it].rs;
    41            if(dp[sta].rs>rs)//状态转移
    42            {
    43                dp[sta].time=time;
    44                dp[sta].rs=rs;
    45                dp[sta].now=jt;
    46                dp[sta].pre=it;
    47            }
    48 
    49        }
    50      }
    51 }
    52 void output(int x)//递归的输出记录的结果
    53 {
    54     if(x==0)return;
    55     output(dp[x].pre);
    56     printf("%s
    ",q[dp[x].now].ch);
    57 }
    58 int main()
    59 {
    60     int cas;
    61     scanf("%d",&cas);
    62     while(cas--)
    63     {
    64         scanf("%d",&n);
    65         m=(1<<n)-1;//算出状态数
    66         for(i=0;i<n;i++)
    67         {
    68             scanf("%s %d %d",q[i].ch,&q[i].d,&q[i].c);
    69         }
    70         solve();
    71         printf("%d
    ",dp[m].rs);//输出最优结果
    72         output(m);
    73     }
    74     return 0;
    75 }
    View Code
  • 相关阅读:
    【原创】Apache ab结果参数详解
    【转载】QPS,用户平均等待时间,服务器平均请求处理时间
    【原创】Apache ab测试时出现:apr_socket_recv "connection reset by peer" 104
    【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp
    oracle递归查询
    http1.0和1.1的区别
    here with you
    spring杂碎
    西海情歌
    //随机生成 10到20条数据 数据包含 用户名(5-10位的字母) 性别 年龄(1-100岁)
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3416081.html
Copyright © 2011-2022 走看看