zoukankan      html  css  js  c++  java
  • UVALive

    题意:收到n个订单,每个订单有q,d分别代表做这个的时间,和最晚的完成时间,问你最多能接受几个订单

    思路:贪心,我们显然要按最早的完成时间排序,那么接下来,我们用(6,8)和(4,9)做为例子,按照我们的贪心原则我们首先选择(6,8),然后再(4,9),但显然(4,9)作为首选才是最好的选择,试想一下不能两个都选的情况,就是我们总共做的时间4+6>9(第二个的最迟的时间),那么我们要删除做的时间最长的才是最优的

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1000002;
    
    struct node{
        int q,d;
    }arr[MAXN];
    int n;
    
    bool cmp(node a,node b){
        return a.d < b.d;
    }
    
    int main(){
        int t;
        scanf("%d",&t);
        for (int cas = 0; cas < t; cas++){
            if (cas)
                printf("
    ");
            scanf("%d",&n);
            for (int i = 0; i < n; i++)
                scanf("%d%d",&arr[i].q,&arr[i].d);
            sort(arr,arr+n,cmp);
            priority_queue<int> Q;
            int ans = 0;
            for (int i = 0; i < n; i++){
                ans += arr[i].q;
                Q.push(arr[i].q);
                while (ans > arr[i].d){
                    ans -= Q.top();
                    Q.pop();
                }
            }
            printf("%d
    ",Q.size());
        }
        return 0;
    }



  • 相关阅读:
    凹透镜
    三角形动点和将军饮马
    数学
    壮壮学习准则
    均值不等式,求极值
    2020年自贡中考数学真题,用的是花钱买的"几何画板",wechat:QZCS12
    90年高考题
    裂项:2005年初中数学竞赛题p32,4
    02-需求来源
    01-产品需求的内涵
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3508724.html
Copyright © 2011-2022 走看看