寒假Day40:HDU3576-Elevators in Jiayuan Students' Apartment-dp
简单dp写起来都存在问题,稍微难一点点就凉凉了,况且这题还不是难一点点,
题面:
There are three elevators in the Building B of Jiayuan Students' Apartment in BJTU, which carry a lot of students to go up and down every day. There're 16 floors in the Building B. The students living on the 1F don't need to wait for the elevators, so all the m students want to get to the 2F or higher. Supposed that the capacity of each elevator is C( 1 ≤ C ≤ 13 ). Now there are m( 1 ≤ m ≤ 3*C ) students waiting for upstairs on the first floor, while all the three elevators stop on the first floor. You can arrange the way how they go upstairs, that Nobody will left on the first floor when the elevators are in the process of rising. How many times do the three elevators have to stop at least to carry all the m students to the right floor? Input The first line of input contains a number t, which means there are t cases of the test data. In each case of testing data, the first line contains two integers C and m. In the next line, there are m integers f ( 2 ≤ f ≤ 16 ), indicate the floor a student wants to reach. Output For each case of testing data, the first line is written as "Case n: X", where n is the case number, X is the minimum times the elevators have to stop in the process of rising.
样例:
Sample Input 2 1 3 2 4 16 2 6 9 9 9 6 6 6 Sample Output Case 1: 3 Case 2: 4
题意:
给出c和m,表示三个电梯的容量人数和有m个人,
接下去给出m个人想要到达的楼层,总共16层
问:所有人都到达相应楼层电梯所停的次数最少,求出最小次数。
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<stack> #include<queue> #include<map> using namespace std; int dp[20][20][20][20],a[20]; int main() { int t,tt=1,c,m,x; scanf("%d",&t); while(t--) { memset(a,0,sizeof(a)); memset(dp,-1,sizeof(dp)); scanf("%d %d",&c,&m);//三个电梯的容量、m人要到达的电梯层 for(int i=1; i<=m; i++) { scanf("%d",&x); a[x]++; } dp[1][0][0][0]=0; for(int f=2; f<=16; f++) //共16层 { for(int i=0; i<=c; i++) //枚举容量 { for(int j=0; j<=c; j++) { for(int k=0; k<=c; k++) { if(i+j+k>m||dp[f-1][i][j][k]==-1) continue; for(int p=0; p<=a[f]; p++) { for(int q=0; q<=a[f]; q++) { if(p+q>a[f]) continue; int w=a[f]-p-q; if(i+p>c||j+q>c||k+w>c) continue; int sum=dp[f-1][i][j][k]; if(p>0) sum++; if(q>0) sum++; if(w>0) sum++; int x=dp[f][i+p][j+q][k+w]; if(x==-1||x>sum) dp[f][i+p][j+q][k+w]=sum; } } } } } } int ans=0x3f3f3f3f; for (int i=0; i<=c; i++) { for (int j=0; j<=c; j++) { if(i+j>m) continue; int k=m-i-j; if(dp[16][i][j][k]==-1) continue; ans=min(ans,dp[16][i][j][k]); } } printf("Case %d: %d\n",tt++,ans); } return 0; }