题目链接:
题目大意:
给出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 );
}
}