UVA 11729 简单贪心

添加链接描述

结论:根据完成的时间排序。

证明:假设交代任务时间为a,完成任务时间为b。选取n个人之中的两个人进行观察,这两个人之间的顺序对其他人不产生影响,假设b1 > b2,当b1在前面的时候,二者为max1(a1+a2+b2,a1+b1),当b1在后面的时候max2(a2+b2,a1+a2+b1),因为b1 > b2,观察max2(a2+b2,a1+a2+b1) = a1 + a2 + b1,显然其与max1中的两项比较,a1 + a2 + b1 > a1 + a2,a1 + a2 + b1 >a1 + b1,当二者顺序不同的时候,max1 < max2 , 显然 max1 更优 ,即应该是完成任务时间长的在前面。

附上 vegetable 的代码。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=1010;

int n;
struct Node
{
	int b,j;
	bool operator < (const Node &W) const
	{
		return j>W.j;
	}
}a[N];

int main()
{
	int T=1;
	while(cin>>n&&n)
	{
		for(int i=1;i<=n;i++)
			cin>>a[i].b>>a[i].j;
		
		sort(a+1,a+1+n);
		
		int ans=0,cnt=0;
		
		for(int i=1;i<=n;i++)
		{
			cnt+=a[i].b;
			if(ans<a[i].j+cnt) 
				ans=a[i].j+cnt;
		}
		
		printf("Case %d: %d\n",T++,ans);
	}
	

	return 0;
}








发布了43 篇原创文章 · 获赞 1 · 访问量 1583

猜你喜欢

转载自blog.csdn.net/DaNIelLAk/article/details/104752232