qdu Blue Bridge training queue to fetch water

Subject description
has queued to r n individual taps to fetch water, they filled buckets of time t1, t2 ............ tn is an integer and each are not equal, should be how to arrange their time fetching water order to make them cost a total of least?

Data size and conventions
which 80% of the data to ensure that n <= 10

Enter
the first row n, r (n <= 500 , r <= 75)
the second n individual behavior kick elapsed time Ti (Ti <= 100);
output
the least time spent
Sample Input

3 2
1 2 3

Sample Output

7

And it can be divided into a total time of each person and each person's time to draw with and the wait time, because each person's waiting time is not a good count directly, we need to keep it connected with a vector water faucet on each staff Information. So you can forget

#include<bits/stdc++.h>
using namespace std;
const	int N=1010;
int a[N];
vector<int>vec[N];
int main()
{
	int n,m;
	cin>>n>>m;
	long long sum=0;
	int tx=0;
	for(int i=0;i<n;i++)
	cin>>a[i];
		sort(a,a+n);
	for(int i=0;i<n;i++)
	{
	sum+=a[i];
	vec[tx].push_back(a[i]);
	tx=(tx+1)%m;
	}

	long long ans=0;
	for(int i=0;i<m;i++)
	{
		long long ss=0;
		if(vec[i].size())
		{
		for(int j=0;j<vec[i].size()-1;j++)
		{
			ss+=vec[i][j];
			ans+=ss;
		}
			}
	}
	cout<<ans+sum<<endl;
}
Published 165 original articles · won praise 8 · views 2479

Guess you like

Origin blog.csdn.net/qq_45961321/article/details/104956258