请在 下方输入 要搜索的题目:

在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。

在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。

发布时间:2025-06-04 19:22:21
推荐参考答案 ( 由 快搜搜题库 官方老师解答 )
联系客服
答案:答:vara:array[1..10000] of integer;b:array[1..10000] of longint;i,j,k,n,m,open,closed,ans:longint;procedure swap(var x,y:integer);var t:integer;begint:=x;x:=y;y:=t;end;procedure qsort(s,t:longint);var i,j,mid:longint;begini:=s;j:=t;mid:=a[(s t) div 2];while i<=j do   begin     while a[i]mid do dec(j);     if i<=j then       begin         swap(a[i],a[j]);         inc(i);dec(j);       end;   end;if is then qsort(s,j);end;beginreadln(n,m);for i:=1 to n do read(a[i]);qsort(1,n);closed:=1;open:=1;fillchar(b,sizeof(b),0);k:=n;while k>m do    begin       k:=k div m k mod m;    end;   for i:=1 to k do b[1]:=b[1] a[i];k:=k 1;if k<=n thenrepeat   inc(closed);   for i:=1 to m do    begin     if ((a[k]<=b[open])or(open=closed))and(k<=n) then      begin         inc(b[closed],a[k]);         inc(k);      end     else if ((a[k]>b[open])or(k>n))and(openn);ans:=0;for i:=1 to closed do inc(ans,b[i]);writeln(ans);end.for(i=0,j=0;j<=cols 1;j ) maze[i][j]='1';for(i=rows 1,j=0;j<=cols 1;j ) maze[i][j]='1';*startpos=start;*endpos=end;return OK;}int canPass(Position curpos){if(maze[curpos.x][curpos.y]=='0')return TRUE;return FALSE;}void markPos(Position curpos,MarkTag tag){ //为已走过的位置标记switch(tag){case YES: maze[curpos.x][curpos.y]='.'; break; //路径标记case NO: maze[curpos.x][curpos.y]='#'; break; //死胡同标记}}Position nextPos(Position curpos,Direction dir){//根据当前的位置坐标和下一步要探索的方向dir求下一步要走的位置坐标Position nextpos;switch(dir){case RIGHT: nextpos.x=curpos.x; nextpos.y=curpos.y 1; break;case DOWN: nextpos.x=curpos.x 1; nextpos.y=curpos.y; break;case LEFT: nextpos.x=curpos.x; nextpos.y=curpos.y-1; break;case UP: nextpos.x=curpos.x-1; nextpos.y=curpos.y; break;}return nextpos;}Direction nextDir(Direction dir){switch(dir){ //按照RIGHT DOWN LEFT UP的次序进行路径探索case RIGHT: return DOWN;case DOWN: return LEFT;case LEFT: return UP;}}/*若迷宫中存在从入口start到出口end的通道,则求得一条存放在栈S中,并返回TRUE,若不存在则返回FALSE*/int Solve(Stack *S,Position start,Position end){Position curpos;SElemType e;int curstep=1;if(InitStack(S)==ERROR)return FALSE;curpos=start;do{if(canPass(curpos)){ //当前位置可以通过markPos(curpos,YES); //留下足迹e.order=curstep;e.seat=curpos;e.di=RIGHT;Push(S,e);if(curpos.x==end.x && curpos.y=end.y)return TRUE; //找到从入口到出口的通道curpos=nextPos(curpos,RIGHT);curstep ;}else{if(!Empty(*S)){ //当前位置不能通过if(Pos(S,&e)==ERROR)return FALSE;while(e.di==UP && !Empty(*S)){//4个方向都找不到通路,则回溯curpos=e.seat;markPos(curpos,NO);if(Pop(S,&e)==ERROR)return FALSE;}if(e.di!=UP){ //4个方向还没有探索完e.di=nextDir(e.di);Push(S,e); //换下一个方向探索curpos=nextPos(e.seat,e.di);}}}while(!Empty(*S));return FALSE;}void main(void){Position startPos,endPos;Stack path;SElemType e;char *fname="in.txt";if(createMaze(fname,&startPos,&endPos)==ERROR) return;Solve(&path,startPos,endPos);while(!Empty(path)){ //输出出口到入口的路径Pop(&path,&e);printf("(%d,%d)\n",e.seat.x,e.seat.y);}}
专业技术学习
专业技术学习
搜搜题库系统