Blue Bridge Cup algorithm for improving water-fetching problem logic strategy greedy

Source: Internet
Author: User
Tags time limit

Algorithm to improve water-fetching problem
Time limit: 1.0s memory limit: 512.0MB
Submit this question
Problem description
n individuals to draw water, there is a M faucet, the first person to take water time ti, please arrange a reasonable plan to make everyone's waiting time as small as possible.
Input format
First line two positive integer n M next row n positive integer ti.
n,m<=1000,ti<=1000
Output format
The sum of the minimum waiting time. (No need to export the specific scheduling scheme)
Sample input
7 3
3 6 1 4 2 5 7
Sample output
11
Tips
One of the best water-fetching schemes is to assign n individuals to the M tap water in order of TI from small to large.
For example, ti from small to large sort of 1,2,3,4,5,6,7, they are assigned to 3 taps, then go to the tap of a water for the 1,4,7; to tap the water for 2, 5; Go to the third faucet for the 3, 6.
The first tap water person total wait time = 0 + 1 + (1 + 4) = 6
The second tap water person total wait time = 0 + 2 = 2
The third leading water man total wait time = 0 + 3 = 3
So the total wait time = 6 + 2 + 3 = 11

http://lx.lanqiao.cn/problem.page?gpid=T224
Greedy strategy.
In order to allow the shortest waiting time, so let the small first to pick up water
In order to make the overall waiting time shortest, so small to wait for a small place to pick up water
So the next time in this faucet to wait for the shortest water

 #include <iostream> #include <string> #include <cstring> #include <
stdio.h> #include <cmath> #include <algorithm> using namespace std;
int a[10000];
int d[10000];
     int main () {int n,m;
         while (cin>>n>>m) {memset (d,0,sizeof (d));
         memset (A,0,sizeof (a));
         for (int i=0;i<n;i++) {cin>>a[i];
         } sort (a,a+n);//Sort priority allows small water-sum=0 int;
            for (int i=0;i<n;i++) {sort (d,d+m);//Sort, let small to wait for the shortest place to connect water sum+=d[0];
     d[0]+=a[i];//the smallest faucet plus wait time} 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.