zoukankan      html  css  js  c++  java
  • 【POJ3614 Sunscreen】【贪心】

    题面:

    有c头牛,需要的亮度在[min_ci,max_ci]中,有n种药,每种m瓶,可以使亮度变为v 问最多能满足多少头牛

    算法

    我们自然考虑贪心,我们首先对每头牛的min进行排序,然后对于每种药,将min<v的牛拿出来讨论
    我们自然会先把药给max较小的牛来使用 max较大的留到后面 这样有更大的可能性,且不会丢失最优解

    代码

    实现上的一些问题在注释里

    #include<iostream>
    #include<cstdio>
    #include <queue>
    #include<algorithm>
    #define MAXN 2555
    #define INF 1000000007
    #define ll long long
    using namespace std;
    ll c,n;
    typedef pair<ll,ll> QWQ;//定义一个类型pair 
    QWQ cow[MAXN],scr[MAXN];
    priority_queue<ll>QAQ;
    int main()
    {
    	scanf("%lld%lld",&c,&n);
    	for(ll i = 1;i <= c;i++)scanf("%lld%lld",&cow[i].first,&cow[i].second);
    	for(ll i = 1;i <= n;i++)scanf("%lld%lld",&scr[i].first,&scr[i].second);
    	sort(cow + 1,cow + 1 + c);
    	sort(scr + 1,scr + 1 + n);
    	ll now = 1,ans = 0;
    	for(ll i = 1;i <= n;i++)
    	{
    		for(;now <= c && cow[now].first <= scr[i].first;now++)//将min<v的牛拿出来考虑 
    		QAQ.push(-cow[now].second); //max较小的在前 这里用相反数建立小根堆 
    		while(!QAQ.empty()&&scr[i].second)//这瓶药是否能用 
    		{
    			ll x = -QAQ.top();
    			QAQ.pop();
    			if(x >= scr[i].first)
    			{
    				ans ++;
    				scr[i].second--;
    			}
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    
  • 相关阅读:
    Boot.ini
    CCP4 SET
    Install GTK+ GLIB
    C head file
    Changes in Python
    ubuntu
    错误: 配置节中设置 validateRequest=false 可以禁用请求验证
    c++中冒号(:)和双冒号(::)的用法
    C++学习之类和结构体
    C++中双冒号的作用
  • 原文地址:https://www.cnblogs.com/dixiao/p/13708827.html
Copyright © 2011-2022 走看看