当前位置: 首页 > 工具软件 > Commando > 使用案例 >

Commando War

魏威
2023-12-01

题目传送

题意:

你给N个士兵交代任务,其中你对第i个士兵花费bi的时间来介绍,而第i个士兵则要花费ji的时间来完成,问最小花费多少时间使所有的任务都完成?

思路:

安排任务花费时间越多的人,越早完成它,进行一次贪心算法,按照J对士兵们进行从大到小的排序,然后每次比较每个士兵最晚完成任务的值,取个最大值就行。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct node
{
	int b,j;
	bool operator <(const node& x)const
	{
		return j>x.j;
	}
 } s[N];
 int n,p;
int main(){
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)break;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&s[i].b,&s[i].j);
		}
		sort(s+1,s+1+n);
		int ans=0,tot=0;
		for(int i=1;i<=n;i++)
		{
			tot+=s[i].b;
			ans=max(ans,tot+s[i].j);
		}
		printf("Case %d: %d\n",++p,ans);
	}

	return 0;
} 
 类似资料:

相关阅读

相关文章

相关问答