Uva-1467-Installations

150 篇文章 0 订阅

这个题的大意是说有n个服务,第i个服务需要si的单位时间安装,截止时间为di。要求求出惩罚值最大的两个任务的惩罚值之和最小。

我的解法是,首先按照截止时间作为第一关键字进行从小到大排序,若截止时间相同,则花费时间少的排在前面。然后从头到尾找到2个惩罚值的最大值,并记录最后一个最大值的位置。

然后进行每次将前面的服务放到最后的操作,更新答案值。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=501;
struct node
{
    int s;
    int d;
    bool operator < (const node &a)const
    {
	if(d==a.d)
	    return s<a.s;
	return d<a.d;
    }
}a[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	    scanf("%d%d",&a[i].s,&a[i].d);
	sort(a,a+n);
	int now=0,ta=0,tb=0,pos=0;
	for(int i=0;i<n;i++)
	{
	    now+=a[i].s;
	    int t=max(now-a[i].d,0);
	    if(t>ta)
	    {
		tb=ta;
		ta=t;
		pos=i;
	    }
	    else if(t>tb)
	    {
		tb=t;
		pos=i;
	    }
	}
	int ans=ta+tb;
	for(int i=0;i<=pos;i++)
	{
	    now=0,ta=0,tb=0;
	    for(int j=0;j<=pos;j++)
	    {
		if(i==j)
		    continue;
		now+=a[j].s;
		int t=max(now-a[j].d,0);
		if(t>ta)
		{
		    tb=ta;
		    ta=t;
		}
		else if(t>tb)
		    tb=t;
	    }
	    now+=a[i].s;
	    int t=max(now-a[i].d,0);
	    if(t>ta)
	    {
		tb=ta;
		ta=t;
	    }
	    else if(t>tb)
		tb=t;
	    ans=min(ans,ta+tb);
	}
	printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值