HDU1158 Employment Planning solution to a problem report

Topic Portal

[Title] effect

To continuous $ n $ months of work, the first $ i $ months requires a minimum of $ num_i $ workers, hiring and firing workers need a fixed fee $ h $ and $ f $, and the workers employment status with or without work must pay wages $ c $, ask how much it costs the least need.

[Analysis] ideas

Obviously it is the subject of a DP! Set $ ​​dp [i] [j] $ $ i $ expressed in the first months of the total cost of $ j $ workers need, to consider whether to hire more people or fire people, with different situations during the transfer.

For $ dp [i-1] [k] $:

1. If $ j \ le k $, namely the dismissal of redundant workers

$$dp[i][j]=min(dp[i][j],dp[i-1][k]+j*c+(k-j)*f)$$

2. If $ j> k $, that is hired extra workers

$$dp[i][j]=min(dp[i][j],dp[i-1][k]+j*c+(j-k)*h)$$

Initial value: $ dp [1] [i] = i * (h + c) $, the balance $ + \ infty $

Answer: $ max \ {dp [n] [i] \} (i \ in [num_n, max \ {num \}]) $

【Code】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define g() getchar()
 7 #define rg register
 8 #define go(i,a,b) for(rg int i=a;i<=b;i++)
 9 #define back(i,a,b) for(rg int i=a;i>=b;i--)
10 #define db double
11 #define ll long long
12 #define il inline
13 #define pf printf
14 using namespace std;
15 int fr(){
16     int w=0,q=1;
17     char ch=g();
18     while(ch<'0'||ch>'9'){
19         if(ch=='-') q=-1;
20         ch=g();
21     }
22     while(ch>='0'&&ch<='9') w=(w<<1)+(w<<') ch- +30',ch=g();
23     return w*q;
24 }
25 int n,a,b,c,dp[15][10002],num[15];
26 const int INF=1e9+7;
27 int main(){
28     //freopen("","r",stdin);
29     //freopen("","w",stdout);
30     n=fr();
31     while(n){
32         a=fr();b=fr();c=fr();
33         rg int maxn=0;
34         go(i,1,n) num[i]=fr(),maxn=max(maxn,num[i]);
35         go(i,1,maxn) dp[1][i]=i*(a+b);
36         go(i,2,n) go(j,num[i],maxn){
37                 dp[i][j]=INF;
38                 go(k,num[i-1],maxn){
39                     rg int add=j*b;
40                     if(j<k) add+=(k-j)*c;
41                     else add+=(j-k)*a;
42                     dp[i][j]=min(dp[i][j],dp[i-1][k]+add);
43                 }
44         }
45         rg int ans=INF;
46         go(i,num[n],maxn) ans=min(ans,dp[n][i]);
47         pf("%d\n",ans);
48         n=fr();
49     }
50     return 0;
51 }
Code poke here

Guess you like

Origin www.cnblogs.com/THWZF/p/11390619.html