寒假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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」