|洛谷|堆|P3045 [USACO12FEB]牛券Cow Coupons

https://www.luogu.org/problem/show?pid=3045

把c作为关键字放入一个小根堆,在堆里买k个牛,然后再把这个小根堆里的牛放到另一个以p作为关键字的小根堆,买牛直到没钱

注意m要long long

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
int n,k;
long long m,ans = 0;
struct cow
{
       int p,c;
};
struct cmp1
{
       bool operator()(cow a, cow b)
       {
            return a.c>b.c;
       }
};
struct cmp2
{
       bool operator()(cow a, cow b)
       {
            return a.p>b.p;
       }
};
priority_queue, cmp1> p1;
priority_queue, cmp2> p2;
int main()
{
   scanf("%d%d%lld", &n, &k ,&m);
   for (int i=1;i<=n;i++)
   {
       cow c;
       scanf("%d%d", &c.p, &c.c);
       p1.push(c);
   }
   while (k>0&&!p1.empty())
   {
         if (m-p1.top().c>=0)
         {
            m -= p1.top().c; p1.pop(); ans++; k--;
         } else break;
   }
   while (!p1.empty()) {p2.push(p1.top()); p1.pop();   }   
   while (!p2.empty())
   {
         if (m-p2.top().p>=0)
         {
            m -= p2.top().p; p2.pop(); ans++;
         } else break;
   }
   printf("%lld\n", ans);
   system("pause");
    return 0;
}


转载于:https://www.cnblogs.com/flyinthesky1/p/6384238.html

你可能感兴趣的:(|洛谷|堆|P3045 [USACO12FEB]牛券Cow Coupons)