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;
    }



  • 相关阅读:
    分布式和集群
    c++ >>
    c++ ip地址相关
    c++ ip地址的操作 c版
    c++ 缺少动态库
    c++ dirname() basename()
    shell ulimit -n
    shell 进程查询相关的命令
    shell grep 高亮
    c++ swap 函数
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3508724.html
Copyright © 2011-2022 走看看