zoukankan      html  css  js  c++  java
  • _bzoj1029 [JSOI2007]建筑抢修【贪心 堆】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1029

    经典的贪心问题,不解释。

    #include <cstdio>
    #include <algorithm>
    #include <queue>
    
    const int maxn = 150005;
    
    int n, ima, ans;
    struct st {
    	int x, y;
    } a[maxn];
    std::priority_queue<int> hp;
    
    bool cmp(const st & aa, const st & ss) {
    	return aa.y < ss.y;
    }
    
    int main(void) {
    	//freopen("in.txt", "r", stdin);
    	scanf("%d", &n);
    	for (int i = 0; i < n; ++i) {
    		scanf("%d%d", &a[i].x, &a[i].y);
    	}
    	std::sort(a, a + n, cmp);
    	
    	for (int i = 0; i < n; ++i) {
    		if (ima + a[i].x <= a[i].y) {
    			ima += a[i].x;
    			++ans;
    			hp.push(a[i].x);
    		}
    		else if (a[i].x < hp.top()) {
    			ima = ima - hp.top() + a[i].x;
    			hp.pop();
    			hp.push(a[i].x);
    		}
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    

      

  • 相关阅读:
    子网掩码的作用与IP网段的划分
    DHCP服务器
    Anaconda安装、更新第三方包
    time模块的使用
    TensorFlow安装
    机器学习-线性回归
    机器学习
    Pyhton-类(2)
    python-类(1)
    Python-函数
  • 原文地址:https://www.cnblogs.com/ciao-sora/p/6375646.html
Copyright © 2011-2022 走看看