Doing Homework HDU, doinghomeworkhdu
Doing Homework HDU-1074
Question:
There are n jobs, each of which has a deadline and a required time for completion. If the time for completing a job exceeds the deadline, the score of the deadline is deducted. Find the order in which jobs are completed with the minimum deduction points.
Method:Pressure dp. Ans [S] indicates the minimum deduction for completing the job of set S (set S is represented by a number ). Pre [S] and pre2 [S] indicate the task sequence number completed by the previous State S and the previous state of S respectively.
First of all, you should realize that the total time is the same no matter in which order you complete the job for a set. (I have never found out that this leads to no idea)
From small to large enumeration S (in this case, the set guarantee obtained by removing a job from each set has been enumerated, because the number of its subset must be smaller than S ).
$ Ans [S] = min \ {ans [S-p] + max (sumt [S]-d [p], 0) \} $ (p indicates any job in the S set)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 typedef long long LL; 7 char name[16][110]; 8 LL d[16],c[16],ans[70000]; 9 LL pre[70000],pre2[70000];10 LL T,n,sumt;11 vector<LL> vec;12 int main()13 {14 LL i,j,t;15 scanf("%lld",&T);16 while(T--)17 {18 memset(name,0,sizeof(name));19 memset(ans,0x3f,sizeof(ans));20 memset(pre,0,sizeof(pre));21 memset(pre2,0,sizeof(pre2));22 scanf("%lld",&n);23 for(i=1;i<=n;i++)24 scanf("%s%lld%lld",name[i],&d[i],&c[i]);25 ans[0]=0;26 for(i=1;i<(1<<n);i++)27 {28 sumt=0;29 for(j=1;j<=n;j++)30 if(i&(1<<(j-1)))31 sumt+=c[j];32 for(j=1;j<=n;j++)33 if(i&(1<<(j-1)))34 {35 t=ans[i^(1<<(j-1))]+max(sumt-d[j],0LL);36 if(t<=ans[i])37 {38 ans[i]=t;39 pre[i]=j;40 pre2[i]=i^(1<<(j-1));41 }42 }43 }44 printf("%lld\n",ans[(1<<n)-1]);45 vec.clear();46 for(i=(1<<n)-1;i!=0;i=pre2[i])47 vec.push_back(pre[i]);48 for(i=vec.size()-1;i>=0;i--)49 printf("%s\n",name[vec[i]]);50 }51 return 0;52 }
Question
One bare state compression ~
There are N jobs in total. The three data items are the job name, expiration time, and the number of days required for completion. if the job is completed for more than one day, one point is deducted. in what order can I complete my assignment to minimize the score. if the same score name is in lexicographically ordered order.
State transition equation: dp [I] = min (dp [j] + hw [k]-Hwlast [k]) + hw [k]; j indicates that the k job is removed from I, and hw [k] indicates that the current job takes several days to complete, hwlast indicates the duration of the current job. If (dp [j] + hw [k]
Source code:
#include <myhead.h>const int N=16;const int M=110;const int NUM=(1<<N);struct HomeWork {char name[M];int last;int time;};struct Dp {int timer;int scr;int last;int i;void init() {this->timer=0;this->scr=INT_MAX;this->last=0;this->i=0;}};int n,num;HomeWork hw[N];Dp dp[NUM];void init() {scanf("%d",&n);num=(1<<n);dp[0].init();dp[0].scr=0;for(int i=0;i<n;++i) {scanf("%s%d%d",hw[i].name,&hw[i].last,&hw[i].time);}}void work() {for(int i=1;i<num;++i) {dp[i].init();for(int j=n-1;j>=0;--j) {int s=(1<<j);if(s&i) {int past=i-s;int t=dp[past].timer+hw[j].time-hw[j].last;checkmax(t,0);t+=dp[past].scr;if(t<dp[i].scr) {dp[i].scr=t;dp[i].i=j;dp[i].last=past;dp[i].timer=dp[past].timer+hw[j].time;}}}}stack<int> s;int star=num-1;while(star) {s.push(dp[star].i);star=dp[star].last;}printf("%d\n",dp[num-1].scr);while(!s.empty()) {printf("%s\n",hw[s.top()].name);s.pop();}}int main() {int t;scanf("%d",&t);while(t--) {init();work();}return 0;}