HDU-1074 Doing Homework 状态压缩DP

2024-06-16 16:18
文章标签 dp 压缩 状态 hdu homework 1074

本文主要是介绍HDU-1074 Doing Homework 状态压缩DP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接


题目大意: 有n个作业,每个作业有各自的完成期限与完成所需时间。每个作业每超过完成期限一个单位时间扣1学分,问怎么安排完成作业顺序使得所扣学分最少。


由于n小于等于15 状态少 明显可以用二进制压缩状态枚举排列 要注意的 答案相同要按作业名字典序排。


#include <stdio.h>
#include <string.h>
#include <iostream>
#include<functional>
#include <queue>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 16;
const int inf = 1<<30;
typedef __int64 LL;
int n;
struct node
{string name;int dl,cost;
}sub[maxn];
struct Node
{int Time,score,pre;
}dp[1<<maxn];
void GetDp()
{int tmp,pre;dp[0].score = 0; dp[0].Time = 0; dp[0].pre = -1;for( int s = 1; s < (1<<n); s ++ ){dp[s].score = inf;for( int i = 0; i < n; i ++ ){if( s&(1<<i) ){pre = s-(1<<i);tmp = dp[pre].Time + sub[i].cost - sub[i].dl;if( tmp < 0 )	tmp = 0;if( dp[pre].score + tmp <  dp[s].score ){dp[s].score = dp[pre].score + tmp;dp[s].Time = dp[pre].Time + sub[i].cost;dp[s].pre = i;}else if( dp[pre].score + tmp ==  dp[s].score && sub[i].name > sub[dp[s].pre].name ){dp[s].pre = i;}}}}
}
void Outup( int s )
{if( s == 0 )	return;int preS = s - (1<<dp[s].pre);Outup( preS );cout<<sub[dp[s].pre].name<<endl;
}
int main()
{#ifndef ONLINE_JUDGE  freopen("data.txt","r",stdin);  #endif  int cas;scanf("%d",&cas);while( cas -- ){scanf("%d",&n);for( int i = 0; i < n; i ++ )cin>>sub[i].name>>sub[i].dl>>sub[i].cost;GetDp();printf("%d\n",dp[(1<<n)-1].score);Outup( (1<<n)-1 );}return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include<functional>
#include <queue>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 16;
const int inf = 1<<30;
typedef __int64 LL;
int n;
struct node
{string name;int dl,cost;
}sub[maxn];
struct Node
{int Time,score,pre;
}dp[1<<maxn];
void GetDp()
{int tmp,pre;dp[0].score = 0; dp[0].Time = 0; dp[0].pre = -1;for( int s = 1; s < (1<<n); s ++ ){dp[s].score = inf;for( int i = 0; i < n; i ++ ){if( s&(1<<i) ){pre = s-(1<<i);tmp = dp[pre].Time + sub[i].cost - sub[i].dl;if( tmp < 0 )	tmp = 0;if( dp[pre].score + tmp <  dp[s].score ){//扣分相同,取字典序小的那一个,由于这里j是按从小到达搜索的,默认已是按字典序,不需再处理dp[s].score = dp[pre].score + tmp;dp[s].Time = dp[pre].Time + sub[i].cost;dp[s].pre = i;}}}}
}
void Outup( int s )
{if( s == 0 )	return;int preS = s - (1<<dp[s].pre);Outup( preS );cout<<sub[dp[s].pre].name<<endl;
}
int main()
{#ifndef ONLINE_JUDGE  freopen("data.txt","r",stdin);  #endif  int cas;scanf("%d",&cas);while( cas -- ){scanf("%d",&n);for( int i = n-1; i >= 0; i -- )cin>>sub[i].name>>sub[i].dl>>sub[i].cost;GetDp();printf("%d\n",dp[(1<<n)-1].score);Outup( (1<<n)-1 );}return 0;
}


这篇关于HDU-1074 Doing Homework 状态压缩DP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1066924

相关文章

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr