(CodeForces - 416C)Booking System

这篇博客讨论了如何优化餐厅预订系统,以最大化收入。给定每个预订请求的团体人数和预计消费,以及餐厅的桌子数量和最大容量,目标是确定哪些请求应该接受,使得总收入最大化。解决方案采用贪心策略,优先考虑支付金额高的预订,并尝试找到合适大小的桌子来接纳他们。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(CodeForces - 416C)Booking System

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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

题目大意:有n组人预订了酒店,给出每组人的人数和能付给酒店的钱,酒店有k张桌子,给出每张桌子可以容纳的人数。每组人一定要坐在同一张桌子上,如果坐不下那么这组人离开酒店自然不会给酒店钱。问如何安排才能使酒店获得更多的钱。

思路:贪心。对于每组人按照他们给的钱数从大到小排序,如果钱数相同,人数小的优先。在对桌子按能容纳人数从小到大排序。钱数多的那组人先给他们安排找桌子坐下,当然是要找一个能容纳人数大于等于那组人数桌子但也不用是最大的那张(不能大材小用)。具体见代码。

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn=10005;
int ans1[maxn],ans2[maxn];

struct node1
{
    int id,num,pay;
}a[maxn];

bool cmp1(node1 a,node1 b)
{
    return (a.pay>b.pay||(a.pay==b.pay&&a.num<b.num));
}

struct node2
{
    int id,vol;
    bool vis;
}b[maxn];

bool cmp2(node2 a,node2 b)
{
    return a.vol<b.vol;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].num,&a[i].pay);
            a[i].id=i;
        }
        sort(a+1,a+1+n,cmp1);
        int k;
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
            scanf("%d",&b[i].vol);
            b[i].vis=0;
            b[i].id=i;
        }
        sort(b+1,b+1+k,cmp2);
        int m=0,s=0,tot=0;
        for(int i=1;i<=n;i++)
        {
            bool flag=false;
            for(int j=1;j<=k;j++)
                if(!b[j].vis&&b[j].vol>=a[i].num)
                {
                    b[j].vis=1;
                    ans2[tot]=b[j].id;
                    flag=true;
                    break;
                }
            if(flag)
            {
                ans1[tot++]=a[i].id;
                m++;
                s+=a[i].pay;
            }
        }
        printf("%d %d\n",m,s);
        for(int i=0;i<tot;i++)
            printf("%d %d\n",ans1[i],ans2[i]);  
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值