博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1074 Doing Homework ( 状态压缩 )
阅读量:5147 次
发布时间:2019-06-13

本文共 3909 字,大约阅读时间需要 13 分钟。

Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10297    Accepted Submission(s): 4963


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
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.

123

 【状态压缩】

正好学习一下 状态压缩;

状压 用的 最多的是 &  |  << >>  运算;  对于 n <  一般小于20 (2^20 已经不小了  一般15,10)的 范围内;   我们把某个状态 用 0/1 来表示,   这样就有 2^n 个状态

然后枚举状态; 在当前状态下, 判断某个情况是否是符合  符合的话进行下一步操作;

四种运算:  (搬过来的)

1.’&’符号,x&y,会将两个十进制数在二进制下进行与运算,然后返回其十进制下的值。例如3(11)&2(10)=2(10)。

2.’|’符号,x|y,会将两个十进制数在二进制下进行或运算,然后返回其十进制下的值。例如3(11)|2(10)=3(11)。

3.’^’符号,x^y,会将两个十进制数在二进制下进行异或运算,然后返回其十进制下的值。例如3(11)^2(10)=1(01)。

4.’<<’符号,左移操作,x<<2,将x在二进制下的每一位向左移动两位,最右边用0填充,x<<2相当于让x乘以4。相应的,’>>’是右移操作,x>>1相当于给x/2,去掉x二进制下的最有一位。

以及应用;

1.判断一个数字x二进制下第i位是不是等于1。

方法:if ( ( ( 1 << ( i - 1 ) ) & x ) > 0)

将1左移i-1位,相当于制造了一个只有第i位上是1,其他位上都是0的二进制数。然后与x做与运算,如果结果>0,说明x第i位上是1,反之则是0。

2.将一个数字x二进制下第i位更改成1。

方法:x = x | ( 1<<(i-1) )

证明方法与1类似,此处不再重复证明。

3.把一个数字二进制下最靠右的第一个1去掉。

方法:x=x&(x-1)

应用用起来最多;

【HDU 1074】

按字典序,  那么就要记录 路径,   这里用stack   pre记录前驱  记录;

【代码】

//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)#define S1(n) scanf("%d",&n)#define SL1(n) scanf("%I64d",&n)#define S2(n,m) scanf("%d%d",&n,&m)#define SL2(n,m) scanf("%I64d%I64d",&n,&m)#define Pr(n) printf("%d\n",n)#define lson rt << 1, l, mid#define rson rt << 1|1, mid + 1, r#define mem(a,b) memset(a,b,sizeof(a))typedef long long ll;const int INF=0x3f3f3f3f;const ll MOD=1e9+7;const int MAXN=1e5+5;const int N=16;ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x);x=(x*x);}return res;}using namespace std;struct node{ string name; int cost,dead;}a[100];struct nnode{ int time,pre,score,now;}dp[1<
>T; int n; while(T--) { scanf("%d",&n); for(int i=0;i
>a[i].name>>a[i].dead>>a[i].cost; } mem(dp,0); int bit= 1<
=0;j--) // 枚举 全部作业 题意, 做完某一个 就放在后面, 那么看状态, j前面的是否能够到达j { int te= 1<
Q; int te= bit-1; printf("%d\n",dp[te].score); while(te) { Q.push(dp[te].now); te= dp[te].pre; } while(!Q.empty()) { cout<
<

转载于:https://www.cnblogs.com/sizaif/p/9078437.html

你可能感兴趣的文章
成都Uber优步司机奖励政策(4月1日)
查看>>
MIPI-DSI/CSI协议介绍-转载
查看>>
flex学习记录——加载图片
查看>>
菜菜鸟Zend Framework 2 不完全学习涂鸦(三)-- 例子功能设置
查看>>
git之版本回退
查看>>
解决img标签上下间隙问题
查看>>
RPM usage
查看>>
English
查看>>
echarts点击按钮更换数据(数据长度 条数变化)
查看>>
装系统·折腾记
查看>>
Linux下ps -ef和ps aux的区别及格式详解-转
查看>>
Problem C: 程序改错(递归函数):数字转字符
查看>>
压缩打包
查看>>
用html+css+javascript制作圆形时钟
查看>>
chrome 右键页面无反应
查看>>
jquery-防多店铺购物车结算全选,单选,及删除,价格计算
查看>>
前端 ==> CSS (总结)
查看>>
MSMQ 开发
查看>>
HDU 4897 Little Devil I
查看>>
谁是最好的Coder
查看>>