• GHOJ 926 小X的AK计划


    题目描述

            虽然在小X的家乡,有机房一条街,街上有很多机房。每个机房里都有一万个人在切题。小X刚刷完CodeChef,准备出来逛逛。机房一条街有n个机房,第i个机房的坐标为xi,小X的家坐标为0。小X在街上移动的速度为1,即从x1到x2所耗费的时间为|x1−x2|。

            每个机房的学生数量不同,ACM题目水平也良莠不齐。小X到达第i个机房后,可以花 ti的时间想题,然后瞬间AK;当然,也可以过机房而不入。

    小X现在只有m个单位时间,之后他就该赶着去打Codeforces了。现在他想知道自己最多能在多少个机房AK,希望你帮帮他。

    输入格式

            第一行包含两个整数n,m。

            接下来n行,每行包含两个整数xi,ti。

    输出格式

            一行,包含一个整数,表示小X最多能AK的机房数量。

    输入样例

    2 10

    1 100

    5 5

    输出样例

    1

    题解

            我们可以按距离排序,然后从距离近的像距离远的枚举,每次尝试和前面的选取方案更换即可。

            我们可以用堆来维护前面的方案,如果还有足够的剩余时间,则将当前机房放入方案里;否则,如果当前机房耗时比前面耗时最大的机房耗时少,则更换这两个机房。

            具体参考下面的代码。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    
    #define MAX_N (100000 + 5) 
    
    using namespace std;
    
    struct Node 
    {
        long long x;
        int t;
        friend inline bool operator < (Node a, Node b)
        {
            return a.x < b.x;
        }
    };
    
    int n;
    long long m;
    Node a[MAX_N];
    priority_queue <int> q;
    int ans;
    
    int main() 
    {
        scanf("%d%lld", &n, &m);
        for(int i = 1; i <= n; ++i) 
        {
            scanf("%lld%d", &a[i].x, &a[i].t);
        }
        sort(a + 1, a + n + 1);
        for(int i = 1; i <= n; ++i) 
        {
            m -= a[i].x - a[i - 1].x;
            while(m < 0 && !q.empty()) 
            {
                m += q.top();
                q.pop();
            }
            if(m < 0) break;
            if(m >= a[i].t)
            {
                m -= a[i].t;
                q.push(a[i].t);
            }
            else if(!q.empty() && a[i].t < q.top()) 
            {
                m += q.top();
                q.pop();
                m -= a[i].t;
                q.push(a[i].t);
            }
            ans = max(ans, (int)q.size());
        }
        printf("%d", ans);
        return 0;
    }
    参考程序
  • 相关阅读:
    读取手机联络人实例
    MotionEvent中getX()和getRawX()的区别
    Android开源项目发现--- 效率开发工具篇(持续更新)
    开发资源收藏
    ViewHolder VS HolderView ?
    如何测试 Android 中的定时事件
    性能优化实例
    lamba
    并行操作多个序列map
    连续处理函数reduce
  • 原文地址:https://www.cnblogs.com/kcn999/p/11242371.html
Copyright © 2020-2023  润新知