algorithm to improve the queuing water problem.

Source: Internet
Author: User

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; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.