Algorithm to improve the problem of queue fetching water
Time limit: 1.0s memory limit: 256.0MB
Submit this question
Problem description
There are n people queuing up to r taps to fetch water, they fill buckets of time T1, t2...........tn are integers and unequal, how to arrange their water order so that they spend the least amount of time.
Input format
First line N,r (n<=500,r<=75)
Second act n the time taken by the individual to fetch Ti (TI<=100);
Output format
Minimum time to spend
Sample input
3 2
1 2 3
Sample output
7
Data size and conventions
80% of these data guarantee n<=10
http://lx.lanqiao.cn/problem.page?gpid=T292
First of all, the shortest time is the most important of the problem, then you need to keep the minimum water time in front of the person,
After that, you have to keep the water people waiting for the shortest time.
Create two arrays, one representing the waiting time for water, a representative
The waiting time of the faucet where the water is being added up
Then the person in the back preferred to wait for the least time tap to pick up the water.
A total of n orders, each time for R*LG (R)
So the complexity of N*R*LG (R)
The maximum data is 500*75*9 very small--
#include <iostream> #include <cstring> #include <cstdio> #include <
algorithm> #include <cmath> #include <map> using namespace std;
int d[505];//waits for the time int x[505];//to water the person int main () {int n,r;
while (cin>>n>>r) {int sum=0;
Memset (d,0,sizeof (d));
for (int i=0;i<n;i++) {cin>>x[i];
} sort (x,x+n); for (int i=0;i<n;i++) {sort (d,d+r);//The faucet with the smallest waiting time is discharged to the front//for (int j=0;j<r;j++) cout<&
lt;d[j]<< ';cout<<endl;
Sum+=d[0]+x[i];
D[0]+=x[i]; for (int j=0;j<r;j++) cout<<d[j]<< "; Cout<<endl<<sum<<endl;cout<<endl
<<endl;
} cout<<sum<<endl; }
}