Doing Homework (状态DP&&位运算)

本篇介绍了一个算法问题:如何为刚参加完ACM/ICPC比赛的学生Ignatius合理安排大量课后作业的完成顺序,以最小化因延期提交而被扣分的情况。文章提供了一种解决方案,通过动态规划来确定最佳作业完成顺序。

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<stdio.h>
#include<string.h>
#include<math.h>
#include<stack>
typedef long long LL;
using namespace std;
const LL inf=1000000000;
int t;
int n;
struct node1
{
    char name[105];
    int d,c;
}sub[20];

struct node2
{
    LL time,cost,pre,now;
}dp[1<<16];

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        dp[1].pre=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s%d%d",&sub[i].name,&sub[i].d,&sub[i].c);
        }
        for(int i=1;i<(1<<n);i++){
            dp[i].cost=inf;
            for(int j=n-1;j>=0;j--){
                if(i&(1<<j)){//是否存在该状态
                    LL ii=(i&(~(1<<j)));//当这门课完成之前的状态(也就是把i状态(2进制)的第j个位置上的数变为0)
                    LL s=dp[ii].time+sub[j].c-sub[j].d;
                    if(s<0)
                        s=0;
                    if(s+dp[ii].cost<dp[i].cost){
                        dp[i].time=dp[ii].time+sub[j].c;
                        dp[i].pre=ii;
                        dp[i].now=j;
                        dp[i].cost=s+dp[ii].cost;
                    }
                }
            }
        }
        stack<LL>st;
        printf("%lld\n",dp[(1<<n)-1].cost);
        LL a=((1<<n)-1);//最终状态是有n个1的状态
        while(a){
            st.push(dp[a].now);
            a=dp[a].pre;
        }
        while(!st.empty()){
            printf("%s\n",sub[st.top()].name);
            st.pop();
        }
    }
}

逻辑运算符 AND(`&&`)在编程中用于判断多个条件是否同时成立。当使用 `&&` 连接多个条件时,只有所有条件都为 `true` 时,整个表达式的结果才会是 `true`;只要有一个条件为 `false`,整个表达式的结果就会是 `false`。 ### 使用方法 - **基本语法**:`condition1 && condition2 && condition3...` - **返回值**:返回布尔值 `true` 或 `false`,具体取决于操作数的条件是否都满足。 - **短路特性**:`&&` 具有短路求值的特性。如果第一个条件为 `false`,后续的条件将不再计算,因为整体结果已经可以确定为 `false`[^1]。 ### 示例代码 ```javascript let a = 2; let b = 5; let c = 10; let d = 0; // 使用 && 运算符判断多个条件是否同时成立 console.log(a < b && a > c); // false,因为 a > c 不成立 console.log(a < b && a < c && a > d && c > b); // true,所有条件都成立 ``` ### 与其他运算符的区别 - **与 `||` 的区别**:`&&` 要求所有条件都为 `true`,而 `||` 只需要至少一个条件为 `true`。 - **与 `&` 的区别**:`&&` 是逻辑与,用于布尔值之间的运算;`&` 是按位与,用于二进制位的运算。此外,`&&` 具有短路特性,而 `&` 会计算所有条件[^2]。 ### 应用场景 - **条件判断**:在 `if`、`while`、`for` 等控制流语句中,`&&` 常用于组合多个条件。 - **避免空值错误**:通过 `&&` 可以在访问对象属性之前检查对象是否为 `null` 或 `undefined`,例如: ```javascript let user = { name: "Alice" }; console.log(user && user.name); // 输出 "Alice" let guest = null; console.log(guest && guest.name); // 输出 null,避免了错误 ``` ### 优先级与结合性 - **优先级**:`&&` 的优先级低于比较运算符(如 `>`、`<`、`==` 等),但高于赋值运算符(如 `=`、`+=` 等)[^3]。 - **结合性**:`&&` 是从左到右结合的,即先计算左边的表达式,再计算右边的表达式。 ### 短路求值的优势 - **性能优化**:由于 `&&` 在遇到第一个 `false` 条件时会停止计算,因此可以用来优化性能。例如,可以将耗时较长的条件放在 `&&` 的最后,以确保只有在其他条件都满足时才会执行。 - **安全检查**:`&&` 可以用来确保某些操作仅在特定条件下执行。例如: ```javascript function doSomething() { console.log("Doing something..."); } let flag = true; flag && doSomething(); // 如果 flag 为 true,则调用 doSomething() ``` ### 总结 逻辑运算符 `&&` 是编程中非常重要的工具,它不仅用于判断多个条件是否同时成立,还因其短路特性而在性能优化和安全性检查中发挥重要作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值