codeforces 416C C. Booking System(贪心)

题目链接:

codeforces 416C


题目大意:

给出n个请求,每个请求包括客人数量和支付金额,再给出m个桌子,包括桌子大小,问如何安排才能最大盈利。给出最大盈利和一个能够最大盈利的方案。


题目分析:

  • 采取如下的贪心策略,首先将请求按照支付金额从大到小排序,如果支付金额相同,按照人数从小到大排序,然后我们选取桌子中符合要求最小的桌子,这样就能得到最优方案。
  • 那么对于我们选取的这张桌子,是我们选取当前请求的最小的桌子,所以当前请求之后的请求不会出现比当前金额大的请求能够用这张桌子,所以这样选择一定能够得到最优方案。

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#define MAX 1007

using namespace std;

struct Node
{
    int c,p,id;
    bool operator < ( const Node & a ) const 
    {
        if ( p == a.p ) return c < a.c;
        return p > a.p;
    }
}p[MAX];

struct PP
{
    int a,b;
    PP ( int x , int y )
    {
        a = x;
        b = y;
    }
    bool operator < ( const PP &x ) const 
    {
        return a < x.a;
    }
};

int n,k,r,m;
multiset<PP> table;
vector<pair<int,int> > pp;

int main ( )
{
    while ( ~scanf ( "%d" , &n ) )
    {
        for ( int i = 0 ; i < n ; i++ )
        {
            scanf ( "%d%d" , &p[i].c , &p[i].p );
            p[i].id = i+1;
        }
        sort ( p , p+n );
        scanf ( "%d" , &m );
        for ( int i = 0 ; i < m ; i++ )
        {
            scanf ( "%d" , &r );
            table.insert ( PP ( r , i+1 ) );
        }
        int ans = 0;
        multiset<PP>::iterator it;
        for ( int i = 0 ; i < n ; i++ )
        {
            it = table.lower_bound ( PP ( p[i].c , 1 ) );
            if ( it == table.end() ) continue;
            ans += p[i].p;
            table.erase ( it );
            pp.push_back ( make_pair ( p[i].id , it->b ) );
        }
        printf ( "%d %d\n" , (int)pp.size() ,ans );
        for ( int i = 0 ; i < pp.size() ; i++ )
            printf ( "%d %d\n" , pp[i].first , pp[i].second );
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值