CF1443C. The Delivery Dilemma

传送门:https://codeforces.com/contest/1443/problem/C

题意:

需要买n个菜,每个菜有两个选择,自己去买和点外卖,自己买需要一个一个去,点外卖是同时送来,求最少需要花费多长时间。

题解:

首先按照外卖送到时间从晚到早sort,然后从前往后依次枚举,从全都叫外卖到全都自己买,这样,自己买花费的时间就会越来越长,最慢的外卖送来的时间就会越来越早,维护最优解

注意,当遇到多组测例时,谨慎使用memset,否则会T

#include<bits/stdc++.h>
#define LL long long
using namespace std;
struct Dish{
    int a,b;
    friend bool operator >(const Dish &x,const Dish &y){
        return x.a>y.a;
    }
}dish[200005];


int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        //memset(dish,0,sizeof dish);
        for(int i=1;i<=n;i++){
            scanf("%d",&dish[i].a);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&dish[i].b);
        }
        dish[n+1].a=dish[n+1].b=0;
        //全部要求外卖
        //直到全部亲历亲为
        sort(dish+1,dish+1+n,greater<Dish>());
        LL ans=0x3f3f3f3f3f3f3f3f;
        LL sum=0;
        for(int i=0;i<=n;i++){
            sum+=dish[i].b;
            ans=min(max(sum,1LL*dish[i+1].a),ans);
        }
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/isakovsky/p/13964109.html