CodeForces 416C Booking System(贪心)

Booking System
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
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.

Sample Input

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


比赛时因为写的时间太晚了,有点着急,结果也没写出来。

分析:贪心。按照团队的花费从大到小排序,如果花费相同则按照人数从小到大排序。然后从k张桌子里面挑选可以让这个团队坐下的桌子,然后把这个团队的id和桌子的id记录下来输出即可。

#include<stdio.h>
#include<algorithm>
using namespace std;

int n, k;
struct table //桌子
{
    int id;
    int limit;
    int flag;
}t[1005];
struct group //团队
{
    int num;
    int id;
    int cost;
}g[1005];
struct Ans //答案
{
    int g_id;
    int t_id;
}ans[1005];

bool comp1(group g1, group g2)
{
    if(g1.cost != g2.cost)
        return g1.cost > g2.cost;
    return g1.num < g2.num;
}

bool comp2(table t1, table t2)
{
    return t1.limit < t2.limit;
}

int Search(int x)
{
    for(int i = 0; i < k; i++)
    {
        if(!t[i].flag && t[i].limit >= x)
        {
            t[i].flag = 1;
            return t[i].id;
        }
    }
    return -1;
}

int main()
{
    int i;
    while(~scanf("%d",&n))
    {
        for(i = 0; i < n; i++)
        {
            scanf("%d%d",&g[i].num, &g[i].cost);
            g[i].id = i + 1;
        }
        sort(g, g+n, comp1);
        scanf("%d",&k);
        for(i = 0; i < k; i++)
        {
            scanf("%d",&t[i].limit);
            t[i].flag = 0;
            t[i].id = i + 1;
        }
        sort(t, t+k, comp2);
        int s = 0, sum = 0;
        for(i = 0; i < n; i++)
        {
            int pos = Search(g[i].num);
            if(pos == -1) continue;
            ans[s].g_id = g[i].id;
            ans[s++].t_id = pos;
            sum += g[i].cost;
        }
        printf("%d %d\n",s, sum);
        for(i = 0; i < s; i++)
            printf("%d %d\n",ans[i].g_id, ans[i].t_id);
    }
    return 0;
}


你可能感兴趣的:(贪心)