HDU1074:Doing Homework(状压DP)
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15868 Accepted Submission(s): 7718
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.
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
Computer
Math
English
3
Computer
English
Math
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.
题意:
有T测试用例,每个用例有个N门作业,每门作业有截止时间和完成所需的时间,默认时间为0,在截止时间过后每拖一天就会扣一分,求做作业的顺序让扣的分最少,如果有多个答案则输出字典序最小的答案(注意!),且输入的课程名称按字母顺序递增。
思路:
因为题目只有十五门课程,可以暴力状压DP,枚举1~1<<n的所有状态。具体见下代码。(不要在意头文件)
#define _CRT_SECURE_NO_DepRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <cctype>
#include <iterator>
#include <vector>
#include <cstring>
#include <cassert>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ll long long
#define INF 0x3f3f3f3f
#define ld long double
const ld pi = acos(-.0L), eps = 1e-;
int qx[] = { ,,,- }, qy[] = { ,-,, }, qxx[] = { ,- }, qyy[] = { ,- };
using namespace std;
struct node
{
string name;
int need, end;
}book[];
struct fate
{
int last, now, score, day;
}dp[<<];
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
for (int i = ; i < n; i++)
{
cin >> book[i].name >> book[i].end >> book[i].need;
}
memset(dp, , sizeof(dp));
for (int i = ; i < << n; i++)//枚举做作业的每种情况
{
dp[i].score = INF;
for (int f = n - ; f >= ; f--)
{
int s = << f;//让1代表选择做哪一门课
if (s & i)//判断该情况是否有做这一门课
{
int last = i - s;//last为没做s这门课的时候
int score = max(dp[last].day + book[f].need - book[f].end, );//计算做s这门课的时候的分数,分数不能小于0
if (score + dp[last].score < dp[i].score)//如果做s这门课得分更少则做这么课
{
dp[i].score = score + dp[last].score;
dp[i].day = dp[last].day + book[f].need;
dp[i].last = last;//记录得分最少的上一种情况,记录路径
dp[i].now = f;//记录此时选做哪一门课,注意是倒着选的
}
}
}
}
cout << dp[( << n) - ].score << endl;//(1 << n) - 1的情况即为做全部作业的时候,DP的思想。
int out = ( << n) - ;
stack<string>output;
while (out)
{
output.push(book[dp[out].now].name);//因为是倒着记录的所以应用stack记录然后输出
out = dp[out].last;
}
while (!output.empty())
{
cout << output.top() << endl;
output.pop();
}
}
return ;
}
HDU1074:Doing Homework(状压DP)的更多相关文章
- HDU1074 Doing Homework —— 状压DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (J ...
- hdu_1074_Doing Homework(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...
- HDU 1074 Doing Homework 状压dp(第一道入门题)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework (状压DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework 状压DP
由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...
- kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
- Doing Homework HDU - 1074 (状压dp)
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every ...
- HDU 1074:Doing Homework(状压DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Problem Description Ignatius has just ...
随机推荐
- ADO.NET连接数据库DBHelper工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- TLS/SSL 梳理
数据加密通篇都是为了防止第三方的劫持伪造,保证连接安全, 毫无遮掩的明文传输只有民风淳朴的时候才是安全的. 先是一些基础的内容: 对称加密 最开始为了对数据进行加密,使用的是对称加密算法,即双方协商好 ...
- [置顶] Django-rest framework框架
出师表 先帝创业未半而中道崩殂,今天下三分,益州疲弊此诚危急存亡之秋也.然侍卫之臣不懈于内忠志之士忘身于外者盖追先帝之殊遇,欲报之于陛下也.诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻 ...
- (转)ARM协处理器主要用途 及其 指令CDP LDC STC MCR MRC介绍
原文地址:http://zqwt.012.blog.163.com/blog/static/120446842010111610612200/ ARM 微处理器可支持多达 16 个协处理器,用于各种协 ...
- 使用 VSCode 在 Mac 上配置 C/C++ 调试环境
Background VSCode是微软开发的一款开源代码编辑器,具有可拓展性强,多语言支持,跨平台等优点,在不同的个性化配置下几乎可以用作所有的轻量级开发.我在初学C的时候也使用的是类似于Xcode ...
- Java 锁详解(转)
转自 https://www.cnblogs.com/jyroy/p/11365935.html Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率.本文旨在对锁相 ...
- 【TIJ4】第四章全部习题
第四章 没啥好说的...... 4.1 package ex0401; //[4.1]写一个程序打印从1到100的值 public class PrintOneToHundred { public s ...
- MySQL数据备份及还原(一)
关于删库跑路的事故现在已经屡见不鲜了,数据备份的必要性是企业数据管理极其重要的一项工作.关于数据备份.恢复也有很多场景及方法,本系列也会将主要的几种工具通过案例进行演示. 本系列将从逻辑备份及恢复开始 ...
- C++ 深拷贝和浅拷贝详解
前言 在对象拷贝过程中,如果没有自定义拷贝构造函数,系统会提供一个缺省的拷贝构造函数,缺省的拷贝构造函数对于基本类型的成员变量,按字节复制,对于类类型成员变量,调用其相应类型的拷贝构造函数. 位拷贝( ...
- The instance of entity type 'manager' cannot be tracked because another instance with the same key value for {'id'} is already being tracked. When attaching existing entities, ensure that only one ent
最近在用ASP.NET CORE时遇到一些问题,现记录下: 出现上述错误,即在更新实体数据时出现的错误 services.AddDbContext<StoreContext>(c => ...