zoukankan      html  css  js  c++  java
  • 阿浜的冰可乐

    题目来源

                 http://www.zylpb.cn/problem/1012
    

    描述

    阿浜喜欢喝肥宅快乐水,并且只喝冰可乐,他认为常温可乐是没有灵魂的。

    他有 n 瓶冰可乐,第 i 瓶冰可乐喝完需要 ti1 分钟,而保持冰度的最后时间为 ti2 分钟,因此他想要喝完第 i 瓶可乐就必须在 ti2 分钟之前喝完。

    阿浜喝可乐只会喝完一瓶可乐才会喝下瓶可乐,不能够同时喝多瓶可乐,他现在想知道能够喝完的最大冰可乐的瓶数。

    输入

    第一行一个整数n (1<=n<=200000)

    随后n行,每行两个整数ti1,ti2 (1<=ti1<ti2<=200000000)

    输出

    输出一个整数表示阿浜能够喝完的冰可乐的最大瓶数

    输入样例 1

    4
    2 3
    3 5
    4 5
    5 6

    输出样例 1

    2

    题目思路

       运用贪心思想,想要获得最短时间,就得将所经历的时间全部运用到位。
       先按照ti2的时间排序,方便后面进行安排。(一个一个的添加到坐标轴中)
       然后设置储存一个所用时间的变量y以及优先队列。
       如果y + ti1 <= ti2 说明能够放进优先队列,continue。
       如果不能放进去并且我们这个元素的时间比优先队列队首的时间少,那我们就将其替换一下,换成小的,使得在此时间之前所放进去的结果是最优的,在前y个时间中喝的瓶数达到最多。(优先队列默认大根堆,所以只要去队首即可。)
       最后输出优先队列的大小。
    

    题解

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    typedef pair<int, int> PII;
    priority_queue<PII> q;
    vector<PII> alls;
    
    int main() {
    	int t;
    	scanf("%d", &t);
    	while (t--) {
    		int a, b;
    		scanf("%d%d", &a, &b);
    		alls.push_back({ a, b });
    	}
    
    	sort(alls.begin(), alls.end(), [](PII a, PII b) {
    		if (a.second != b.second) return a.second < b.second;
    		return a.first < b.first;
    		});
    
    	int y = 0;
    	for (auto i : alls) {
    		if (y + i.first <= i.second) {
    			y += i.first;
    			q.push(i);
    			continue;
    		}
    
    		PII u = q.top();
    		if (u.first > i.first) {
    			q.pop();
    			y -= u.first;
    			y += i.first;
    			q.push(i);
    		}
    	}
    
    	printf("%d
    ", q.size());
    	return 0;
    }
    
  • 相关阅读:
    yii 引入文件
    CodeForces 621C Wet Shark and Flowers
    面试题题解
    POJ 2251 Dungeon Master
    HDU 5935 Car(模拟)
    HDU 5938 Four Operations(暴力枚举)
    CodeForces 722C Destroying Array(并查集)
    HDU 5547 Sudoku(dfs)
    HDU 5583 Kingdom of Black and White(模拟)
    HDU 5512 Pagodas(等差数列)
  • 原文地址:https://www.cnblogs.com/Attacking-vincent/p/13223093.html
Copyright © 2011-2022 走看看