min_element/max_element的應用(貪心)

某工廠有n個獨立的作業,由m臺相同的機器進行加工處理。作業i所需的加工時間爲ti,任何作業在被處理時不能中斷,也不能進行拆分處理。現廠長請你給他寫一個程序:算出n個作業由m臺機器加工處理的最短時間。

 輸入

第一行T(1<T<100)表示有T組測試數據。每組測試數據的第一行分別是整數n,m(1<=n<=10000,1<=m<=100),接下來的一行是n個整數ti(1<=t<=100)。

輸出

所需的最短時間

 

樣例輸入

2

2 2

1 5

6 3

2 5 13 15 16 20

 

樣例輸出

5

28

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int speed[10010];
int mintime[101];
bool cmp( const int &x, const int &y )
{
	return x > y;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int m, n;		
		memset(speed, 0, sizeof(speed));
		memset(mintime, 0, sizeof(mintime));
		cin>>n>>m;
		for(int i = 0; i < n; ++i)
		{
			cin>>speed[i];
		}
		sort(speed, speed + n, cmp);
		for(int i = 0; i < n; ++i)
		{
			*min_element(mintime, mintime + m) += speed[i];
		} 
		for(int i=0;i<n;i++)
		cout<<mintime[i]<<' ';
		cout<<*max_element(mintime, mintime + m)<<endl;
	}
	return 0;
}


mintime爲22,21,28;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章