Time Limit : 1000/500ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)数组
Problem Descriptionide
Due to the preparation to the wedding you have to fill up M balloons. There are N volunteers and one device for balloons filling up. Device can fill at most one balloon at a time. Each volunteer can continuously operate the device for 1 minute.
Volunteer number i can fill Ai balloons during one minute and then he or she must have at least Bi minutes of rest.
You are to write a program that computes minimal possible time of filling up M balloons.ui
Input
There are two integers M and N on the first line (1 <= M <= 100, 1 <= N <= 10).
Next N lines contain two integers each: Ai and Bi (0 <= Ai <= 10, 1 <= Bi <= 4).spa
Output
Print one integer T - the minimal number of minutes required to filling M balloons.rest
Sample test(s)code
Input
10 3
4 4
3 4
2 3排序
Output
5ip
题意:N(1<=N<=10)我的一台装置填充M(1<=M<=100)个气球,每一个人一分钟能充Ai个(0<=Ai<=10),休息Bi(1<=Bi<=4)分钟后才能够继续充气球,文填充M个气球最少须要多少时间?
分析:看到题目,第一想法就是排序,先大后小,而后DFS,这样显然错的,样例都过不了,而后又想着暴力写出来,不行,确定会超时,只给了0.5s。
而后无奈看了大佬写的博客,他就直接观察到了Bi的范围,是1到4,这个很小,能够开一个五维数组ans[sum][a][b][c][d]来表示还剩sum个气球,前一分钟是a在充气球,前两分钟是b在充气球,前3分钟是c在充气球,前4分钟是d在吹气球,而后直接记忆化搜索博客
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=11,INF=0x3f3f3f3f; int ans[101][N][N][N][N],A[N],B[N]; int n,m,res; int dfs(int sum,int a,int b,int c,int d) { if(sum<=0) sum=0;///所有充完 if(ans[sum][a][b][c][d]!=INF) return ans[sum][a][b][c][d];///还剩sum个球所用的最短期已找到 if(sum==0) ans[sum][a][b][c][d]=0;///所有填充完毕 else { int flag=0; for(int i=1; i<=n; i++) { if(a==i&&B[i]>0) continue; if(b==i&&B[i]>1) continue; if(c==i&&B[i]>2) continue; if(d==i&&B[i]>3) continue; flag=1; int t=dfs(sum-A[i],i,a,b,c);///第i人在充气球 ans[sum][a][b][c][d]=min(ans[sum][a][b][c][d],t+1);///+1是由于最后还有一我的在充 } if(!flag)///都在休息 { int t=dfs(sum,0,a,b,c); ans[sum][a][b][c][d]=min(ans[sum][a][b][c][d],t+1); } }; return ans[sum][a][b][c][d]; } int main() { scanf("%d%d",&m,&n); memset(ans,INF,sizeof(ans)); for(int i=1; i<=n; i++) scanf("%d%d",&A[i],&B[i]); res=dfs(m,0,0,0,0); printf("%d\n",res); return 0; }
写这题的时候那个oj的判题有问题,我写错了一个地方,致使样例都过不了,还AC了。。。。。 不过这个思路应该没问题
若有不对的地方,烦请大佬指出,谢谢
小结:像这类题,观察数据范围,好比数组B,范围小,就能开一个数组记忆最少时间,而后和DFS结合。
本身对记忆化搜索不太感冒,还要勤加练习。string