状压dp,HDU1074.Doing Homework

news/2024/7/8 1:19:12

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

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.

2、输入输出

2.1输入

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.

2.2输出

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.

3、原题链接

Problem - 1074 (hdu.edu.cn)


二、解题报告

1、思路分析

从数据量上会往状压dp上想

我们用二进制位表示任务是否完成,那么我们最终状态是确定的

如果有n个任务,那么我们的最终状态就是(1 << n) - 1,我们记为ed

对于ed而言,代表n个任务都已经完成,它可以由n个前驱状态转移而来

假如n = 3,那么ed = 111(2),那么可以由011、101、110三个状态转移,分别代表最后完成的任务为任务1、2、3

那么对于011,101,110而言,同样可以由前驱状态转移

那么我们自顶向下进行状态转移即可

2、复杂度

时间复杂度:O(1<<N) 空间复杂度:O(1<<N)

3、代码详解

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
const int N = 20, inf = 0x3f3f3f3f;
struct state
{
    int pre, id, t, s;
} f[1 << N];
int cost[N], dead[N], n, tot;
string lessons[N];
void solve()
{
    cin >> n, tot = 1 << n, memset(f, 0, sizeof f);
    for (int i = 0; i < n; i++)
        cin >> lessons[i] >> dead[i] >> cost[i];
    for (int i = 1; i < tot; i++)
    {
        f[i].s = inf;
        for (int j = n - 1; j >= 0; j--)
        {
            if (i & (1 << j))
            {
                int last = i - (1 << j);
                int c = max(0, f[last].t + cost[j] - dead[j]);
                if (f[last].s + c < f[i].s)
                    f[i] = {last, j, f[last].t + cost[j], f[last].s + c};
            }
        }
    }
    cout << f[--tot].s << '\n';
    stack<int> s;
    while (f[tot].t)
    {
        s.emplace(f[tot].id), tot = f[tot].pre;
    }
    while (s.size())
        cout << lessons[s.top()] << '\n', s.pop();
}
int main()
{
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
        solve();
    return 0;
}


http://lihuaxi.xjx100.cn/news/2035089.html

相关文章

同一目录使用 `df` 和 `du` 命令查看的磁盘占用情况不一致

在排查软件应用卡顿时&#xff0c;通常会检查服务器的磁盘使用情况。然而有些时候&#xff0c;使用 df 和du 查到的情况不一致。 例如&#xff0c;分别使用 df -h / 和 du -h / 查看 / 的磁盘大小。结果可能差异如下&#xff1a; df -h / 查看结果为197G rootxzbd:~# df -h …

无穷绕八双纽线

目录&#xff09; 前言双纽线双纽线工程化双纽线应用参考文献 前言 今天是初八&#xff0c;在中国某些地方初八有拜财神的习俗&#xff0c;“八”谐音“发”&#xff0c;等同于恭喜发财的“发”&#xff0c;寓意着在新的一年里红红火火发大财&#xff0c;三叔首先祝福各位读者…

【4.2计算机网络】开放互连参考模型

目录 1.OSI七层模型介绍 1.OSI七层模型介绍 例题1. 解析&#xff1a;选B。A选项网桥也不能检测冲突只是能隔离冲突&#xff0c;C选项集线器是多端口中继器&#xff0c;多端口网桥是交换机。 例题二. 解析&#xff1a;选B。A集线器是物理层&#xff0c;C路由器是网络层&#x…

[深度学习] 卷积神经网络“卷“在哪里?

​ &#x1f308; 博客个人主页&#xff1a;Chris在Coding &#x1f3a5; 本文所属专栏&#xff1a;[深度学习] ❤️ 热门学习专栏&#xff1a;[Linux学习] ⏰ 我们仍在旅途 目录 1.卷积的定义 2.卷积的"卷"在哪里 3.什么又是卷积神…

基于Springboot的新能源充电系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的新能源充电系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&a…

【python】深入探索使用Matplotlib中的plt.legend()添加图例

当我们绘制复杂的图表&#xff0c;尤其是包含多个数据系列的图表时&#xff0c;一个清晰、易读的图例是至关重要的。plt.legend()函数是Matplotlib库中用于添加和定制图例的关键工具。在本篇博文中&#xff0c;我们将深入探讨plt.legend()的功能、用法以及如何通过它提升图表的…

【python全栈开发】面向对象进阶

这里写目录标题 成员变量 成员 变量 实例变量&#xff1a;属于对象&#xff0c;每个对象中各自维护自己的数据。 类变量&#xff1a;属于类&#xff0c;可以被所有对象共享&#xff0c;一般用于给对象提供公共数据&#xff08;类似于全局变量&#xff09;。 类变量和实例变量…

乐优商城(六)ElasticSearch搜索二

1.索引库数据导入 之前我们学习了Elasticsearch的基本应用。今天就学以致用&#xff0c;搭建搜索微服务&#xff0c;实现搜索功能。 1.1.创建搜索服务 创建module&#xff1a; Pom文件&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <…