Installations UVALive - 4850

问题

https://vj.z180.cn/57cf802531d0c7c8008f2d38be7c24a6?v=1582873709

分析

这题如果是求最大延迟最小,那么就按照结束时间从小到大排序,使用贪心即可。但是现在要求是最大的两个惩罚值的和最小。所以要改变。
参考:https://blog.csdn.net/The_useless/article/details/53606549
但是我没想明白为什么只往pos后插入就可以了
测了好多答案,都是错的,别人的代码我也测了几份,也是WA,评测出错了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=600+5;
struct Task{
    int s,d;
    bool operator < (const Task &rhs) const{
        return d<rhs.d || (d==rhs.d && s<rhs.s);
    }
};
int n,kase=0,pos;
Task task[maxn];

int solve(int x){
    int cur=0,max1=0,max2=0;
    for(int i=0;i<=pos;++i){
        if(i==x) continue;
        cur+=task[i].s;
        if(cur-task[i].d>max2){
            max2=cur-task[i].d;
            if(max2>max1) swap(max2,max1);
        }
    }
    cur+=task[x].s;
    if(cur-task[x].d>max2){
        max2=cur-task[x].d;
        if(max2>max1) swap(max2,max1);
    }
    for(int i=pos+1;i<n;++i){
        cur+=task[i].s;
        if(cur-task[i].d>max2){
            max2=cur-task[i].d;
            if(max2>max1) swap(max2,max1);
        }
    }
    return max1+max2;
}

int main(void){
    scanf("%d",&kase);
    while(kase--){
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            scanf("%d%d",&task[i].s,&task[i].d);
        }
        sort(task,task+n);
        pos=0;
        //cur记录现在的时间,max1,max2记录两个最大的延迟,pos记录两个延迟对应的序号中较大的
        int cur=0,max1=0,max2=0;
        for(int i=0;i<n;++i){
            cur+=task[i].s;
            if(cur-task[i].d>max2){  //如果值大于等于第二大的
                pos=i;
                max2=cur-task[i].d;
                if(max2>max1)  //如果值最大
                    swap(max1,max2);
            }
        }
        int ans=max1+max2;
        for(int i=0;i<pos;i++)  //尝试置换位置使得ans更优
            ans=min(ans,solve(i));
        printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值