Title Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1588
Indicates that there is a complete algorithm class code, but that you do not understand. The specific idea is the first order, after the full use of the results of the first order, the first number of the resulting insert into the back, through this method to improve the overall efficiency.
#include <iostream>#include<stdio.h>#include<algorithm>using namespacestd;#defineMax 1005intA[max];intMain () {intt,b; Long Longcount; scanf ("%d",&t); while(t--) {scanf ("%d",&b); for(inti =0; I < b;i++) scanf ("%d", A +i); Count=0; intc =b; Sort (A,a+b); while(c>=2) {a[b-c+1]+=a[b-c]; Count+ = a[b-c+1]; //for (int i =b-c+1;i <b;i++) cout<<a[i]<< "";cout<<endl;//cout<< "b-c+1" <<b-c+1<<endl; for(inti = b-c+1; I < b;i++){ if(a[b-c+1]<A[i]) { intTemp =a[b-c+1]; for(intJ =b-c+2; J <i;j++) {a[j-1]=A[j]; } a[i-1]=temp; Break; }Else if(a[b-c+1]>=a[b-1]){ intTemp =a[b-c+1]; for(intJ =b-c+2; J < b;j++) {a[j-1]=A[j]; } a[b-1]=temp; Break; } } //for (int i =b-c+1;i <b;i++) cout<<a[i]<< "";cout<<endl;c--; } cout<<count<<Endl; } return 0;}
1588: Merging fruit