CodeForces 416C

Booking System

description:

Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called “Dijkstra’s Place” has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: ci and pi — the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all ci people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know ri — the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.

Input:

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of requests from visitors. Then n lines follow. Each line contains two integers: ci, pi (1 ≤ ci, pi ≤ 1000) — the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 ≤ k ≤ 1000) — the number of tables in the restaurant. The last line contains k space-separated integers: r1, r2, …, rk (1 ≤ ri ≤ 1000) — the maximum number of people that can sit at each table.

Output:

In the first line print two integers: m, s — the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines — each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.

If there are multiple optimal answers, print any of them.

Examples:

Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2


题目大意:
有一家餐馆有k张桌子,第i张桌子最大可以坐下Ri个人。现在来了n伙顾客,第 i 群顾客共有Ci个人,将会带来收益Pi。每张桌子只能安排一群顾客,而且同一群顾客都要坐在一张桌子上。问接受哪几群顾客,并分别安排在哪几张桌子可以带来最大的收益。

解题思路:
1.显然根据顾客的需求,对一群顾客都是找一张容纳量大于人数的桌子。而且一张桌子只能给一群顾客使用。那么这就满足贪心策略的要求。由于桌子的数量是有限的,先将顾客群按照收益的高低排序,依次安排就可以获得最大收益。
2.由于最后的结果不仅要输出可以接受的顾客群数量以及总收益,还要按照输入的顺序依次给出接受的顾客次序与对应的桌子编号。所以在输入的时候,是需要记录输入顺序的。
3.用multiset来保存桌子的状态,在对顾客群中设立table_id表示对应分配的桌子id,如果不接受就分配为-1. 排序完成以后,依次读取顾客的数量,并从multiset中找到最小的能坐的下的桌子。如果找不到,或者已经没有桌子了就返回-1.


源代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;

int n,k;

struct node{
    int id;
    int c,p;
    int table_id;
    bool operator <(const node &a) const {
        return p > a.p;
    }
};

struct table{
    int id;
    int num;
    bool operator <(const table &a) const {
        return num < a.num;
    }
};

bool comp(node &a,node &b){
    return a.id1005];
multiset t;
set
::iterator it; int sum = 0,acc=0; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ a[i].id = i; scanf("%d%d",&a[i].c,&a[i].p); } scanf("%d",&k); table temp; for(int i=1;i<=k;i++){ temp.id = i; scanf("%d",&temp.num); t.insert(temp); } sort(a+1,a+1+n); for(int i=1;i<=n;i++){ temp.num = a[i].c; it = t.lower_bound(temp); if(it==t.end()){ a[i].table_id = -1; }else{ a[i].table_id = (*it).id; t.erase(it); sum+=a[i].p; acc++; } } printf("%d %d\n",acc,sum); sort(a+1,a+1+n,comp); for(int i=1;i<=n;i++){ if(a[i].table_id!=-1) printf("%d %d\n",i,a[i].table_id); } return0; }

你可能感兴趣的:(CodeForces)